結果

問題 No.1054 Union add query
ユーザー AEnAEn
提出日時 2022-12-22 01:05:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,264 ms / 2,000 ms
コード長 2,670 bytes
コンパイル時間 178 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 87,368 KB
最終ジャッジ日時 2024-04-29 03:36:32
合計ジャッジ時間 9,489 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
66,688 KB
testcase_01 AC 71 ms
66,816 KB
testcase_02 AC 72 ms
67,200 KB
testcase_03 AC 1,163 ms
83,372 KB
testcase_04 AC 919 ms
87,368 KB
testcase_05 AC 1,264 ms
83,860 KB
testcase_06 AC 690 ms
82,176 KB
testcase_07 AC 656 ms
82,176 KB
testcase_08 AC 592 ms
82,176 KB
testcase_09 AC 876 ms
86,912 KB
testcase_10 AC 330 ms
85,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from typing import List

class UnionFind:
    """0-indexed"""
    def __init__(self, n):
        self.n = n
        self.parent = [-1] * n
        self.num = [0]*n
        self.__group_count = n

    def unite(self, x, y) -> bool:
        """xとyをマージ"""
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return False

        self.__group_count -= 1

        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.num[y] -= self.num[x]
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def is_same(self, x, y) -> bool:
        """xとyが同じ連結成分か判定"""
        return self.root(x) == self.root(y)

    def root(self, x) -> int:
        """xの根を取得"""
        if self.parent[x] < 0:
            return x
        else:
            # 経路圧縮あり
            # self.parent[x] = self.root(self.parent[x])
            # return self.parent[x]
            # 経路圧縮なし
            return self.root(self.parent[x])

    def size(self, x) -> int:
        """xが属する連結成分のサイズを取得"""
        return -self.parent[self.root(x)]

    def all_sizes(self) -> List[int]:
        """全連結成分のサイズのリストを取得 O(N)"""
        sizes = []
        for i in range(self.n):
            size = self.parent[i]
            if size < 0:
                sizes.append(-size)
        return sizes
    
    def members(self, x) -> List[int]:
        """xが属するグループのリストを返す O(N)"""
        mem = []
        r = self.root(x)
        for i in range(self.n):
            if self.root(i) == r:
                mem.append(i)
        return mem

    def groups(self) -> List[List[int]]:
        """全連結成分の内容のリストを取得 O(N・α(N))"""
        groups = dict()
        for i in range(self.n):
            p = self.root(i)
            if not groups.get(p):
                groups[p] = []
            groups[p].append(i)
        return list(groups.values())

    @property
    def group_count(self) -> int:
        """連結成分の数を取得 O(1)"""
        return self.__group_count
    
    def add(self,v,x):
        root = self.root(v)
        self.num[root] += x
    
    def query(self,v,res=0):
        res += self.num[v]
        if self.parent[v]<0:
            return res
        else:
            return self.query(self.parent[v],res)

N, Q = map(int, input().split())
uf = UnionFind(N)
for i in range(Q):
    t,a,b, = map(int, input().split())
    if t==1:
        uf.unite(a-1,b-1)
    elif t==2:
        uf.add(a-1,b)
    else:
        print(uf.query(a-1))
0