結果

問題 No.1420 国勢調査 (Easy)
コンテスト
ユーザー gomaazarasi
提出日時 2026-10-06 16:32:16
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 332 ms / 2,000 ms
+ 237µs
コード長 677 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 434 ms
コンパイル使用メモリ 81,280 KB
実行使用メモリ 116,676 KB
最終ジャッジ日時 2026-10-06 16:32:31
合計ジャッジ時間 11,966 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque

n,m = list(map(int,input().split()))
uv = [[] for i in range(n)]

for i in range(m):
    u,v = list(map(int,input().split()))
    u,v = u-1,v-1
    x = int(input())
    
    uv[u].append([v,x])
    uv[v].append([u,x])

ans = [0 for i in range(n)]

for i in range(n):
    if ans[i]:
        continue
    
    d = deque([i])
    ans[i] = 1
    
    while len(d):
        u = d.popleft()
        
        for v,x in uv[u]:
            if ans[v]:
                if ans[u] ^ ans[v] != x:
                    print(-1)
                    exit()
            else:
                ans[v] = ans[u] ^ x
                d.append(v)

print(*ans,sep="\n")

0