結果

問題 No.1420 国勢調査 (Easy)
ユーザー NatsubiSoganNatsubiSogan
提出日時 2021-03-06 00:05:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 467 ms / 2,000 ms
コード長 1,294 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 82,300 KB
最終ジャッジ日時 2024-04-16 12:12:42
合計ジャッジ時間 10,523 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
52,352 KB
testcase_01 AC 41 ms
52,480 KB
testcase_02 AC 308 ms
78,708 KB
testcase_03 AC 322 ms
79,252 KB
testcase_04 AC 314 ms
79,232 KB
testcase_05 AC 294 ms
78,940 KB
testcase_06 AC 292 ms
78,576 KB
testcase_07 AC 238 ms
79,348 KB
testcase_08 AC 261 ms
78,564 KB
testcase_09 AC 245 ms
79,084 KB
testcase_10 AC 279 ms
78,972 KB
testcase_11 AC 222 ms
78,476 KB
testcase_12 AC 114 ms
79,232 KB
testcase_13 AC 155 ms
80,384 KB
testcase_14 AC 122 ms
79,360 KB
testcase_15 AC 159 ms
80,128 KB
testcase_16 AC 155 ms
80,512 KB
testcase_17 AC 157 ms
80,128 KB
testcase_18 AC 153 ms
80,384 KB
testcase_19 AC 155 ms
80,256 KB
testcase_20 AC 159 ms
80,128 KB
testcase_21 AC 167 ms
79,888 KB
testcase_22 AC 467 ms
81,864 KB
testcase_23 AC 451 ms
82,300 KB
testcase_24 AC 449 ms
81,668 KB
testcase_25 AC 459 ms
82,160 KB
testcase_26 AC 457 ms
81,264 KB
testcase_27 AC 235 ms
80,512 KB
testcase_28 AC 235 ms
80,396 KB
testcase_29 AC 176 ms
80,000 KB
testcase_30 AC 242 ms
80,788 KB
testcase_31 AC 240 ms
80,092 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class WeightedUnionFind:
    def __init__(self, n):
        self.n = n
        self.par = list(range(n))
        self.rank = [0] * n
        self.weight = [0] * n

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

    def unite(self, x, y, w):
        p, q = self.find(x), self.find(y)
        if self.rank[p] < self.rank[q]:
            self.par[p] = q
            self.weight[p] = w ^ self.weight[x] ^ self.weight[y]
        else:
            self.par[q] = p
            self.weight[q] = w ^ self.weight[y] ^ self.weight[x]
            if self.rank[p] == self.rank[q]:
                self.rank[p] += 1

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

    def diff(self, x, y):
        return self.weight[x] ^ self.weight[y]

f = True
n, m = map(int, input().split())
UF = WeightedUnionFind(n)
for _ in range(m):
    a, b = map(int, input().split())
    y = int(input())
    if UF.same(a - 1, b - 1):
        if UF.diff(a - 1, b - 1) != y:
            f = False
            break
    UF.unite(a - 1, b - 1, y)
if not f: exit(print(-1))
for i in range(n):
    print(UF.diff(i, UF.find(i)))
0