-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1414.py
More file actions
66 lines (48 loc) · 1.11 KB
/
1414.py
File metadata and controls
66 lines (48 loc) · 1.11 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
def find_parent(parent, x):
while parent[x] != x:
x = parent[x]
return x
def union(parent, a, b):
a = find_parent(parent, a)
b = find_parent(parent, b)
if a < b:
parent[b] = a
else:
parent[a] = b
def char_to_len(char):
if char.isupper():
return (ord(char) - ord("A") + 27)
elif char.islower():
return (ord(char) - ord("a") + 1)
else:
return 0
n = int(input())
graph = []
for _ in range(n):
row = list(input())
length = []
for c in row:
length.append(char_to_len(c))
graph.append(length)
weights = []
for i in range(n):
for j in range(n):
weight = graph[i][j]
if weight != 0:
weights.append((weight, i, j))
weights.sort()
count = 0
parent = [i for i in range(n)]
line_len = sum([sum(row) for row in graph])
for w in weights:
weight, a, b = w
if find_parent(parent, a) != find_parent(parent, b):
union(parent, a, b)
count += 1
line_len -= weight
if count >= n - 1:
break
if count >= n - 1:
print(line_len)
else:
print(-1)