結果

問題 No.1420 国勢調査 (Easy)
ユーザー 👑 NachiaNachia
提出日時 2021-03-05 23:09:55
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 20 ms / 2,000 ms
コード長 2,205 bytes
コンパイル時間 2,716 ms
コンパイル使用メモリ 210,120 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-16 11:08:37
合計ジャッジ時間 5,425 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 9 ms
5,376 KB
testcase_03 AC 9 ms
5,376 KB
testcase_04 AC 9 ms
5,376 KB
testcase_05 AC 9 ms
5,376 KB
testcase_06 AC 9 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 8 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 8 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 5 ms
5,376 KB
testcase_16 AC 5 ms
5,376 KB
testcase_17 AC 5 ms
5,376 KB
testcase_18 AC 5 ms
5,376 KB
testcase_19 AC 5 ms
5,376 KB
testcase_20 AC 5 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 20 ms
5,376 KB
testcase_23 AC 20 ms
5,376 KB
testcase_24 AC 20 ms
5,376 KB
testcase_25 AC 20 ms
5,376 KB
testcase_26 AC 19 ms
5,376 KB
testcase_27 AC 7 ms
5,376 KB
testcase_28 AC 6 ms
5,376 KB
testcase_29 AC 5 ms
5,376 KB
testcase_30 AC 7 ms
5,376 KB
testcase_31 AC 6 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// WHY IS OFAST SLOWER !!!
#pragma GCC optimize ("O3")

#include<bits/stdc++.h>
using namespace std;
inline int my_getchar(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(int &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
inline void my_putchar(const int k){
  putchar(k);
}
inline void wt_L(char a){
  my_putchar(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar('-');
  }
  while(s--){
    my_putchar(f[s]+'0');
  }
}
vector<int> V(100000,-1);
vector<int> X(100000,0);
int root(int a){
  if(V[a]<0){
    return a;
  }
  int p=V[a];
  int r=root(p);
  V[a]=r;
  X[a]^=X[p];
  return r;
}
int merge(int r,int c,int y){
  int rr=root(r);
  int rc=root(c);
  if(rr==rc){
    return X[r]^X[c]^y;
  }
  V[rc]=r;
  X[rc]=y^X[c];
  return 0;
}
int main(){
  int PiIOrLma, i;
  int N;
  rd(N);
  int M;
  rd(M);
  int AuJVIghY = M;
  for(PiIOrLma=(0);PiIOrLma<(AuJVIghY);PiIOrLma++){
    int a;
    rd(a);
    int b;
    rd(b);
    int y;
    rd(y);
    if(merge(a-1,b-1,y)){
      printf("-1\n");
      return 0;
    }
  }
  for(i=(0);i<(N);i++){
    root(i);
    wt_L(X[i]);
    wt_L('\n');
  }
  return 0;
}
// cLay varsion 20201003-1

// --- original code ---
// 
// VI V(1d5,-1),X(1d5,0);
// 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 main(){
// 	int @N,@M;
// 	REP(M){
// 		int @a,@b,@y;
// 		if(merge(a-1,b-1,y)) printf("-1\n"), return 0;
// 	}
// 	rep(i,N) root(i),wt(X[i]);
// 	return 0;
// }
0