結果

問題 No.556 仁義なきサルたち
ユーザー kohei2019kohei2019
提出日時 2022-02-09 20:37:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 223 ms / 2,000 ms
コード長 1,530 bytes
コンパイル時間 958 ms
コンパイル使用メモリ 87,308 KB
実行使用メモリ 80,720 KB
最終ジャッジ日時 2023-09-07 06:22:47
合計ジャッジ時間 4,914 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,504 KB
testcase_01 AC 78 ms
71,164 KB
testcase_02 AC 78 ms
71,456 KB
testcase_03 AC 78 ms
71,180 KB
testcase_04 AC 77 ms
71,544 KB
testcase_05 AC 78 ms
71,464 KB
testcase_06 AC 78 ms
71,432 KB
testcase_07 AC 78 ms
71,328 KB
testcase_08 AC 80 ms
71,356 KB
testcase_09 AC 85 ms
75,268 KB
testcase_10 AC 103 ms
76,032 KB
testcase_11 AC 103 ms
76,308 KB
testcase_12 AC 104 ms
76,556 KB
testcase_13 AC 97 ms
76,688 KB
testcase_14 AC 149 ms
78,868 KB
testcase_15 AC 169 ms
78,636 KB
testcase_16 AC 141 ms
78,724 KB
testcase_17 AC 176 ms
78,916 KB
testcase_18 AC 223 ms
80,720 KB
testcase_19 AC 134 ms
78,088 KB
testcase_20 AC 133 ms
78,156 KB
testcase_21 AC 130 ms
77,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = list(range(n))
        self.size0 = [1]*(n)
        self.roots = n

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

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

        if self.parents[x] > self.parents[y]:
            x, y = y, x
        if self.size(x) < self.size(y):
            x,y = y,x
        if x == y:
            return
        self.size0[x] += self.size0[y]
        self.roots -= 1
        self.parents[y] = x

    def size(self, x):#O(1)xが含まれる集合の要素数
        return self.size0[self.find(x)]

    def same(self, x, y):#O(1)
        return self.find(x) == self.find(y)

    def membersf(self, x):#取り出し部分はO(N)
        p = self.find(x)
        ret = []
        for i in range(self.n):
            if self.find(i) == p:
                ret.append(i)
        return ret

    def rootsf(self):#根の要素O(N)
        ret = []
        for i in range(self.n):
            if self.find(i) == i:
                ret.append(i)
        return ret

    def group_count(self):#根の数O(1)
        return self.roots

N,M = map(int,input().split())
lsAB = [tuple(map(lambda x:int(x)-1,input().split())) for i in range(M)]
UF = UnionFind(N)
for i in range(M):
    a,b = lsAB[i]
    UF.union(a,b)

for i in range(N):
    print(UF.find(i)+1)
0