結果

問題 No.1420 国勢調査 (Easy)
ユーザー rlangevinrlangevin
提出日時 2023-05-07 12:45:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,549 bytes
コンパイル時間 243 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 81,900 KB
最終ジャッジ日時 2024-05-03 13:04:16
合計ジャッジ時間 10,219 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,224 KB
testcase_01 AC 40 ms
52,352 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 AC 328 ms
81,900 KB
testcase_23 AC 330 ms
81,240 KB
testcase_24 AC 329 ms
81,548 KB
testcase_25 AC 330 ms
81,656 KB
testcase_26 AC 342 ms
81,308 KB
testcase_27 AC 290 ms
81,212 KB
testcase_28 AC 288 ms
80,768 KB
testcase_29 AC 286 ms
81,288 KB
testcase_30 AC 285 ms
81,024 KB
testcase_31 AC 286 ms
80,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class UnionFind(object):
    def __init__(self, n=1):
        self.par = [i for i in range(n)]
        self.rank = [0 for _ in range(n)]
        self.size = [1 for _ in range(n)]
        self.diff_weight = [0 for _ in range(n)]
        self.flag = 1

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            root = self.find(self.par[x])
            self.diff_weight[x] ^= self.diff_weight[self.par[x]]
            self.par[x] = root
            return self.par[x]

    def union(self, x, y, w):
        rx = self.find(x)
        ry = self.find(y)
        w ^= self.diff_weight[y]^self.diff_weight[x]
        if rx != ry:
            if self.rank[rx] < self.rank[ry]:
                rx, ry = ry, rx
            if self.rank[rx] == self.rank[ry]:
                self.rank[rx] += 1
            self.par[ry] = rx
            self.size[rx] += self.size[ry]
            self.diff_weight[ry] = w
        else:
            if w:
                self.flag = 0


    def is_same(self, x, y):
        return self.find(x) == self.find(y)

    def get_size(self, x):
        x = self.find(x)
        return self.size[x]
    
    def diff(self, x, y):
        return self.diff_weight[y]^self.diff_weight[x]

N, M = map(int, input().split())
U = UnionFind(N)
for i in range(N):
    A, B = map(int, input().split())
    A, B = A - 1, B - 1
    Y = int(input())    
    U.union(A, B, Y)
    
if U.flag:
    for i in range(N):
        print(U.diff(U.find(i), i))
else:
    print(-1)
0