結果

問題 No.1420 国勢調査 (Easy)
ユーザー 👑 NachiaNachia
提出日時 2021-03-05 22:59:16
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 531 bytes
コンパイル時間 2,216 ms
コンパイル使用メモリ 195,432 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 10:58:46
合計ジャッジ時間 6,069 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 93 ms
5,376 KB
testcase_03 AC 93 ms
5,376 KB
testcase_04 AC 96 ms
5,376 KB
testcase_05 AC 95 ms
5,376 KB
testcase_06 AC 96 ms
5,376 KB
testcase_07 AC 27 ms
5,376 KB
testcase_08 AC 64 ms
5,376 KB
testcase_09 AC 34 ms
5,376 KB
testcase_10 AC 68 ms
5,376 KB
testcase_11 AC 15 ms
5,376 KB
testcase_12 AC 8 ms
5,376 KB
testcase_13 AC 22 ms
5,376 KB
testcase_14 AC 11 ms
5,376 KB
testcase_15 AC 22 ms
5,376 KB
testcase_16 AC 22 ms
5,376 KB
testcase_17 AC 22 ms
5,376 KB
testcase_18 AC 22 ms
5,376 KB
testcase_19 AC 21 ms
5,376 KB
testcase_20 AC 21 ms
5,376 KB
testcase_21 AC 21 ms
5,376 KB
testcase_22 AC 114 ms
5,376 KB
testcase_23 AC 115 ms
5,376 KB
testcase_24 AC 116 ms
5,376 KB
testcase_25 AC 116 ms
5,376 KB
testcase_26 AC 114 ms
5,376 KB
testcase_27 AC 50 ms
5,376 KB
testcase_28 AC 49 ms
5,376 KB
testcase_29 AC 26 ms
5,376 KB
testcase_30 AC 51 ms
5,376 KB
testcase_31 AC 48 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

vector<int> V,X;
int root(int a){
	if(V[a]<0) return a;
	int p=V[a], r=root(p);
	V[a]=r; X[a]^=X[p];
	return r;
}
int merge(int r,int c,int y){
	int rr=root(r), rc=root(c);
	if(rr==rc) return X[r]^X[c]^y;
	V[rc]=r; X[rc]=y^X[c];
	return 0;
}

int N,M;

int main(){
	cin>>N>>M;
	V.assign(N,-1);
	X.assign(N,0);
	for(int i=0; i<M; i++){
		int a,b,y; cin>>a>>b>>y;
		if(merge(a-1,b-1,y)){ printf("-1\n"); return 0; }
	}
	for(int i=0; i<N; i++){ root(i); cout<<X[i]<<"\n"; }
	return 0;
}
0