結果

問題 No.2072 Anatomy
ユーザー chineristACchineristAC
提出日時 2022-09-14 13:34:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 580 ms / 2,000 ms
コード長 1,510 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 115,188 KB
最終ジャッジ日時 2023-08-21 13:20:52
合計ジャッジ時間 14,025 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
74,708 KB
testcase_01 AC 111 ms
74,628 KB
testcase_02 AC 113 ms
74,588 KB
testcase_03 AC 120 ms
78,712 KB
testcase_04 AC 111 ms
74,544 KB
testcase_05 AC 117 ms
79,024 KB
testcase_06 AC 113 ms
74,488 KB
testcase_07 AC 121 ms
79,252 KB
testcase_08 AC 550 ms
111,416 KB
testcase_09 AC 367 ms
109,596 KB
testcase_10 AC 528 ms
111,592 KB
testcase_11 AC 431 ms
110,560 KB
testcase_12 AC 397 ms
105,668 KB
testcase_13 AC 482 ms
113,020 KB
testcase_14 AC 448 ms
103,736 KB
testcase_15 AC 293 ms
100,836 KB
testcase_16 AC 525 ms
113,588 KB
testcase_17 AC 397 ms
111,640 KB
testcase_18 AC 283 ms
97,864 KB
testcase_19 AC 478 ms
113,548 KB
testcase_20 AC 580 ms
114,248 KB
testcase_21 AC 347 ms
111,896 KB
testcase_22 AC 538 ms
115,148 KB
testcase_23 AC 386 ms
111,604 KB
testcase_24 AC 343 ms
111,900 KB
testcase_25 AC 481 ms
113,428 KB
testcase_26 AC 114 ms
74,300 KB
testcase_27 AC 377 ms
112,188 KB
testcase_28 AC 526 ms
115,188 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