-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathplague-jr.py
More file actions
30 lines (26 loc) · 805 Bytes
/
plague-jr.py
File metadata and controls
30 lines (26 loc) · 805 Bytes
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
from collections import deque
def simulate(node, edges, current_min):
queue = deque([(node, 0)])
visited = {node}
while queue:
node, night = queue.popleft()
if night >= current_min: return current_min
for o in edges[node]:
if o not in visited:
queue.append((o, night + 1))
visited.add(o)
return night
def solution():
num_rods = int(input())
edges = {}
for _ in range(num_rods):
a, b = map(int, input().split())
if a not in edges: edges[a] = []
if b not in edges: edges[b] = []
edges[a].append(b)
edges[b].append(a)
current_min = float('inf')
for node in edges:
current_min = simulate(node, edges, current_min)
print(current_min)
solution()