-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1639.py
More file actions
39 lines (31 loc) · 803 Bytes
/
1639.py
File metadata and controls
39 lines (31 loc) · 803 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# クラスカル法
class UnionFind:
def __init__(self, n):
self.n = n
self.p = [-1] * n
def leader(self, a):
while self.p[a] >= 0:
a = self.p[a]
return a
def merge(self, a, b):
x = self.leader(a)
y = self.leader(b)
if x == y: return x
if self.p[x] > self.p[y]:
x, y = y, x
self.p[x] += self.p[y]
self.p[y] = x
return x
def same(self, a, b): return self.leader(a) == self.leader(b)
def size(self, a): return -self.p[self.leader(a)]
n = int(input())
uf = UnionFind(n)
# (a, b, cost)の無向グラフ
g = [list(map(int, input().split())) for _ in range(n * (n - 1) // 2)]
g.sort(key=lambda x: x[2])
ans = 0
for i, j, cost in g:
if uf.same(i - 1, j - 1): continue
uf.merge(i - 1, j - 1)
ans = max(ans, cost)
print(ans)