-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask7.py
More file actions
46 lines (40 loc) · 1.11 KB
/
Copy pathtask7.py
File metadata and controls
46 lines (40 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
#task7
task7_input = open('input7_1.txt', "r")
task7_output = open('output7_1.txt', "w")
N = int(task7_input.readline().strip())
edges = []
for i in range(N-1):
u, v = [int(x) for x in task7_input.readline().strip().split()]
edges.append((u, v))
print(edges)
print(N)
adj = [[] for i in range(N+1)] # 1-based indexing
for u, v in edges:
adj[u].append(v)
adj[v].append(u)
def bfs(start):
visited = [0] * (N+1)
dist = [0] * (N+1)
queue = [start]
visited[start] = 1
front = 0
while front < len(queue):
node = queue[front]
front += 1
for neighbor in adj[node]:
if not visited[neighbor]:
visited[neighbor] = 1
dist[neighbor] = dist[node] + 1
queue.append(neighbor)
max_dist = 0
farthest = start
for i in range(1, N+1):
if dist[i] > max_dist:
max_dist = dist[i]
farthest = i
return farthest, max_dist
u, dist = bfs(1)
v, length = bfs(u)
task7_output.write(f"{u} {v}\n")
task7_input.close()
task7_output.close()