結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,520 KB
testcase_01 AC 38 ms
53,520 KB
testcase_02 AC 37 ms
53,520 KB
testcase_03 AC 1,294 ms
83,012 KB
testcase_04 AC 899 ms
85,416 KB
testcase_05 AC 1,226 ms
82,004 KB
testcase_06 AC 632 ms
79,324 KB
testcase_07 AC 583 ms
79,380 KB
testcase_08 AC 517 ms
79,252 KB
testcase_09 AC 867 ms
84,036 KB
testcase_10 AC 284 ms
83,636 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

def main():
        
    ################
    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
        
    ################


    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))

main()
0