complete 4386.py 16402.py
This commit is contained in:
45
zeta_python/completed/16402.py
Normal file
45
zeta_python/completed/16402.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import sys
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
|
||||
def find(deps, country) -> int:
|
||||
a = country
|
||||
while a != deps[a]:
|
||||
a = deps[a]
|
||||
return a
|
||||
|
||||
|
||||
def union(deps, win, lose):
|
||||
rw, rl = find(deps, win), find(deps, lose)
|
||||
if rw == rl:
|
||||
if rl == lose:
|
||||
deps[rl] = win
|
||||
deps[win] = win
|
||||
else:
|
||||
deps[rl] = rw
|
||||
|
||||
|
||||
def get_sovereigns(N, deps):
|
||||
return [i for i in range(N) if i == deps[i]]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
N, M = map(int, input().split())
|
||||
name = [input().split()[2] for _ in range(N)]
|
||||
name.sort()
|
||||
name2index = {name[i]: i for i in range(N)}
|
||||
index2name = [name[i] for i in range(N)]
|
||||
|
||||
deps = [i for i in range(N)]
|
||||
|
||||
for _ in range(M):
|
||||
string_a, string_b, result = input().split(",")
|
||||
name_a, name_b = string_a.split()[2], string_b.split()[2]
|
||||
ia, ib = name2index[name_a], name2index[name_b]
|
||||
result = int(result)
|
||||
union(deps, ia, ib) if result == 1 else union(deps, ib, ia)
|
||||
|
||||
sovereigns = get_sovereigns(N, deps)
|
||||
print(len(sovereigns))
|
||||
[print(f"Kingdom of {index2name[i]}") for i in sovereigns]
|
||||
50
zeta_python/completed/4386.py
Normal file
50
zeta_python/completed/4386.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import sys
|
||||
|
||||
input = sys.stdin.readline
|
||||
|
||||
|
||||
def dist(v1: tuple, v2: tuple) -> float:
|
||||
return ((v1[0] - v2[0]) ** 2 + (v1[1] - v2[1]) ** 2) ** 0.5
|
||||
|
||||
|
||||
def find(conn, target):
|
||||
a = target
|
||||
while a != conn[a]:
|
||||
a = conn[a]
|
||||
|
||||
return a
|
||||
|
||||
|
||||
def union(conn, a, b):
|
||||
ra, rb = find(conn, a), find(conn, b)
|
||||
if ra < rb:
|
||||
conn[rb] = ra
|
||||
else:
|
||||
conn[ra] = rb
|
||||
|
||||
|
||||
def get_MST(N: int, Es: list[tuple]) -> float:
|
||||
Es.sort(key=lambda x: x[-1])
|
||||
connected = [i for i in range(N)]
|
||||
|
||||
cnt = 0
|
||||
s = 0
|
||||
for a, b, w in Es:
|
||||
if find(connected, a) != find(connected, b):
|
||||
union(connected, a, b)
|
||||
s += w
|
||||
cnt += 1
|
||||
if cnt == N - 1:
|
||||
break
|
||||
return s
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
N = int(input())
|
||||
Vs = list(tuple(map(float, input().split())) for _ in range(N))
|
||||
|
||||
available_lines = [
|
||||
(i, j, dist(Vs[i], Vs[j])) for i in range(N - 1) for j in range(i + 1, N)
|
||||
]
|
||||
|
||||
print(get_MST(N, available_lines))
|
||||
Reference in New Issue
Block a user