-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP11838.cpp
executable file
·74 lines (70 loc) · 1.47 KB
/
P11838.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
struct Node {
vector<int> in, out;
bool visited;
void reset() {
in.clear();
out.clear();
visited = false;
}
};
Node nodes[2001];
int dfs(int i) {
nodes[i].visited = true;
int ret = 1;
FORIT(vector<int>, nodes[i].out) {
if(nodes[*it].visited)
continue;
ret += dfs(*it);
}
return ret;
}
int main() {
int N, M, a, b, c;
while(true) {
cin >> N >> M; // N nodes, M connections.
if(N == 0 && M == 0)
return 0;
FORI(N)
nodes[i].reset();
// Build graph:
FORI(M) {
cin >> a >> b >> c;
--a;
--b; // 0-index
nodes[a].out.push_back(b);
nodes[b].in.push_back(a);
if(c == 2) {
nodes[b].out.push_back(a);
nodes[a].in.push_back(b);
}
}
// DFS from first node:
int cntReachableFromFirst = dfs(0);
//cerr << "N=" << N << endl;
//cerr << "cntReachableFromFirst = " << cntReachableFromFirst << endl;
if(cntReachableFromFirst != N) {
cout << 0 << endl;
continue;
}
stack<int> unhandledQ; // clique with first node.
unhandledQ.push(0);
int sizeQ = 0;
while(!unhandledQ.empty()) {
Node &n = nodes[unhandledQ.top()];
unhandledQ.pop();
if(!n.visited)
continue;
++sizeQ;
n.visited = false;
FORIT(vector<int>, n.in) {
if(!nodes[*it].visited)
continue;
unhandledQ.push(*it);
} // FORIT
} // while unhandled
if(sizeQ == N)
cout << 1 << endl;
else
cout << 0 << endl;
}
}