結果

問題 No.1420 国勢調査 (Easy)
ユーザー m_tsubasam_tsubasa
提出日時 2021-03-05 22:10:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 284 ms / 2,000 ms
コード長 903 bytes
コンパイル時間 2,761 ms
コンパイル使用メモリ 205,356 KB
実行使用メモリ 9,600 KB
最終ジャッジ日時 2024-04-16 09:43:31
合計ジャッジ時間 9,067 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 121 ms
6,500 KB
testcase_03 AC 123 ms
6,444 KB
testcase_04 AC 120 ms
6,576 KB
testcase_05 AC 123 ms
6,452 KB
testcase_06 AC 125 ms
6,328 KB
testcase_07 AC 106 ms
6,588 KB
testcase_08 AC 108 ms
6,452 KB
testcase_09 AC 107 ms
6,476 KB
testcase_10 AC 107 ms
6,316 KB
testcase_11 AC 108 ms
6,580 KB
testcase_12 AC 17 ms
6,528 KB
testcase_13 AC 148 ms
6,528 KB
testcase_14 AC 18 ms
6,656 KB
testcase_15 AC 147 ms
6,656 KB
testcase_16 AC 147 ms
6,528 KB
testcase_17 AC 145 ms
6,528 KB
testcase_18 AC 146 ms
6,528 KB
testcase_19 AC 150 ms
6,656 KB
testcase_20 AC 146 ms
6,656 KB
testcase_21 AC 151 ms
6,528 KB
testcase_22 AC 275 ms
9,472 KB
testcase_23 AC 275 ms
9,472 KB
testcase_24 AC 281 ms
9,472 KB
testcase_25 AC 276 ms
9,472 KB
testcase_26 AC 284 ms
9,472 KB
testcase_27 AC 136 ms
9,600 KB
testcase_28 AC 134 ms
9,600 KB
testcase_29 AC 136 ms
9,472 KB
testcase_30 AC 137 ms
9,472 KB
testcase_31 AC 132 ms
9,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using P = pair<int, int>;

int n, m;
vector<vector<P>> g;

vector<int> solve();

int main() {
  cin >> n >> m;
  g.resize(n);
  for (int i = 0; i < m; ++i) {
    int a, b, x;
    cin >> a >> b >> x;
    g[--a].emplace_back(--b, x);
    g[b].emplace_back(a, x);
  }
  auto res = solve();
  if (res.size())
    for (auto p : res) cout << p << endl;
  else
    cout << -1 << endl;
  return 0;
}
vector<int> solve() {
  vector<int> res(n, -1);
  queue<int> qu;
  for (int i = 0; i < n; ++i)
    if (res[i] < 0) {
      res[i] = 0;
      qu.push(i);
      while (qu.size()) {
        int now = qu.front();
        qu.pop();
        for (auto [to, x] : g[now])
          if (res[to] < 0) {
            res[to] = x ^ res[now];
            qu.push(to);
          } else if (res[to] != (x ^ res[now]))
            return vector<int>();
      }
    }
  return res;
}
0