結果

問題 No.1420 国勢調査 (Easy)
ユーザー nephrologistnephrologist
提出日時 2021-03-05 23:52:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 624 ms / 2,000 ms
コード長 1,166 bytes
コンパイル時間 175 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 123,988 KB
最終ジャッジ日時 2024-04-16 11:58:08
合計ジャッジ時間 13,254 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
53,632 KB
testcase_01 AC 48 ms
54,144 KB
testcase_02 AC 406 ms
91,416 KB
testcase_03 AC 409 ms
91,652 KB
testcase_04 AC 419 ms
91,128 KB
testcase_05 AC 421 ms
91,596 KB
testcase_06 AC 410 ms
91,556 KB
testcase_07 AC 369 ms
91,388 KB
testcase_08 AC 364 ms
91,408 KB
testcase_09 AC 365 ms
91,364 KB
testcase_10 AC 379 ms
91,156 KB
testcase_11 AC 361 ms
91,104 KB
testcase_12 AC 136 ms
81,920 KB
testcase_13 AC 214 ms
89,304 KB
testcase_14 AC 151 ms
85,504 KB
testcase_15 AC 222 ms
89,864 KB
testcase_16 AC 222 ms
89,860 KB
testcase_17 AC 223 ms
89,824 KB
testcase_18 AC 220 ms
89,712 KB
testcase_19 AC 214 ms
89,072 KB
testcase_20 AC 226 ms
89,704 KB
testcase_21 AC 217 ms
89,440 KB
testcase_22 AC 614 ms
123,988 KB
testcase_23 AC 605 ms
123,952 KB
testcase_24 AC 596 ms
123,392 KB
testcase_25 AC 611 ms
123,792 KB
testcase_26 AC 624 ms
123,844 KB
testcase_27 AC 292 ms
105,096 KB
testcase_28 AC 443 ms
122,236 KB
testcase_29 AC 206 ms
92,796 KB
testcase_30 AC 350 ms
111,828 KB
testcase_31 AC 339 ms
111,112 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

n, m = map(int, input().split())
graph = [[] for _ in range(n)]
memo = defaultdict(lambda: defaultdict(lambda: -1))
for _ in range(m):
    a, b = map(int, input().split())
    y = int(input())
    a, b = a - 1, b - 1
    graph[a].append(b)
    graph[b].append(a)
    if a > b:
        b, a = a, b
    if memo[a][b] >= 0:
        if memo[a][b] == y:
            continue
        else:
            print(-1)
            exit()
    memo[a][b] = y


used = [0] * n
ans = [-1] * n
for i in range(n):
    if not used[i]:
        stack = [i]
        used[i] = 1
        ans[i] = 0
        while stack:
            v = stack.pop()
            now = ans[v]
            for u in graph[v]:
                if u < v:
                    temp = memo[u][v]
                else:
                    temp = memo[v][u]
                if used[u]:
                    if ans[u] ^ ans[v] == temp:
                        continue
                    else:
                        print(-1)
                        exit()
                used[u] = 1
                ans[u] = temp ^ ans[v]
                stack.append(u)
for a in ans:
    print(a)
0