結果

問題 No.2072 Anatomy
ユーザー chineristACchineristAC
提出日時 2022-09-14 13:34:20
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 759 ms / 2,000 ms
コード長 1,510 bytes
コンパイル時間 260 ms
使用メモリ 121,488 KB
最終ジャッジ日時 2023-01-07 23:42:25
合計ジャッジ時間 17,229 ms
ジャッジサーバーID
(参考情報)
judge16 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 125 ms
78,396 KB
testcase_01 AC 124 ms
78,396 KB
testcase_02 AC 130 ms
78,588 KB
testcase_03 AC 139 ms
83,160 KB
testcase_04 AC 128 ms
78,584 KB
testcase_05 AC 132 ms
83,056 KB
testcase_06 AC 129 ms
78,576 KB
testcase_07 AC 138 ms
83,280 KB
testcase_08 AC 717 ms
116,692 KB
testcase_09 AC 534 ms
114,688 KB
testcase_10 AC 693 ms
116,540 KB
testcase_11 AC 618 ms
116,124 KB
testcase_12 AC 545 ms
110,072 KB
testcase_13 AC 665 ms
118,252 KB
testcase_14 AC 575 ms
109,340 KB
testcase_15 AC 409 ms
105,068 KB
testcase_16 AC 714 ms
119,984 KB
testcase_17 AC 574 ms
116,852 KB
testcase_18 AC 385 ms
102,160 KB
testcase_19 AC 659 ms
119,320 KB
testcase_20 AC 759 ms
121,488 KB
testcase_21 AC 513 ms
116,620 KB
testcase_22 AC 702 ms
120,720 KB
testcase_23 AC 536 ms
116,728 KB
testcase_24 AC 496 ms
116,820 KB
testcase_25 AC 641 ms
118,708 KB
testcase_26 AC 123 ms
78,448 KB
testcase_27 AC 554 ms
116,788 KB
testcase_28 AC 703 ms
120,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        stack = [x]
        while self._parent[stack[-1]]!=stack[-1]:
            stack.append(self._parent[stack[-1]])
        for v in stack:
            self._parent[v] = stack[-1]
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

    def is_same_group(self, x, y):
        return self.find_root(x) == self.find_root(y)

import sys,random
from collections import deque

def solve(N,M,_E):
    E = [(u-1,v-1) for u,v in _E]
    uf = UnionFindVerSize(N)
    D = [0] * N
    for u,v in E[::-1]:
        pu,pv = uf.find_root(u),uf.find_root(v)
        if pu!=pv:
            uf.unite(pu,pv)
            D[uf.find_root(pu)] = max(D[pu],D[pv]) + 1
        else:
            D[pu] += 1
    
    return max(D)

N,M = map(int,input().split())
E = [tuple(map(int,input().split())) for _ in range(M)]

print(solve(N,M,E))
0