結果

問題 No.1420 国勢調査 (Easy)
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-03-05 22:19:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 302 ms / 2,000 ms
コード長 936 bytes
コンパイル時間 447 ms
コンパイル使用メモリ 82,264 KB
実行使用メモリ 97,224 KB
最終ジャッジ日時 2024-04-16 10:00:05
合計ジャッジ時間 8,271 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,868 KB
testcase_01 AC 40 ms
55,168 KB
testcase_02 AC 170 ms
85,444 KB
testcase_03 AC 179 ms
86,784 KB
testcase_04 AC 171 ms
86,948 KB
testcase_05 AC 179 ms
86,900 KB
testcase_06 AC 171 ms
87,044 KB
testcase_07 AC 154 ms
84,208 KB
testcase_08 AC 157 ms
85,264 KB
testcase_09 AC 155 ms
84,424 KB
testcase_10 AC 151 ms
84,624 KB
testcase_11 AC 156 ms
84,376 KB
testcase_12 AC 89 ms
83,916 KB
testcase_13 AC 128 ms
85,044 KB
testcase_14 AC 90 ms
83,652 KB
testcase_15 AC 121 ms
84,872 KB
testcase_16 AC 133 ms
85,236 KB
testcase_17 AC 129 ms
84,972 KB
testcase_18 AC 133 ms
85,384 KB
testcase_19 AC 129 ms
85,172 KB
testcase_20 AC 130 ms
84,904 KB
testcase_21 AC 128 ms
84,868 KB
testcase_22 AC 298 ms
97,224 KB
testcase_23 AC 285 ms
97,072 KB
testcase_24 AC 301 ms
97,072 KB
testcase_25 AC 302 ms
97,016 KB
testcase_26 AC 296 ms
97,080 KB
testcase_27 AC 183 ms
91,268 KB
testcase_28 AC 193 ms
91,820 KB
testcase_29 AC 191 ms
91,524 KB
testcase_30 AC 197 ms
91,604 KB
testcase_31 AC 198 ms
91,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

桁ごとに求められる
1箇所決め打てば、後は連鎖的に決まる
桁ごとにdfsして、矛盾なければおk

"""

import sys
from sys import stdin
from collections import deque  

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

lis = [ [] for i in range(N) ]

for i in range(M):

    A,B = map(int,stdin.readline().split())
    A -= 1
    B -= 1

    Y = int(stdin.readline())

    lis[A].append((B,Y))
    lis[B].append((A,Y))

ans = [0] * N
able = [True] * N

for st in range(N):

    if able[st]:

        q = deque([st])
        able[st] = False

        while q:
            v = q.popleft()

            for nex,Y in lis[v]:
                if able[nex]:
                    able[nex] = False
                    ans[nex] = (Y ^ ans[v])
                    q.append(nex)
                elif ans[nex] != (Y ^ ans[v]):
                    print (-1)
                    sys.exit()

print ("\n".join(map(str,ans)))
0