結果

問題 No.1420 国勢調査 (Easy)
ユーザー nikkukunnikkukun
提出日時 2021-03-05 23:16:58
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 207 ms / 2,000 ms
コード長 1,600 bytes
コンパイル時間 1,887 ms
コンパイル使用メモリ 173,964 KB
実行使用メモリ 10,112 KB
最終ジャッジ日時 2024-04-16 11:14:37
合計ジャッジ時間 7,410 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,272 KB
testcase_01 AC 4 ms
6,144 KB
testcase_02 AC 54 ms
8,184 KB
testcase_03 AC 54 ms
8,056 KB
testcase_04 AC 54 ms
7,924 KB
testcase_05 AC 55 ms
8,052 KB
testcase_06 AC 55 ms
8,188 KB
testcase_07 AC 42 ms
7,928 KB
testcase_08 AC 42 ms
8,052 KB
testcase_09 AC 41 ms
7,884 KB
testcase_10 AC 42 ms
7,980 KB
testcase_11 AC 42 ms
8,052 KB
testcase_12 AC 12 ms
7,424 KB
testcase_13 AC 137 ms
7,296 KB
testcase_14 AC 11 ms
7,296 KB
testcase_15 AC 138 ms
7,168 KB
testcase_16 AC 138 ms
7,240 KB
testcase_17 AC 137 ms
7,364 KB
testcase_18 AC 136 ms
7,168 KB
testcase_19 AC 139 ms
7,168 KB
testcase_20 AC 137 ms
7,168 KB
testcase_21 AC 138 ms
7,168 KB
testcase_22 AC 207 ms
9,984 KB
testcase_23 AC 206 ms
9,884 KB
testcase_24 AC 205 ms
10,112 KB
testcase_25 AC 204 ms
9,984 KB
testcase_26 AC 207 ms
9,856 KB
testcase_27 AC 81 ms
9,856 KB
testcase_28 AC 78 ms
9,984 KB
testcase_29 AC 75 ms
9,884 KB
testcase_30 AC 77 ms
10,088 KB
testcase_31 AC 78 ms
9,984 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//
#include <bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define lch (o << 1)
#define rch (o << 1 | 1)

typedef double db;
typedef long long ll;
typedef unsigned int ui;
typedef pair<int, int> pint;
typedef tuple<int, int, int> tint;

const int N = 1e5 + 5;
const int K = 20 + 2;
const int INF = 0x3f3f3f3f;
const ll INF_LL = 0x3f3f3f3f3f3f3f3f;

int dis[N];

struct Adj {
	int v, w;
};
vector<Adj> a[N];
vector<tint> res;

struct DSU {
	int pa[N];
	DSU() {
		for (int i = 1; i < N; i++)
			pa[i] = i;
	}
	int find(int x) {
		return pa[x] == x ? x : pa[x] = find(pa[x]);
	}
	bool isConnected(int u, int v) {
		return find(u) == find(v);
	}
	void merge(int u, int v) {
		int pa1 = find(u);
		int pa2 = find(v);
		pa[pa1] = pa2;
	}
};
DSU dsu;

bool vst[N];
int f[N];

void dfs(int u, int pa) {
	vst[u] = 1;
	for (auto e: a[u]) {
		if (e.v == pa) continue;
		f[e.v] = f[u] ^ e.w;
		dfs(e.v, u);
	}
}

int main() {
	ios::sync_with_stdio(0);

	int n, m;
	cin >> n >> m;
	for (int i = 1; i <= m; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		if (dsu.isConnected(u, v)) {
			res.push_back(make_tuple(u, v, w));
		} else {
			a[u].push_back({v, w});
			a[v].push_back({u, w});
			dsu.merge(u, v);
		}
	}

	for (int i = 1; i <= n; i++)
		if (!vst[i])
			dfs(i, 0);

	bool flag = true;
	for (auto t: res) {
		int u = get<0>(t);
		int v = get<1>(t);
		int w = get<2>(t);
		int val = f[u] ^ f[v];
		if (val != w) flag = false;
	}

	if (flag) {
		for (int i = 1; i <= n; i++)
			cout << f[i] << endl;
	} else {
		cout << -1 << endl;
	}

	return 0;
}
0