結果

問題 No.2072 Anatomy
ユーザー roarisroaris
提出日時 2022-09-16 22:02:17
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 512 ms / 2,000 ms
コード長 1,389 bytes
コンパイル時間 307 ms
使用メモリ 108,516 KB
最終ジャッジ日時 2023-01-11 06:59:36
合計ジャッジ時間 11,766 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 98 ms
76,668 KB
testcase_01 AC 99 ms
76,568 KB
testcase_02 AC 103 ms
76,600 KB
testcase_03 AC 106 ms
81,480 KB
testcase_04 AC 102 ms
76,600 KB
testcase_05 AC 106 ms
80,852 KB
testcase_06 AC 101 ms
76,568 KB
testcase_07 AC 109 ms
81,216 KB
testcase_08 AC 465 ms
104,444 KB
testcase_09 AC 306 ms
104,728 KB
testcase_10 AC 450 ms
105,316 KB
testcase_11 AC 442 ms
105,456 KB
testcase_12 AC 368 ms
100,584 KB
testcase_13 AC 494 ms
106,756 KB
testcase_14 AC 374 ms
98,476 KB
testcase_15 AC 341 ms
97,832 KB
testcase_16 AC 484 ms
107,476 KB
testcase_17 AC 448 ms
106,520 KB
testcase_18 AC 310 ms
95,616 KB
testcase_19 AC 509 ms
108,496 KB
testcase_20 AC 512 ms
108,516 KB
testcase_21 AC 413 ms
106,916 KB
testcase_22 AC 492 ms
107,380 KB
testcase_23 AC 407 ms
106,164 KB
testcase_24 AC 411 ms
106,720 KB
testcase_25 AC 496 ms
107,508 KB
testcase_26 AC 101 ms
76,604 KB
testcase_27 AC 406 ms
106,136 KB
testcase_28 AC 381 ms
107,264 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