結果

問題 No.1054 Union add query
ユーザー hedwig100hedwig100
提出日時 2020-05-16 09:57:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,096 ms / 2,000 ms
コード長 2,479 bytes
コンパイル時間 226 ms
コンパイル使用メモリ 81,708 KB
実行使用メモリ 94,064 KB
最終ジャッジ日時 2023-10-21 23:24:15
合計ジャッジ時間 8,025 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,456 KB
testcase_01 AC 40 ms
53,456 KB
testcase_02 AC 38 ms
53,456 KB
testcase_03 AC 1,096 ms
82,740 KB
testcase_04 AC 958 ms
94,064 KB
testcase_05 AC 1,039 ms
80,964 KB
testcase_06 AC 648 ms
82,496 KB
testcase_07 AC 590 ms
82,496 KB
testcase_08 AC 539 ms
82,360 KB
testcase_09 AC 892 ms
91,980 KB
testcase_10 AC 290 ms
91,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(100000000)
MOD = 10 ** 9 + 7
INF = 10 ** 15

class UnionFind():
    def __init__(self,n):
        self.n = n
        self.parents = [-1]*n
        self.count = [0] * n
        self.lazy = [0]*n
        self.plus = [0]*n

    def find(self,x): #根を見つける、繋ぎ直す
        if self.parents[x] < 0:
            return x
        else:
            p = self.find(self.parents[x])
            self.lazy[x] += self.lazy[self.parents[x]]
            self.lazy[x] += self.plus[self.parents[x]]
            self.parents[x] = p
            return p
    
    def unite(self,x,y): #x,yの含むグループを併合する
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        
        if self.parents[x] > self.parents[y]:
            x,y = y,x

        self.parents[x] += self.parents[y]
        self.lazy[y] = -self.count[x]
        self.plus[y] = self.count[y]
        self.parents[y] = x
    
    def same(self,x,y):#xとyが同じグループにいるか判定
        return self.find(x) == self.find(y)
    
    def members(self,x):#xと同じグループのメンバーを列挙
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]
    
    def size(self,x):#xが属するグループのメンバーの数
        return -self.parents[self.find(x)]
    
    def roots(self):#ufの根を列挙
        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):#print()での表示用
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())
    
    def add(self,x,b):
        x = self.find(x)
        self.count[x] += b
    
    def calc(self,x):
        if self.parents[x] < 0:
            return self.count[x]
        self.find(x)
        return self.lazy[x] + self.count[x] + self.count[self.parents[x]]

def main():  
    N,Q = map(int,input().split())
    uf = UnionFind(N)
    for _ in range(Q):
        t,a,b = map(int,input().split())
        if t == 1:
            a -= 1
            b -= 1
            uf.unite(a,b)
        elif t == 2:
            a -= 1
            uf.add(a,b)
        else:
            a -= 1
            print(uf.calc(a))
if __name__ == '__main__':
    main()
0