結果

問題 No.1420 国勢調査 (Easy)
ユーザー Navier_Boltzmann
提出日時 2022-02-13 20:18:45
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,398 ms / 2,000 ms
コード長 840 bytes
コンパイル時間 145 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 58,836 KB
最終ジャッジ日時 2024-06-29 05:46:52
合計ジャッジ時間 26,179 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M = map(int,input().split())
e = [[] for _ in range(N)]
from collections import defaultdict
d = defaultdict(lambda:-1)
for _ in range(M):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    y = int(input())
    if d[(a,b)] != -1:
        if d[(a,b)] != y:
            print(-1)
            exit()
    d[(a,b)] = y
    d[(b,a)] = y
    e[a].append(b)
    e[b].append(a)
    
X = [-1]*N
from collections import deque
v = deque()
for i in range(N):
    if X[i] != -1:
        continue
    X[i] = 0
    v.append(i)
    
    while v:
        x = v.popleft()
        
        for ix in e[x]:
            if X[ix] == -1:
                X[ix] = X[x]^d[(x,ix)]
                v.append(ix)
            if X[ix] != -1:
                if X[ix] != X[x]^d[(x,ix)]:
                    print(-1)
                    exit()
print(*X,sep='\n')
0