-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathseorim.py
49 lines (34 loc) · 1020 Bytes
/
seorim.py
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
40
41
42
43
44
45
46
47
48
49
import sys
input = sys.stdin.readline
class UnionFind:
parent = []
def __init__(self, size):
for i in range(size+1):
self.parent.append(i)
def find(self, x):
while x != self.parent[x]:
self.parent[x] = self.parent[self.parent[x]]
x = self.parent[x]
return x
def union(self, u, v):
u_root = self.find(u)
v_root = self.find(v)
if u_root < v_root:
self.parent[v_root] = u_root
else:
self.parent[u_root] = v_root
def is_connected(self, u, v):
return self.find(u) == self.find(v)
n, m = map(int, input().split())
graph = [list(map(int, input().split())) for _ in range(m)]
graph.sort(key=lambda x: x[2])
uf = UnionFind(n)
cnt = 0
total = 0
for u, v, cost in graph:
if cnt >= n-2: break
if not uf.is_connected(u, v):
uf.union(u, v)
cnt += 1
total += cost
print(total)