結果

問題 No.1420 国勢調査 (Easy)
ユーザー NachiaNachia
提出日時 2021-03-05 21:30:57
言語 C++17
(gcc 12.2.0 + boost 1.81.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 830 bytes
コンパイル時間 1,957 ms
使用メモリ 3,972 KB
最終ジャッジ日時 2022-11-29 13:36:45
合計ジャッジ時間 7,118 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 1 ms
3,572 KB
testcase_01 AC 2 ms
3,600 KB
testcase_02 AC 31 ms
3,648 KB
testcase_03 AC 30 ms
3,648 KB
testcase_04 AC 31 ms
3,652 KB
testcase_05 AC 29 ms
3,740 KB
testcase_06 AC 30 ms
3,708 KB
testcase_07 AC 10 ms
3,676 KB
testcase_08 AC 21 ms
3,620 KB
testcase_09 AC 11 ms
3,668 KB
testcase_10 AC 22 ms
3,588 KB
testcase_11 AC 7 ms
3,596 KB
testcase_12 AC 4 ms
3,824 KB
testcase_13 AC 13 ms
3,892 KB
testcase_14 AC 5 ms
3,892 KB
testcase_15 AC 13 ms
3,920 KB
testcase_16 AC 13 ms
3,824 KB
testcase_17 AC 12 ms
3,832 KB
testcase_18 AC 13 ms
3,896 KB
testcase_19 AC 13 ms
3,936 KB
testcase_20 AC 13 ms
3,960 KB
testcase_21 AC 13 ms
3,972 KB
testcase_22 AC 46 ms
3,828 KB
testcase_23 AC 46 ms
3,888 KB
testcase_24 AC 46 ms
3,936 KB
testcase_25 AC 45 ms
3,940 KB
testcase_26 AC 46 ms
3,896 KB
testcase_27 AC 17 ms
3,892 KB
testcase_28 AC 17 ms
3,956 KB
testcase_29 AC 10 ms
3,956 KB
testcase_30 AC 18 ms
3,956 KB
testcase_31 AC 16 ms
3,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

struct dsu{
  int N;
  vector<int> V;
  vector<int> X;
  dsu(int n=0){
    N=n;
    V.resize(N); rep(i,N) V[i]=i;
    X.assign(N,0);
  }
  int root(int a){
    if(V[a]==a) return a;
    int p=V[a];
    int r=root(V[a]);
    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){ if((X[r]^X[c])!=y) return -1; else return 0; }
    V[rc]=r; X[rc]=y^X[c];
    return 0;
  }
};

int N,M;
dsu G;

int main(){
  scanf("%d%d",&N,&M);
  G=dsu(N);
  rep(i,M){
    int a,b,y; scanf("%d%d%d",&a,&b,&y); a--; b--;
    if(G.merge(a,b,y)){ printf("-1\n"); return 0; }
  }
  rep(i,N) G.root(i);
  rep(i,N) printf("%d\n",G.X[i]);
  return 0;
}
0