-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
34 lines (32 loc) · 779 Bytes
/
Copy pathmain.cpp
File metadata and controls
34 lines (32 loc) · 779 Bytes
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
#include <bits/stdc++.h>
using namespace std;
auto sol = [](int n, int m, auto adj) {
if (n % 2 == 1) return -1;
vector d(n + 1, -1);
auto rec = [&](const auto& self, int cur) -> void {
for (auto [nxt, cost] : adj[cur]) {
if (d[nxt] != -1) continue;
d[nxt] = d[cur] ^ cost;
self(self, nxt);
}
};
d[1] = 0;
rec(rec, 1);
int ret = 0;
for (int i = 1; i <= n; i++) ret ^= d[i] ^ i;
return ret;
};
int main() {
cin.tie(0)->sync_with_stdio(0);
int tc; cin >> tc;
while (tc--) {
int n, m; cin >> n >> m;
vector adj(n + 1, vector(0, pair(0, 0)));
for (int i = 0; i < m; i++) {
int a, b, c; cin >> a >> b >> c;
adj[a].push_back(pair(b, c));
adj[b].push_back(pair(a, c));
}
cout << sol(n, m, adj) << '\n';
}
}