結果

問題 No.1420 国勢調査 (Easy)
ユーザー SSRSSSRS
提出日時 2021-03-05 21:54:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 272 ms / 2,000 ms
コード長 899 bytes
コンパイル時間 1,832 ms
コンパイル使用メモリ 173,400 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2024-04-16 09:21:38
合計ジャッジ時間 8,547 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 122 ms
6,448 KB
testcase_03 AC 118 ms
6,572 KB
testcase_04 AC 119 ms
6,324 KB
testcase_05 AC 119 ms
6,328 KB
testcase_06 AC 123 ms
6,456 KB
testcase_07 AC 107 ms
6,468 KB
testcase_08 AC 105 ms
6,456 KB
testcase_09 AC 106 ms
6,444 KB
testcase_10 AC 107 ms
6,584 KB
testcase_11 AC 109 ms
6,456 KB
testcase_12 AC 20 ms
6,400 KB
testcase_13 AC 147 ms
6,656 KB
testcase_14 AC 22 ms
6,528 KB
testcase_15 AC 151 ms
6,528 KB
testcase_16 AC 149 ms
6,656 KB
testcase_17 AC 149 ms
6,400 KB
testcase_18 AC 145 ms
6,656 KB
testcase_19 AC 147 ms
6,528 KB
testcase_20 AC 149 ms
6,528 KB
testcase_21 AC 151 ms
6,656 KB
testcase_22 AC 267 ms
9,504 KB
testcase_23 AC 269 ms
9,472 KB
testcase_24 AC 272 ms
9,600 KB
testcase_25 AC 266 ms
9,472 KB
testcase_26 AC 270 ms
9,600 KB
testcase_27 AC 140 ms
9,600 KB
testcase_28 AC 140 ms
9,600 KB
testcase_29 AC 138 ms
9,344 KB
testcase_30 AC 137 ms
9,472 KB
testcase_31 AC 139 ms
9,600 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
int main(){
  int N, M;
  cin >> N >> M;
  vector<vector<pair<int, int>>> E(N);
  for (int i = 0; i < M; i++){
    int A, B, Y;
    cin >> A >> B >> Y;
    A--;
    B--;
    E[A].push_back(make_pair(Y, B));
    E[B].push_back(make_pair(Y, A));
  }
  bool ok = true;
  vector<int> X(N, -1);
  for (int i = 0; i < N; i++){
    if (X[i] == -1){
      X[i] = 0;
      queue<int> Q;
      Q.push(i);
      while (!Q.empty()){
        int v = Q.front();
        Q.pop();
        for (auto P : E[v]){
          int Y = P.first;
          int w = P.second;
          if (X[w] == -1){
            X[w] = X[v] ^ Y;
            Q.push(w);
          } else if (X[w] != (X[v] ^ Y)){
            ok = false;
          }
        }
      }
    }
  }
  if (!ok){
    cout << -1 << endl;
  } else {
    for (int i = 0; i < N; i++){
      cout << X[i] << endl;
    }
  }
}
0