-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy path1146_Topological Order (25).cpp
72 lines (62 loc) · 1.33 KB
/
1146_Topological Order (25).cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
// basic topological implement
// maintain in degree value, check in degree is 0 for vertex in order
class DirectedGraph {
public:
DirectedGraph(int n, int m) {
this->vertexNum = n;
this->edgeNum = m;
this->G.resize(n + 1);
this->inDegree.resize(n + 1, 0);
for (int i = 1; i <= n; i++) {
this->G[i].resize(n+1, false);
this->G[i][i] = true;
}
}
void addEdge(int p, int q) {
this->G[p][q] = true;
this->inDegree[q] += 1;
}
bool isTopologicalOrder(const vector<int> &order) {
vector<int> tempInDegree(this->inDegree);
for (const auto &e : order) {
if (tempInDegree[e] != 0) return false;
for (int i = 1; i <= this->vertexNum; i++) {
if (e != i && this->G[e][i])
tempInDegree[i]--;
}
}
return true;
}
private:
int vertexNum, edgeNum;
vector<vector<bool>> G;
vector<int> inDegree;
};
int main() {
int n, m, k, p, q;
cin >> n >> m;
DirectedGraph dg(n, m);
for (int i = 0; i < m; i++) {
cin >> p >> q;
dg.addEdge(p, q);
}
cin >> k;
vector<int> order(n);
int firstPrint = true;
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
cin >> order[j];
}
if (!dg.isTopologicalOrder(order)) {
if (firstPrint) firstPrint = false;
else cout << " ";
cout << i;
}
}
cout << endl;
return 0;
}