-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathE.py
45 lines (41 loc) · 1023 Bytes
/
E.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
n, m = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(m):
p, q = map(int, input().split())
g[p - 1].append(q - 1)
g[q - 1].append(p - 1)
comp = [-1] * n
def shortest(root):
dist = [-1] * n
q = [0] * n
left, right = 0, 1
q[left] = root
dist[root] = 0
good = True
while left < right:
x = q[left]
left = left + 1
for i in g[x]:
if dist[i] is -1:
dist[i] = 1 + dist[x]
q[right] = i
right = right + 1
elif dist[i] == dist[x]:
good = False
far = 0
for i in dist:
if far < i:
far = i
return good, far, dist
arr = [0] * n
good = True
for i in range(n):
_, opt, dist = shortest(i)
if _ is False: good = False
if comp[i] is -1:
for j in range(n):
if dist[j] is not -1: comp[j] = i
if arr[comp[i]] < opt:
arr[comp[i]] = opt
if good is False: print('-1')
else: print(sum(arr))