-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1128 - Come and Go.cpp
63 lines (53 loc) · 1.07 KB
/
1128 - Come and Go.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
#include <stdio.h>
#include <vector>
#include <memory.h>
#include <algorithm>
using namespace std;
int n, m, low[2001], idx[2001], id, dfs;
bool in[2001];
vector<vector<int> > g;
vector<int> st;
void DFS(int node) {
low[node] = idx[node] = dfs++;
in[node] = true;
st.push_back(node);
for(int i = 0; i < (int)g[node].size(); ++i)
if(idx[g[node][i]] == -1) {
DFS(g[node][i]);
low[node] = min(low[node], low[g[node][i]]);
} else if(in[g[node][i]])
low[node] = min(low[node], low[g[node][i]]);
if(low[node] == idx[node]) {
int cur;
do {
cur = st.back();
st.pop_back();
in[cur] = false;
} while(cur != node);
++id;
}
}
int main() {
while(scanf("%d %d", &n, &m) && n != 0 && m != 0) {
g.clear();
g.resize(n);
for(int i = 0, a, b, c; i < m; ++i) {
scanf("%d %d %d", &a, &b, &c);
--a, --b;
g[a].push_back(b);
if(c == 2)
g[b].push_back(a);
}
memset(idx, -1, sizeof idx);
st.clear();
id = dfs = 0;
for(int i = 0; i < n; ++i)
if(idx[i] == -1)
DFS(i);
if(id == 1)
puts("1");
else
puts("0");
}
return 0;
}