結果

問題 No.1054 Union add query
ユーザー uni_pythonuni_python
提出日時 2020-05-16 19:30:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,289 ms / 2,000 ms
コード長 2,684 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 81,628 KB
実行使用メモリ 85,420 KB
最終ジャッジ日時 2023-10-23 22:47:09
合計ジャッジ時間 8,126 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,528 KB
testcase_01 AC 37 ms
53,528 KB
testcase_02 AC 38 ms
53,528 KB
testcase_03 AC 1,289 ms
83,104 KB
testcase_04 AC 906 ms
85,420 KB
testcase_05 AC 1,222 ms
81,780 KB
testcase_06 AC 635 ms
79,396 KB
testcase_07 AC 590 ms
79,392 KB
testcase_08 AC 521 ms
79,260 KB
testcase_09 AC 867 ms
84,048 KB
testcase_10 AC 286 ms
83,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.stdin.readline
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
mod=10**9+7

################
class UnionFind():
    """
    parents : 親要素(findしない場合は根ではないことの注意),根の場合は"-(要素数)"
    find(x):要素xの属するグループの根を返す
    size(x):要素xの属するグループの要素数を返す
    same(x,y):x,yが同じグループに属しているか返す
    members(x):要素xが属するグループに属する要素をリストで返す
    roots:全ての根の要素を返す
    group_count():グループの数を返す
    all_group_members():{根要素:[そのグループに含まれる要素のリスト]}の辞書を返す
    """
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.w=[0]*n

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

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        #yが要素数の小さい方
        if self.parents[x] > self.parents[y]:
            x, y = y, x
            

        self.w[y]-=self.w[x]
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        
    def add_w(self,v,w):
        root=self.find(v)
        self.w[root]+=w

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

    def same(self, x, y):
        return self.find(x) == self.find(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 len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
    
    def calc(self,x):
        #再帰で根までたどる
        if self.parents[x]<0:#根なら
            return self.w[x]
        else:
            plus=self.calc(self.parents[x])#根方向にたどる
            return self.w[x]+plus
    
################
def main():

    N,Q=MI()
    uf=UnionFind(N)

    
    for _ in range(Q):
        t,a,b=MI()
        if t==1:
            a-=1
            b-=1
            uf.union(a,b)
        elif t==2:
            a-=1
            uf.add_w(a,b)
        else:
            a-=1
            print(uf.calc(a))

if __name__=="__main__":
    main()
0