题目描述
题目链接
方法 BFS + 位运算
#include<bits/stdc++.h>
#define pb push_back
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
const int N = 2e5 + 10;
vector<PII> g[N];
bool visited[N];
LL val[N];
LL ans[N];
vector<int> BFS(int start) {
queue<int> q;
q.push(start);
visited[start] = true;
vector<int> a = {start};
val[start] = 0;
while(! q.empty()) {
int v = q.front();
q.pop();
for (auto [u, w] : g[v]) {
if (! visited[u]) {
visited[u] = true;
val[u] = val[v] ^ w;
a.pb(u);
q.push(u);
} else {
if (val[u] != (val[v] ^ w)) {
cout << -1 << '\n';
exit(0);
}
}
}
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
memset(val, -1, sizeof(val));
for (int i = 0; i < m; i ++) {
int x, y, z;
cin >> x >> y >> z;
x--, y--;
g[x].pb({y, z});
g[y].pb({x, z});
}
for (int st = 0; st < n; st ++) {
if (visited[st]) continue;
vector<int> a = BFS(st);
for (int i = 0; i < 30; i ++) {
int cnt = 0;
LL mask = (LL) 1 << i;
for (int j : a) {
if (val[j] >> i & 1) cnt ++;
}
if (cnt < a.size() - cnt) {
for (int j : a) {
if (val[j] >> i & 1) {
ans[j] |= mask;
}
}
} else {
for (int j : a) {
if (! (val[j] >> i & 1)) {
ans[j] |= mask;
}
}
}
}
}
for (int i = 0; i < n; i ++) {
cout << ans[i] << " ";
}
cout << '\n';
return 0;
}