PAT甲级1146-Topological Order(拓扑排序一类)

一.题目

This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topological order obtained from the given directed graph? Now you are supposed to write a program to test each of the options.

gre.jpg

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 1,000), the number of vertices in the graph, and M (≤ 10,000), the number of directed edges. Then M lines follow, each gives the start and the end vertices of an edge. The vertices are numbered from 1 to N. After the graph, there is another positive integer K (≤ 100). Then K lines of query follow, each gives a permutation of all the vertices. All the numbers in a line are separated by a space.

Output Specification:

Print in a line all the indices of queries which correspond to "NOT a topological order". The indices start from zero. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line. It is graranteed that there is at least one answer.

Sample Input:

6 8
1 2
1 3
5 2
5 4
2 3
2 6
3 4
6 4
5
1 5 2 3 6 4
5 1 2 6 3 4
5 1 2 3 6 4
5 2 1 6 3 4
1 2 3 4 5 6

Sample Output:

3 4

二.题意

首先输入m(表示有m个数)和n(有八条有向边),接下来n行为有向图的边,接下来输入k(表示有k行),每一行有m个数,然后判断是否是一个拓扑序列,将不是拓扑序列的部分输出。

三.代码

#include
using namespace std;

int main(){
    int n,m,a,b,k,flag=0,in[1010]={0};
    vector vec[1010];//类似于二维的数组(kv型)
    cin>>n>>m;
    for(int i=0;i>a>>b;
        vec[a].push_back(b);//将kv键值对push进去
        in[b]++;//in数组表示入度
    }
    cin>>k;
    for(int i=0;i tin(in,in+n+1);//每次循环将in部分复制到tin中,in+n+1
        for(int j=0;j>a;
            if(tin[a]!=0)
                judge=0;//用于判断是不是拓扑排序
            for(int it:vec[a])
                tin[it]--;//将对应元素的入度减一
        }
        if(judge==1)
            continue;//continue关键字可以进行下一次的循环
        printf("%s%d",flag==1?" ":"",i);//输出
        flag=1;//用于判断是不是第一个
    }
    return 0;
}

四.类似题目

1.Leetcode 207

Course Schedule

There are a total of n courses you have to take, labeled from 0 to n-1.

Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]

Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?

Example 1:

Input: 2, [[1,0]] 
Output: true
Explanation: There are a total of 2 courses to take. 
             To take course 1 you should have finished course 0. So it is possible.

Example 2:

Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take. 
             To take course 1 you should have finished course 0, and to take course 0 you should
             also have finished course 1. So it is impossible.

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.
class Solution {
public:
    bool canFinish(int n, vector>& pre) {
        vector> adj(n, vector());
        vector degree(n, 0);
        for (auto &p: pre) {
            adj[p[1]].push_back(p[0]);
            degree[p[0]]++;
        }
        queue q;
        for (int i = 0; i < n; i++)
            if (degree[i] == 0) q.push(i);
        while (!q.empty()) {
            int curr = q.front(); q.pop(); n--;
            for (auto next: adj[curr])
                if (--degree[next] == 0) q.push(next);
        }
        return n == 0;
    }
};

你可能感兴趣的:(PAT甲级1146-Topological Order(拓扑排序一类))