結果

問題 No.1420 国勢調査 (Easy)
コンテスト
ユーザー Kohei
提出日時 2026-08-09 17:40:40
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 264 ms / 2,000 ms
+ 260µs
コード長 713 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 238 ms
コンパイル使用メモリ 95,856 KB
実行使用メモリ 104,832 KB
最終ジャッジ日時 2026-08-09 17:40:54
合計ジャッジ時間 9,746 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque
N, M = map(int, input().split())
G = [list() for _ in range(N)]
ans = [-1]*N
for i in range(M):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    y = int(input())
    G[a].append((b, y))
    G[b].append((a, y))


for i in range(N):
    if ans[i] != -1:
        continue
    ans[i] = 0
    que = deque([i])
    while que:
        pos = que.popleft()
        for nex, xor in G[pos]:
            if ans[nex] == -1:
                ans[nex] = ans[pos]^xor
                que.append(nex)
            else: # 訪問済み
                if ans[nex]^ans[pos] == xor:
                    continue
                else:
                    exit(print(-1))
print(*ans, sep='\n')
0