結果

問題 No.1420 国勢調査 (Easy)
ユーザー aaaaaaaaaa2230
提出日時 2021-03-06 10:30:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 403 ms / 2,000 ms
コード長 672 bytes
コンパイル時間 330 ms
コンパイル使用メモリ 82,272 KB
実行使用メモリ 94,848 KB
最終ジャッジ日時 2024-10-08 05:03:28
合計ジャッジ時間 10,797 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque

n,m = map(int,input().split())
e = [[] for i in range(n)]
for i in range(m):
    a,b = map(int,input().split())
    y = int(input())
    a -= 1
    b -= 1
    e[a].append((b,y))
    e[b].append((a,y))

ans = [0]*n
use = [0]*n

for i in range(n):
    if use[i]:
        continue
    q = deque([i])
    use[i] = 1
    while q:
        now = q.popleft()
        for nex,y in e[now]:
            if use[nex]:
                if ans[now]^ans[nex] != y:
                    print(-1)
                    exit()
                continue
            use[nex] = 1
            ans[nex] = y^ans[now]
            q.append(nex)
for i in ans:
    print(i)
0