結果

問題 No.556 仁義なきサルたち
ユーザー convexineqconvexineq
提出日時 2021-02-25 05:28:51
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 120 ms / 2,000 ms
コード長 1,418 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 81,620 KB
実行使用メモリ 77,036 KB
最終ジャッジ日時 2023-10-25 10:07:47
合計ジャッジ時間 2,891 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
53,480 KB
testcase_01 AC 35 ms
53,480 KB
testcase_02 AC 33 ms
53,480 KB
testcase_03 AC 35 ms
53,480 KB
testcase_04 AC 34 ms
53,480 KB
testcase_05 AC 36 ms
53,480 KB
testcase_06 AC 37 ms
53,480 KB
testcase_07 AC 36 ms
53,480 KB
testcase_08 AC 38 ms
53,480 KB
testcase_09 AC 47 ms
61,348 KB
testcase_10 AC 50 ms
63,524 KB
testcase_11 AC 57 ms
68,192 KB
testcase_12 AC 57 ms
68,328 KB
testcase_13 AC 52 ms
66,416 KB
testcase_14 AC 88 ms
76,496 KB
testcase_15 AC 108 ms
76,960 KB
testcase_16 AC 85 ms
76,492 KB
testcase_17 AC 111 ms
77,036 KB
testcase_18 AC 120 ms
77,036 KB
testcase_19 AC 78 ms
76,436 KB
testcase_20 AC 81 ms
76,432 KB
testcase_21 AC 84 ms
76,432 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n)) #親ノード
        self.size = [1]*n #グループの要素数
        self.top = list(range(n)) #グループの要素数
 
    def root(self, x): #root(x): xの根ノードを返す.
        while self.parent[x] != x:
            self.parent[x] = self.parent[self.parent[x]]
            x = self.parent[x]
        return x 
 
    def merge(self, x, y): #merge(x,y): xのいる組とyのいる組をまとめる
        x, y = self.root(x), self.root(y)
        if x == y: return False
        if self.size[x] < self.size[y] or (self.size[x] == self.size[y] and self.top[x] > self.top[y]):
            self.top[x] = self.top[y]
        else:
            self.top[y] = self.top[x]
        if self.size[x] < self.size[y]: x,y=y,x #xの要素数が大きいように
        self.size[x] += self.size[y] #xの要素数を更新
        self.parent[y] = x #yをxにつなぐ
        return True
 
    def issame(self, x, y): #same(x,y): xとyが同じ組ならTrue
        return self.root(x) == self.root(y)
        
    def getsize(self,x): #size(x): xのいるグループの要素数を返す
        return self.size[self.root(x)]


n,m = map(int,input().split())
UF = UnionFind(n)
top = list(range(n))
for _ in range(m):
    a,b = map(int,input().split())
    UF.merge(a-1,b-1)

for i in range(n):
    print(UF.top[UF.root(i)]+1)
0