結果
| 問題 | No.1420 国勢調査 (Easy) | 
| コンテスト | |
| ユーザー |  titia | 
| 提出日時 | 2021-03-05 22:16:07 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 262 ms / 2,000 ms | 
| コード長 | 699 bytes | 
| コンパイル時間 | 263 ms | 
| コンパイル使用メモリ | 82,384 KB | 
| 実行使用メモリ | 97,408 KB | 
| 最終ジャッジ日時 | 2024-10-07 02:28:13 | 
| 合計ジャッジ時間 | 7,414 ms | 
| ジャッジサーバーID (参考情報) | judge3 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 30 | 
ソースコード
import sys
input = sys.stdin.readline
N,M=map(int,input().split())
E=[[] for i in range(N+1)]
for i in range(M):
    a,b=map(int,input().split())
    c=int(input())
    E[a].append((b,c))
    E[b].append((a,c))
ANS=[-1]*(N+1)
for i in range(1,N+1):
    if ANS[i]==-1:
        Q=[i]
        ANS[i]=0
        
        while Q:
            x=Q.pop()
            for to,xor in E[x]:
                if ANS[to]==-1:
                    ANS[to]=ANS[x]^xor
                    Q.append(to)
                else:
                    if ANS[to]^ANS[x]!=xor:
                        print(-1)
                        sys.exit()
for i in range(1,N+1):
    if ANS[i]==-1:
        ANS[i]=0
print(*ANS[1:])
            
            
            
        