結果

問題 No.2072 Anatomy
ユーザー shobonvip
提出日時 2022-09-16 22:35:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 405 ms / 2,000 ms
コード長 798 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 92,808 KB
最終ジャッジ日時 2024-12-21 21:50:55
合計ジャッジ時間 7,530 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindWithDp:
	def __init__(self, n):
		self.n = n
		self.dp = [0] * n
		self.parents = [-1] * n
	
	def find(self, x):
		if self.parents[x] < 0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]
	
	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)
		if x == y:
			self.dp[x] += 1
			return
		if self.parents[x] > self.parents[y]:
			x, y = y, x
		self.parents[x] += self.parents[y]
		self.dp[x] = max(self.dp[x], self.dp[y]) + 1
		self.parents[y] = x

n, m = map(int,input().split())
edges = []
for i in range(m):
	u, v = map(int,input().split())
	u -= 1
	v -= 1
	edges.append((u, v))

uf = UnionFindWithDp(n)
for i in range(m-1,-1,-1):
	u, v = edges[i]
	uf.union(u, v)

ans = 0
for i in range(n):
	ans = max(ans, uf.dp[i])
print(ans)
0