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.
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
Solution:
Use adjacency matrix has to preserve the matrix, and to calculate the degree of each node, determining the sequence of traversal, every elapse of a node determines that the node is not 0 degree, if not, the sequence topology description is not
per after a node, which points to the node of -1, indicating that the parent node points to the node traversal is complete, thereby ensuring the entire sequence is a sequence of topological
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 #include <algorithm> 5 using namespace std; 6 7 8 int main() 9 { 10 int n, m, k; 11 cin >> n >> m; 12 vector<vector<int>>v(n + 1); 13 vector<int>in(n + 1, 0), temp, res;//节点的入度 14 for (int i = 0; i < m; ++i) 15 { 16 int a, b; 17 cin >> a >> b; 18 v[a].push_back(b); 19 in[b]++; 20 } 21 cin >> k; 22 for (int i = 0; i < k; ++i) 23 { 24 bool flag = true; 25 temp = in; 26 for (int= J 0 ; J <n-; ++ J) 27 { 28 int X; 29 CIN >> X; 30 IF (TEMP [X] =! 0 ) = In Flag to false ; 31 is for (Auto A: V [X]) --temp [a]; // occur once a penetration Save 32 } 33 is IF (! In Flag) 34 is res.push_back (I); 35 } 36 for ( int I = 0 ; I <res.size (); + + I) 37 [ COUT << (I == 0 ? "" : " ") << res[i]; 38 return 0; 39 }