-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomeAndGo_UVA11838OJ.cpp
109 lines (91 loc) · 2.13 KB
/
comeAndGo_UVA11838OJ.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
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <stack>
#include <set>
#include <string>
#include <vector>
using namespace std;
bool first;
bool ans;
void tarjan(int u, int & index, vector<int> & dfn, vector<int> & low, vector<bool> & inStack, stack<int> & stk, vector<vector<int>> & g)
{
if (ans == 0) return;
++index;
dfn[u] = index; low[u] = index;
stk.push(u); inStack[u] = true;
int size0 = g[u].size();
for (int i = 0; i <= size0 - 1 && ans == 1; ++i)
{
int v = g[u][i];
if (dfn[v] == -1)
{
tarjan(v, index, dfn, low, inStack, stk, g);
low[u] = min(low[u], low[v]);
}
else if (inStack[v] == true)
{
low[u] = min(low[u], dfn[v]);
}
}
if (dfn[u] == low[u])
{
if (first == true)
{
first = false;
}
else
{
ans = 0;
}
int t;
do
{
t = stk.top();
inStack[t] = false;
stk.pop();
}
while (t != u);
}
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
while (true)
{
first = true;
ans = 1;
int n = 0, m = 0;
cin >> n >> m;
if (n + m == 0) break;
vector<vector<int>> g(n + 1);
for (int mC = 1; mC <= m; ++mC)
{
int u, v, p; cin >> u >> v >> p;
g[u].push_back(v);
if (p == 2)
{
g[v].push_back(u);
}
}
int index = 0;
vector<int> dfn(n + 1, -1), low(n + 1, -1);
vector<bool> inStack(n + 1, false);
stack<int> stk;
for (int i = 1; i <= n && ans == 1; ++i)
{
if (dfn[i] == -1)
{
tarjan(i, index, dfn, low, inStack, stk, g);
}
}
cout << ans << '\n';
}
cout.flush();
return 0;
}