結果
| 問題 | No.1420 国勢調査 (Easy) | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2022-02-24 22:08:04 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,571 ms / 2,000 ms | 
| コード長 | 889 bytes | 
| コンパイル時間 | 200 ms | 
| コンパイル使用メモリ | 82,432 KB | 
| 実行使用メモリ | 105,396 KB | 
| 最終ジャッジ日時 | 2024-07-02 14:06:37 | 
| 合計ジャッジ時間 | 17,385 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 30 | 
ソースコード
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
edges = [[] for _ in range(n)]
for _ in range(m):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    y = int(input())
    edges[a].append((b, y))
    edges[b].append((a, y))
    
X = [0] * n
for k in range(30):
    bi = [-1] * n
    for i in range(n):
        if bi[i] != -1:
            continue
        bi[i] = 0
        stack = [i]
        while stack:
            pos = stack.pop()
            for npos, y in edges[pos]:
                if y >> k & 1:
                    x = 1
                else:
                    x = 0
                if bi[npos] == -1:
                    bi[npos] = bi[pos] ^ x
                    X[npos] ^= bi[npos] << k
                    stack.append(npos)
                elif bi[npos] != bi[pos] ^ x:
                    print(-1)
                    exit()
print(*X)
            
            
            
        