結果

問題 No.1054 Union add query
ユーザー 👑 rin204rin204
提出日時 2022-03-24 17:21:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 464 ms / 2,000 ms
コード長 2,300 bytes
コンパイル時間 229 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 95,880 KB
最終ジャッジ日時 2024-04-21 02:55:43
合計ジャッジ時間 5,043 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,608 KB
testcase_01 AC 40 ms
52,608 KB
testcase_02 AC 38 ms
52,736 KB
testcase_03 AC 464 ms
84,100 KB
testcase_04 AC 417 ms
95,880 KB
testcase_05 AC 418 ms
82,000 KB
testcase_06 AC 248 ms
82,816 KB
testcase_07 AC 210 ms
83,324 KB
testcase_08 AC 243 ms
83,072 KB
testcase_09 AC 283 ms
93,440 KB
testcase_10 AC 155 ms
91,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

class WeightedUnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.weight = [0] * n # W_i - W_{P_i}
        self.group = n
        self.W = [0] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            p = self.find(self.parents[x])
            self.weight[x] += self.weight[self.parents[x]]
            self.parents[x] = p
            return self.parents[x]

    # x = y + w
    def union(self, x, y, w):
        xp = self.find(x)
        yp = self.find(y)
        w -= self.weight[x]
        x = xp
        w += self.weight[y]
        y = yp
        
        if x == y:
            assert w == 0
            return
        self.group -= 1
        
        if self.parents[x] > self.parents[y]:
            w *= -1
            x, y = y, x
        
        self.parents[x] += self.parents[y]
        self.weight[y] = -w
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)
        
    def diff(self, x, y):
        if not self.same(x, y):
            return None
        else:
            return self.weight[x] - self.weight[y]

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
        
    def add(self, a, b):
        a = self.find(a)
        self.W[a] += b
        
    def get(self, a):
        p = self.find(a)
        return self.W[p] + self.diff(a, p)
        
n, q = map(int, input().split())
W = [0] * n
UF = WeightedUnionFind(n)

for _ in range(q):
    t, a, b = map(int, input().split())
    a -= 1
    if t == 1:
        b -= 1
        UF.union(a, b, UF.get(a) - UF.get(b))
    elif t == 2:
        UF.add(a, b)
    else:
        print(UF.get(a))

0