結果

問題 No.1420 国勢調査 (Easy)
ユーザー H3PO4H3PO4
提出日時 2022-05-12 10:27:10
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 381 ms / 2,000 ms
コード長 860 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 87,020 KB
実行使用メモリ 109,844 KB
最終ジャッジ日時 2023-09-27 08:54:53
合計ジャッジ時間 11,586 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,520 KB
testcase_01 AC 90 ms
71,628 KB
testcase_02 AC 247 ms
88,164 KB
testcase_03 AC 250 ms
88,148 KB
testcase_04 AC 256 ms
88,804 KB
testcase_05 AC 253 ms
89,076 KB
testcase_06 AC 254 ms
88,020 KB
testcase_07 AC 234 ms
87,832 KB
testcase_08 AC 236 ms
87,752 KB
testcase_09 AC 237 ms
87,748 KB
testcase_10 AC 246 ms
88,912 KB
testcase_11 AC 236 ms
88,616 KB
testcase_12 AC 194 ms
98,380 KB
testcase_13 AC 199 ms
98,584 KB
testcase_14 AC 182 ms
98,676 KB
testcase_15 AC 196 ms
98,652 KB
testcase_16 AC 203 ms
98,640 KB
testcase_17 AC 204 ms
98,796 KB
testcase_18 AC 208 ms
98,672 KB
testcase_19 AC 203 ms
98,760 KB
testcase_20 AC 200 ms
98,700 KB
testcase_21 AC 210 ms
98,672 KB
testcase_22 AC 378 ms
109,832 KB
testcase_23 AC 381 ms
109,636 KB
testcase_24 AC 369 ms
109,832 KB
testcase_25 AC 373 ms
109,844 KB
testcase_26 AC 374 ms
109,740 KB
testcase_27 AC 258 ms
107,996 KB
testcase_28 AC 257 ms
108,256 KB
testcase_29 AC 258 ms
107,784 KB
testcase_30 AC 264 ms
108,140 KB
testcase_31 AC 265 ms
108,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

input = sys.stdin.buffer.readline


def solve(G):
    X = [None] * N
    nonvisited = set(range(N))
    while nonvisited:
        v0 = nonvisited.pop()
        X[v0] = 0
        d = deque([v0])
        while d:
            v = d.pop()
            xv = X[v]
            for nv, y in G[v]:
                if nv in nonvisited:
                    X[nv] = xv ^ y
                    nonvisited.remove(nv)
                    d.append(nv)
                else:
                    if X[nv] != xv ^ y:
                        return None
    return X


N, M = map(int, input().split())

G = [[] for _ in range(N)]
for _ in range(M):
    a, b = (int(x) - 1 for x in input().split())
    y = int(input())
    G[a].append((b, y))
    G[b].append((a, y))

X = solve(G)
if X is None:
    print(-1)
else:
    print(*X, sep="\n")
0