結果

問題 No.1420 国勢調査 (Easy)
コンテスト
ユーザー touma6174
提出日時 2026-10-06 02:28:25
言語 PyPy3
(8.0.1 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 342 ms / 2,000 ms
+ 623µs
コード長 708 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 93 ms
コンパイル使用メモリ 64,508 KB
実行使用メモリ 91,700 KB
最終ジャッジ日時 2026-11-05 02:55:13
合計ジャッジ時間 10,956 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque

N, M = map(int, input().split())
ABY = [list(map(int, input().split())) + [int(input())] for _ in range(M)]

BY = [[] for _ in range(N)]
for a, b, y in ABY:
    BY[a-1].append((b-1, y))
    BY[b-1].append((a-1, y))

que = deque()
ans = [0] * N
seen = [False] * N

for i in range(N):
    if seen[i]:
        continue
    que.append(i)
    while que:
        n = que.popleft()
        for b, y in BY[n]:
            if seen[b]:
                if ans[n] ^ ans[b] != y:
                    print(-1)
                    exit()
            else:
                ans[b] = ans[n] ^ y
                seen[b] = True
                que.append(b)

print("\n".join(str(a) for a in ans))
0