結果

問題 No.1420 国勢調査 (Easy)
ユーザー rlangevinrlangevin
提出日時 2023-05-07 12:54:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 308 ms / 2,000 ms
コード長 1,563 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 82,388 KB
実行使用メモリ 81,712 KB
最終ジャッジ日時 2024-05-03 13:07:33
合計ジャッジ時間 7,708 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
52,480 KB
testcase_01 AC 33 ms
52,352 KB
testcase_02 AC 176 ms
77,832 KB
testcase_03 AC 169 ms
77,312 KB
testcase_04 AC 174 ms
77,248 KB
testcase_05 AC 172 ms
77,276 KB
testcase_06 AC 172 ms
77,724 KB
testcase_07 AC 173 ms
77,184 KB
testcase_08 AC 168 ms
77,588 KB
testcase_09 AC 156 ms
77,452 KB
testcase_10 AC 160 ms
77,540 KB
testcase_11 AC 161 ms
77,440 KB
testcase_12 AC 75 ms
79,432 KB
testcase_13 AC 103 ms
79,872 KB
testcase_14 AC 78 ms
79,128 KB
testcase_15 AC 96 ms
79,872 KB
testcase_16 AC 101 ms
79,232 KB
testcase_17 AC 100 ms
79,744 KB
testcase_18 AC 93 ms
79,360 KB
testcase_19 AC 91 ms
79,836 KB
testcase_20 AC 105 ms
79,744 KB
testcase_21 AC 101 ms
79,872 KB
testcase_22 AC 308 ms
81,260 KB
testcase_23 AC 289 ms
81,632 KB
testcase_24 AC 293 ms
81,712 KB
testcase_25 AC 294 ms
81,180 KB
testcase_26 AC 298 ms
80,912 KB
testcase_27 AC 253 ms
81,408 KB
testcase_28 AC 261 ms
81,152 KB
testcase_29 AC 277 ms
80,896 KB
testcase_30 AC 250 ms
80,768 KB
testcase_31 AC 246 ms
80,640 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(M):
    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):
        U.find(i)
        print(U.diff_weight[i])
else:
    print(-1)
0