結果

問題 No.2072 Anatomy
ユーザー lloyzlloyz
提出日時 2022-09-18 13:43:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 591 ms / 2,000 ms
コード長 2,405 bytes
コンパイル時間 1,259 ms
コンパイル使用メモリ 86,828 KB
実行使用メモリ 95,724 KB
最終ジャッジ日時 2023-08-23 18:07:59
合計ジャッジ時間 10,834 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 87 ms
71,764 KB
testcase_01 AC 89 ms
71,268 KB
testcase_02 AC 97 ms
76,080 KB
testcase_03 AC 99 ms
76,236 KB
testcase_04 AC 93 ms
72,308 KB
testcase_05 AC 97 ms
76,340 KB
testcase_06 AC 100 ms
76,472 KB
testcase_07 AC 103 ms
76,384 KB
testcase_08 AC 591 ms
95,724 KB
testcase_09 AC 253 ms
90,876 KB
testcase_10 AC 498 ms
93,668 KB
testcase_11 AC 360 ms
92,152 KB
testcase_12 AC 291 ms
88,724 KB
testcase_13 AC 414 ms
94,472 KB
testcase_14 AC 438 ms
89,628 KB
testcase_15 AC 238 ms
86,760 KB
testcase_16 AC 499 ms
95,464 KB
testcase_17 AC 303 ms
92,088 KB
testcase_18 AC 226 ms
85,568 KB
testcase_19 AC 404 ms
93,064 KB
testcase_20 AC 545 ms
94,884 KB
testcase_21 AC 259 ms
92,296 KB
testcase_22 AC 472 ms
95,056 KB
testcase_23 AC 258 ms
92,180 KB
testcase_24 AC 252 ms
92,232 KB
testcase_25 AC 391 ms
93,268 KB
testcase_26 AC 91 ms
71,712 KB
testcase_27 AC 258 ms
92,524 KB
testcase_28 AC 483 ms
95,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素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を含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素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):
        '''
        要素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())
UF = UnionFind(n)
RootCnt = [0 for _ in range(n)]
Edges = []
for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    Edges.append((u, v))

Edges.reverse()
ans = 0
for i in range(m):
    u, v = Edges[i]
    if UF.same(u, v):
        root = UF.find(u)
        RootCnt[root] += 1
        ans = max(ans, RootCnt[root])
    else:
        uroot = UF.find(u)
        vroot = UF.find(v)
        cnt = max(RootCnt[uroot], RootCnt[vroot]) + 1
        ans = max(ans, cnt)
        UF.union(u, v)
        root = UF.find(u)
        RootCnt[root] = cnt
print(ans)
0