結果

問題 No.2072 Anatomy
ユーザー 👑 H20H20
提出日時 2022-09-16 22:22:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 507 ms / 2,000 ms
コード長 1,627 bytes
コンパイル時間 545 ms
コンパイル使用メモリ 86,900 KB
実行使用メモリ 107,676 KB
最終ジャッジ日時 2023-08-23 16:02:19
合計ジャッジ時間 10,216 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,276 KB
testcase_01 AC 86 ms
71,440 KB
testcase_02 AC 96 ms
76,364 KB
testcase_03 AC 100 ms
76,524 KB
testcase_04 AC 93 ms
72,408 KB
testcase_05 AC 101 ms
76,200 KB
testcase_06 AC 97 ms
76,060 KB
testcase_07 AC 99 ms
76,448 KB
testcase_08 AC 507 ms
103,932 KB
testcase_09 AC 289 ms
102,072 KB
testcase_10 AC 441 ms
104,460 KB
testcase_11 AC 359 ms
102,984 KB
testcase_12 AC 304 ms
97,320 KB
testcase_13 AC 415 ms
105,748 KB
testcase_14 AC 371 ms
96,504 KB
testcase_15 AC 258 ms
94,152 KB
testcase_16 AC 439 ms
105,228 KB
testcase_17 AC 339 ms
103,356 KB
testcase_18 AC 249 ms
91,776 KB
testcase_19 AC 424 ms
106,052 KB
testcase_20 AC 498 ms
107,676 KB
testcase_21 AC 296 ms
102,916 KB
testcase_22 AC 414 ms
105,080 KB
testcase_23 AC 295 ms
103,028 KB
testcase_24 AC 290 ms
102,928 KB
testcase_25 AC 409 ms
105,472 KB
testcase_26 AC 87 ms
71,588 KB
testcase_27 AC 298 ms
102,996 KB
testcase_28 AC 474 ms
107,332 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# UnionFind 参考は以下のサイト
# https://note.nkmk.me/python-union-find/
from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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:
            return

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

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    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 len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())


N,M = map(int, input().split())
UV = [list(map(int, input().split())) for _ in range(M)][::-1]
uf = UnionFind(N)
L = [0]*N
for u,v in UV:
    temp = max(L[uf.find(u-1)],L[uf.find(v-1)])+1
    uf.union(u-1,v-1)
    L[uf.find(u-1)] = temp
print(max(L))
0