-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhe_2.cpp
58 lines (53 loc) · 997 Bytes
/
he_2.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
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <queue>
#include <cstring>
using namespace std;
#define sc(n) scanf("%d", &n)
#define pr(n) printf("%d\n", n)
#define ll long long;
int main()
{
int t;sc(t);
while(t--) {
int n, m;sc(n);sc(m);
int in[n+1];
memset(in, 0, sizeof in);
vector<int> l[n+1];
for(int i=1; i<=m; i++) {
int u, v;
sc(u);sc(v);
l[u].push_back(v);
in[v]+=1;
}
queue<int> q;
bool vis[n+1];
memset(vis, false, sizeof vis);
for(int i=1; i<=n; i++)
if(in[i]==0) {
q.push(i);
vis[i]=true;
}
while(!q.empty()) {
int u=q.front();
q.pop();
for(int i=0; i<l[u].size(); i++) {
if(vis[l[u][i]]==false) {
in[l[u][i]]-=1;
if(in[l[u][i]]==0)
q.push(l[u][i]);
vis[l[u][i]]=true;
}
}
}
int i;
for(i=1; i<=n; i++)
if(vis[i]==false) break;
if(i==n+1)
pr(1);
else
pr(0);
}
return 0;
}