-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaek1967.py
28 lines (25 loc) · 967 Bytes
/
baek1967.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
from sys import setrecursionlimit
setrecursionlimit(20000)
N = int(input())
adjList = []
for _ in range(N):
adjList.append([])
for _ in range(N-1):
parent, child, length = map(int, input().split(" "))
adjList[parent-1].append((child-1, length))
adjList[child-1].append((parent-1, length))
def farthestFind(adjList, visited, now = 0, totalLength = 0):
visited[now] = True
farthestChild = now
farthestLength = totalLength
for childInfo in adjList[now]:
child, length = childInfo
if not visited[child]:
farChild, farLength = farthestFind(adjList, visited, child, totalLength+length)
if farthestLength < farLength:
farthestLength = farLength
farthestChild = farChild
return farthestChild, farthestLength
firstFarChild, length = farthestFind(adjList, [False] * N)
secondFarChild, treeRadius = farthestFind(adjList, [False] * N, firstFarChild)
print(treeRadius)