标签:别人 get c++ turn code http target man ons
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int T, n, m;
int f[maxn << 1], ans[maxn], sz[maxn << 1];
vector<int> v[maxn << 1];
int getf(int v) {
return v == f[v] ? v : f[v] = getf(f[v]);
}
void merge(int u, int v) {
f[getf(u)] = getf(v);
}
int main() {
for (scanf("%d", &T); T--;) {
scanf("%d %d", &n, &m);
for (int i = 1; i <= n * 2; i++) f[i] = i, v[i].clear();
memset(ans, 0, sizeof ans);
memset(sz, 0, sizeof sz);
for (int i = 1, u, v, w; i <= m; i++) {
scanf("%d %d %d", &u, &v, &w);
if (w) merge(u, v), merge(u + n, v + n);
else merge(u, v + n), merge(u + n, v);
}
int flag = 1;
for (int i = 1; i <= n; i++)
if (getf(i) == getf(i + n)) {
flag = 0; break;
}
if (!flag) {
puts("-1"); continue;
}
for (int i = 1; i <= n * 2; i++) v[getf(i)].push_back(i);
for (int i = 1; i <= n; i++) sz[getf(i)]++;
for (int i = 1; i <= n; i++) {
if (ans[i]) continue;
int fa = getf(i);
if (v[fa].size() - sz[fa] >= sz[fa]) {
for (int j : v[fa])
if (j > n) ans[j - n] = 2;
else ans[j] = 1;
} else {
for (int j : v[fa])
if (j > n) ans[j - n] = 1;
else ans[j] = 2;
}
}
for (int i = 1; i <= n; i++) printf("%d", ans[i] - 1);
puts("");
}
}
标签:别人 get c++ turn code http target man ons
原文地址:https://www.cnblogs.com/AlphaWA/p/10901661.html