forked from plpedrofeitosa/SolutionsUri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URI 1610.cpp
128 lines (102 loc) · 1.71 KB
/
URI 1610.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdio.h>
#include <vector>
using namespace std;
#define MAXV 100000 //quantidade máxima de vértices
#define BRANCO 0
#define CINZA 1
#define PRETO 2
#define NULO -1
int falha;
vector<int> l[MAXV + 1]; //lista de adjacências
int N; //quantidade de vértices
int c[MAXV+1]; //cor
int d[MAXV+1]; //tempo de descoberta
int t[MAXV+1]; //tempo de término
int a[MAXV+1]; //antecessor na árvore de busca
int tempo; //tempo corrente durante execução do algoritmo
void DFS_VISIT(int u)
{
c[u] = CINZA;
tempo++;
d[u] = tempo;
int i,v;
for(int v: l[u])
{ //percorre cada elemento da lista l[u]
if(c[v] == CINZA)
{
falha = 1;
}
if(c[v] == BRANCO)
{
a[v] = u;
DFS_VISIT(v);
}
}
c[u] = PRETO;
tempo++;
t[u] = tempo;
}
void DFS()
{
int u;
for(u = 1; u <= N; u++)
{
c[u] = BRANCO;
d[u] = NULO;
t[u] = NULO;
a[u] = NULO;
}
tempo=0;
for(u = 1; u <= N; u++)
{
if(c[u] == BRANCO)
DFS_VISIT(u);
}
}
void imprimir(int vet[])
{
int i;
for(i = 1; i <= N; i++)
{
printf("%d, ", vet[i]);
}
printf("\n");
}
void inicializar()
{
int u;
for(u = 0; u <= MAXV; u++)
{
l[u] = vector<int>(); //cria uma lista vazia para cada vértice u
}
}
int main()
{
int t;
int a, b, n,m;
int i, j;
scanf("%d", &t);
for(j = 0; j < t; j++)
{
inicializar();
scanf("%d %d", &n, &m);
falha = 0;
N = n;
for(i = 0; i < m; i++)
{
scanf("%d %d", &a, &b);
//l[u].pushback(v) torna v adjacente a u
l[b].push_back(a);
}
DFS();
if(falha)
{
printf("SIM\n");
}
else
{
printf("NAO\n");
}
}
return 0;
}