-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreview.cpp
89 lines (74 loc) · 1.82 KB
/
review.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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using vpi = vector<pi>;
using vb = vector<bool>;
void solve() {
ll n, m;
cin >> m >> n;
vector<set<pi>> ed(n);
for (int i = 0; i < m; i++) {
ll a, b, c;
cin >> a >> b >> c;
a--, b--, c--;
ed[a].insert({b, c});
ed[b].insert({a, c});
}
vb vis(n);
vi col(n, -1);
stack<ll> st;
bool works = true;
bool fail = false;
function<void(int)> dfs = [&](int v) {
if (vis[v]) return;
vis[v] = true;
for (pi ch : ed[v]) {
if (ch.second != col[v]) {
if (col[ch.first] == -1) {
st.push(ch.first);
col[ch.first] = col[v] ^ 1;
dfs(ch.first);
}
if (col[ch.first] != (col[v] ^ 1)) {
works = false;
return;
}
}
}
};
for (int i = 0; i < n; i++) {
if (col[i] == -1) {
st.push(i);
col[i] = 0;
dfs(i);
if (works)
continue;
else {
works = true;
while (st.size() > 0) {
ll th = st.top();
st.pop();
col[th] = -1;
vis[th] = false;
}
col[i] = 1;
dfs(i);
if (!works) {
fail = true;
break;
}
}
}
}
cout << (!fail ? "We can do it!" : "Better luck next time.") << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll t;
cin >> t;
while (t--) solve();
return 0;
}