結果

問題 No.1420 国勢調査 (Easy)
ユーザー m_tsubasam_tsubasa
提出日時 2021-03-05 22:10:43
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 903 bytes
コンパイル時間 2,730 ms
コンパイル使用メモリ 203,900 KB
最終ジャッジ日時 2025-01-19 11:13:32
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

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