init
This commit is contained in:
commit
851751ea87
82 changed files with 2093 additions and 0 deletions
32
4B.py
Normal file
32
4B.py
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import sys
|
||||
|
||||
data = sys.stdin.read().split()
|
||||
n = int(data[0])
|
||||
m = int(data[1])
|
||||
edges = []
|
||||
idx = 2
|
||||
for _ in range(m):
|
||||
edges.append((int(data[idx+2]), int(data[idx]), int(data[idx+1])))
|
||||
idx += 3
|
||||
edges.sort()
|
||||
p = list(range(n + 1))
|
||||
def find(i):
|
||||
r = i
|
||||
while r != p[r]:
|
||||
r = p[r]
|
||||
c = i
|
||||
while c != r:
|
||||
p[c], c = r, p[c]
|
||||
return r
|
||||
ans = 0
|
||||
cnt = 0
|
||||
for w, u, v in edges:
|
||||
ru = find(u)
|
||||
rv = find(v)
|
||||
if ru != rv:
|
||||
p[ru] = rv
|
||||
ans += w
|
||||
cnt += 1
|
||||
if cnt == n - 1:
|
||||
break
|
||||
print(ans)
|
||||
Loading…
Add table
Add a link
Reference in a new issue