結果

問題 No.1054 Union add query
ユーザー hedwig100hedwig100
提出日時 2020-05-16 11:27:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,180 ms / 2,000 ms
コード長 2,515 bytes
コンパイル時間 83 ms
コンパイル使用メモリ 12,272 KB
実行使用メモリ 34,960 KB
最終ジャッジ日時 2023-10-22 02:29:04
合計ジャッジ時間 9,691 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
10,324 KB
testcase_01 AC 31 ms
10,324 KB
testcase_02 AC 36 ms
10,324 KB
testcase_03 AC 1,180 ms
17,920 KB
testcase_04 AC 1,074 ms
34,960 KB
testcase_05 AC 1,111 ms
14,272 KB
testcase_06 AC 935 ms
16,572 KB
testcase_07 AC 822 ms
16,572 KB
testcase_08 AC 886 ms
16,572 KB
testcase_09 AC 855 ms
31,788 KB
testcase_10 AC 570 ms
25,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

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