結果
問題 | No.2072 Anatomy |
ユーザー |
![]() |
提出日時 | 2022-09-16 22:02:17 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 355 ms / 2,000 ms |
コード長 | 1,389 bytes |
コンパイル時間 | 312 ms |
コンパイル使用メモリ | 82,232 KB |
実行使用メモリ | 101,888 KB |
最終ジャッジ日時 | 2024-12-21 20:16:55 |
合計ジャッジ時間 | 8,138 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 27 |
ソースコード
import sysinput = sys.stdin.readlinefrom collections import *class Unionfind:def __init__(self, n):self.par = [-1]*nself.rank = [1]*ndef root(self, x):r = xwhile not self.par[r]<0:r = self.par[r]t = xwhile t!=r:tmp = tt = self.par[t]self.par[tmp] = rreturn rdef unite(self, x, y):rx = self.root(x)ry = self.root(y)if rx==ry:returnif self.rank[rx]<=self.rank[ry]:self.par[ry] += self.par[rx]self.par[rx] = ryif self.rank[rx]==self.rank[ry]:self.rank[ry] += 1else:self.par[rx] += self.par[ry]self.par[ry] = rxdef 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]*Nuf = Unionfind(N)for u, v in edges[::-1]:u -= 1v -= 1if uf.is_same(u, v):dep[uf.root(u)] += 1else:ru = uf.root(u)rv = uf.root(v)uf.unite(u, v)dep[uf.root(u)] = max(dep[ru], dep[rv])+1print(dep[uf.root(0)])