結果

問題 No.1420 国勢調査 (Easy)
ユーザー H3PO4H3PO4
提出日時 2022-05-12 09:29:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 451 ms / 2,000 ms
コード長 790 bytes
コンパイル時間 636 ms
コンパイル使用メモリ 87,080 KB
実行使用メモリ 111,920 KB
最終ジャッジ日時 2023-09-27 07:41:45
合計ジャッジ時間 15,264 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,684 KB
testcase_01 AC 90 ms
71,616 KB
testcase_02 AC 322 ms
89,104 KB
testcase_03 AC 315 ms
89,296 KB
testcase_04 AC 322 ms
89,556 KB
testcase_05 AC 313 ms
89,060 KB
testcase_06 AC 321 ms
89,060 KB
testcase_07 AC 305 ms
89,760 KB
testcase_08 AC 312 ms
89,724 KB
testcase_09 AC 301 ms
89,844 KB
testcase_10 AC 313 ms
89,712 KB
testcase_11 AC 304 ms
88,916 KB
testcase_12 AC 220 ms
100,796 KB
testcase_13 AC 231 ms
100,832 KB
testcase_14 AC 218 ms
101,024 KB
testcase_15 AC 232 ms
100,860 KB
testcase_16 AC 234 ms
101,160 KB
testcase_17 AC 235 ms
100,836 KB
testcase_18 AC 237 ms
101,040 KB
testcase_19 AC 228 ms
100,768 KB
testcase_20 AC 230 ms
100,848 KB
testcase_21 AC 239 ms
100,908 KB
testcase_22 AC 446 ms
111,920 KB
testcase_23 AC 447 ms
111,884 KB
testcase_24 AC 439 ms
111,904 KB
testcase_25 AC 435 ms
111,876 KB
testcase_26 AC 451 ms
111,724 KB
testcase_27 AC 333 ms
110,556 KB
testcase_28 AC 333 ms
110,412 KB
testcase_29 AC 337 ms
110,384 KB
testcase_30 AC 335 ms
110,648 KB
testcase_31 AC 345 ms
110,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


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()
            for x, y in G[v]:
                if x in nonvisited:
                    X[x] = X[v] ^ y
                    nonvisited.remove(x)
                    d.append(x)
                else:
                    if X[x] != X[v] ^ 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