hihoCoder 1174 : 拓扑排序·一

题目链接:http://hihocoder.com/problemset/problem/1174

题目是中文题面我就不说题意了,要看题面的请点击上方链接~

代码实现如下:

 1 #include <queue>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 const int maxn = 1e5 + 7;
 8 int t, n, m, num, u, v;
 9 int InDeg[maxn];
10 vector<int> G[maxn];
11 
12 bool topsort() {
13     num = 0;
14     queue<int> q;
15     for(int i = 1; i <= n; i++) {
16         if(InDeg[i] == 0) {
17             q.push(i);
18         }
19     }
20     int x;
21     while(!q.empty()) {
22         x = q.front(), q.pop();
23         num++;
24         int s = G[x].size();
25         for(int i = 0; i < s; i++) {
26             if(--InDeg[G[x][i]] == 0) {
27                 q.push(G[x][i]);
28             }
29         }
30     }
31     return num == n;
32 }
33 
34 int main() {
35     scanf("%d", &t);
36     while(t--) {
37         scanf("%d%d", &n, &m);
38         for(int i = 0; i <= n; i++) {
39             G[i].clear();
40             InDeg[i] = 0;
41         }
42         for(int i = 0; i < m; i++) {
43             scanf("%d%d", &u, &v);
44             G[u].push_back(v);
45             InDeg[v]++;
46         }
47         if(topsort()) {
48             printf("Correct\n");
49         } else {
50             printf("Wrong\n");
51         }
52     }
53     return 0;
54 }

 

posted @ 2018-05-07 17:43  Dillonh  阅读(265)  评论(0编辑  收藏  举报