結果

問題 No.2072 Anatomy
ユーザー 👑 rin204rin204
提出日時 2022-09-16 21:26:45
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 465 ms / 2,000 ms
コード長 1,554 bytes
コンパイル時間 303 ms
コンパイル使用メモリ 87,124 KB
実行使用メモリ 105,360 KB
最終ジャッジ日時 2023-08-23 14:25:25
合計ジャッジ時間 9,569 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,240 KB
testcase_01 AC 72 ms
71,392 KB
testcase_02 AC 73 ms
71,384 KB
testcase_03 AC 72 ms
71,328 KB
testcase_04 AC 71 ms
71,588 KB
testcase_05 AC 74 ms
71,304 KB
testcase_06 AC 72 ms
71,584 KB
testcase_07 AC 74 ms
71,544 KB
testcase_08 AC 444 ms
102,432 KB
testcase_09 AC 271 ms
101,132 KB
testcase_10 AC 398 ms
102,432 KB
testcase_11 AC 326 ms
102,200 KB
testcase_12 AC 282 ms
97,160 KB
testcase_13 AC 380 ms
103,204 KB
testcase_14 AC 348 ms
95,592 KB
testcase_15 AC 225 ms
92,304 KB
testcase_16 AC 407 ms
104,048 KB
testcase_17 AC 316 ms
102,584 KB
testcase_18 AC 209 ms
90,260 KB
testcase_19 AC 380 ms
104,036 KB
testcase_20 AC 465 ms
105,360 KB
testcase_21 AC 271 ms
101,932 KB
testcase_22 AC 387 ms
104,868 KB
testcase_23 AC 269 ms
101,948 KB
testcase_24 AC 270 ms
102,048 KB
testcase_25 AC 359 ms
104,128 KB
testcase_26 AC 71 ms
71,196 KB
testcase_27 AC 267 ms
102,124 KB
testcase_28 AC 414 ms
105,000 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n
        self.cnt = [0] * n

    def find(self, x):
        if self.parents[x] < 0:
            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 x == y:
            self.cnt[x] += 1
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x
        self.cnt[x] = max(self.cnt[x], self.cnt[y]) + 1

    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 self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n, m = map(int, input().split())
E = [list(map(int, input().split())) for _ in range(m)]
UF = UnionFind(n)
for u, v in E[::-1]:
    UF.union(u - 1, v - 1)

print(UF.cnt[UF.find(0)])
0