結果

問題 No.2072 Anatomy
ユーザー roarisroaris
提出日時 2022-09-16 22:02:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 461 ms / 2,000 ms
コード長 1,389 bytes
コンパイル時間 593 ms
コンパイル使用メモリ 86,668 KB
実行使用メモリ 103,460 KB
最終ジャッジ日時 2023-08-23 15:33:20
合計ジャッジ時間 11,669 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,492 KB
testcase_01 AC 88 ms
71,292 KB
testcase_02 AC 94 ms
72,220 KB
testcase_03 AC 97 ms
76,828 KB
testcase_04 AC 91 ms
71,368 KB
testcase_05 AC 95 ms
76,488 KB
testcase_06 AC 90 ms
71,304 KB
testcase_07 AC 98 ms
77,084 KB
testcase_08 AC 419 ms
100,780 KB
testcase_09 AC 270 ms
100,812 KB
testcase_10 AC 406 ms
99,952 KB
testcase_11 AC 414 ms
101,748 KB
testcase_12 AC 354 ms
96,400 KB
testcase_13 AC 452 ms
102,600 KB
testcase_14 AC 350 ms
93,780 KB
testcase_15 AC 310 ms
93,524 KB
testcase_16 AC 436 ms
102,184 KB
testcase_17 AC 411 ms
101,512 KB
testcase_18 AC 286 ms
91,424 KB
testcase_19 AC 461 ms
103,116 KB
testcase_20 AC 457 ms
103,460 KB
testcase_21 AC 372 ms
102,112 KB
testcase_22 AC 441 ms
102,908 KB
testcase_23 AC 367 ms
101,880 KB
testcase_24 AC 355 ms
101,912 KB
testcase_25 AC 434 ms
102,912 KB
testcase_26 AC 91 ms
71,568 KB
testcase_27 AC 359 ms
101,760 KB
testcase_28 AC 344 ms
102,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import *

class Unionfind:
    def __init__(self, n):
        self.par = [-1]*n
        self.rank = [1]*n
    
    def root(self, x):
        r = x
        
        while not self.par[r]<0:
            r = self.par[r]
        
        t = x
        
        while t!=r:
            tmp = t
            t = self.par[t]
            self.par[tmp] = r
        
        return r
    
    def unite(self, x, y):
        rx = self.root(x)
        ry = self.root(y)
        
        if rx==ry:
            return
        
        if self.rank[rx]<=self.rank[ry]:
            self.par[ry] += self.par[rx]
            self.par[rx] = ry
            
            if self.rank[rx]==self.rank[ry]:
                self.rank[ry] += 1
        else:
            self.par[rx] += self.par[ry]
            self.par[ry] = rx
    
    def is_same(self, x, y):
        return self.root(x)==self.root(y)
    
    def count(self, x):
        return -self.par[self.root(x)]

N, M = map(int, input().split())
edges = [tuple(map(int, input().split())) for _ in range(M)]
dep = [0]*N
uf = Unionfind(N)

for u, v in edges[::-1]:
    u -= 1
    v -= 1
    
    if uf.is_same(u, v):
        dep[uf.root(u)] += 1
    else:
        ru = uf.root(u)
        rv = uf.root(v)
        uf.unite(u, v)
        dep[uf.root(u)] = max(dep[ru], dep[rv])+1

print(dep[uf.root(0)])
0