結果

問題 No.1420 国勢調査 (Easy)
ユーザー c-yanc-yan
提出日時 2021-03-06 11:26:54
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 1,044 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 44,212 KB
最終ジャッジ日時 2024-04-16 23:34:44
合計ジャッジ時間 17,032 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
10,880 KB
testcase_01 AC 31 ms
10,880 KB
testcase_02 AC 577 ms
30,464 KB
testcase_03 AC 573 ms
30,592 KB
testcase_04 AC 578 ms
30,464 KB
testcase_05 AC 575 ms
30,460 KB
testcase_06 AC 571 ms
30,592 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 58 ms
14,720 KB
testcase_13 RE -
testcase_14 AC 69 ms
16,200 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 AC 276 ms
32,436 KB
testcase_28 RE -
testcase_29 AC 148 ms
23,888 KB
testcase_30 AC 395 ms
37,176 KB
testcase_31 AC 375 ms
36,632 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import setrecursionlimit, stdin
from collections import deque


def find(parent, i):
    t = parent[i]
    if t < 0:
        return i
    t = find(parent, t)
    parent[i] = t
    return t


def unite(parent, i, j):
    i = find(parent, i)
    j = find(parent, j)
    if i == j:
        return
    parent[j] += parent[i]
    parent[i] = j


readline = stdin.readline
setrecursionlimit(10 ** 6)

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

parent = [-1] * N
d = {}
for _ in range(M):
    A, B = map(lambda x: int(x) - 1, readline().split())
    Y = int(readline())
    if A in d and B in d[A]:
        if d[A][B] != Y:
            print(-1)
            exit()
    d.setdefault(A, {})
    d.setdefault(B, {})
    d[A][B] = Y
    d[B][A] = Y
    unite(parent, A, B)

result = [-1] * N
q = deque(i for i in range(N) if parent[i] < 0)
for i in q:
    result[i] = 0
while q:
    a = q.popleft()
    for b in d[a]:
        if result[b] != -1:
            continue
        result[b] = result[a] ^ d[a][b]
        q.append(b)
print(*result, sep='\n')
0