結果

問題 No.1420 国勢調査 (Easy)
ユーザー 👑 NachiaNachia
提出日時 2021-03-05 21:30:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 830 bytes
コンパイル時間 2,156 ms
コンパイル使用メモリ 203,204 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-07-29 01:03:09
合計ジャッジ時間 8,859 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 32 ms
4,380 KB
testcase_03 AC 32 ms
4,376 KB
testcase_04 AC 32 ms
4,380 KB
testcase_05 AC 32 ms
4,376 KB
testcase_06 AC 32 ms
4,376 KB
testcase_07 AC 10 ms
4,376 KB
testcase_08 AC 22 ms
4,376 KB
testcase_09 AC 12 ms
4,380 KB
testcase_10 AC 23 ms
4,376 KB
testcase_11 AC 6 ms
4,380 KB
testcase_12 AC 4 ms
4,376 KB
testcase_13 AC 13 ms
4,384 KB
testcase_14 AC 5 ms
4,376 KB
testcase_15 AC 13 ms
4,380 KB
testcase_16 AC 13 ms
4,380 KB
testcase_17 AC 14 ms
4,376 KB
testcase_18 AC 13 ms
4,376 KB
testcase_19 AC 13 ms
4,376 KB
testcase_20 AC 13 ms
4,376 KB
testcase_21 AC 14 ms
4,376 KB
testcase_22 AC 47 ms
4,376 KB
testcase_23 AC 48 ms
4,380 KB
testcase_24 AC 49 ms
4,380 KB
testcase_25 AC 48 ms
4,376 KB
testcase_26 AC 48 ms
4,380 KB
testcase_27 AC 18 ms
4,380 KB
testcase_28 AC 18 ms
4,380 KB
testcase_29 AC 10 ms
4,380 KB
testcase_30 AC 19 ms
4,380 KB
testcase_31 AC 17 ms
4,380 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