-
Notifications
You must be signed in to change notification settings - Fork 0
/
URI1610.cpp
57 lines (49 loc) · 825 Bytes
/
URI1610.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
//Feito para treinar com o exemplo do Rodrigo.
#include <bits/stdc++.h>
#define N 150000
using namespace std;
vector<int> G[N];
int t, n, m, a, b;
int vis[N];
bool dfs(int u){
vis[u] = 1;
for(int i =0; i<G[u].size();i++){
int v = G[u][i];
if(vis[v] == 1){
return 1;
}
else if(vis[v] == 0){
int ret = dfs(v);
if(ret==1){
return 1;
}
}
}
vis[u] = 2;
return 0;
}
int main(){
scanf("%d", &t);
while(t--){
scanf("%d %d", &n, &m);
for(int i=0; i<m; i++){
scanf("%d %d", &a, &b);
a--;
b--;
G[a].push_back(b);
}
memset(vis, 0, sizeof vis);
bool temCiclo = false;
for(int i = 0; i< n && !temCiclo; i++){
if(!vis[i]){
temCiclo = dfs(i);
}
}
if(temCiclo){
printf("SIM\n");
}
else printf("NAO\n");
for(int i=0; i<n;i++) G[i].clear();
}
return 0;
}