def dfs(x):
global child,blood_distance,bottom
if x in bottom:
return 0
elif len(child[x])==1:
blood_distance=max(dfs(child[x][0])+1,blood_distance)
return dfs(child[x][0])+1
else:
firstmax=0
secondmax=0
for i in child[x]:
deep=dfs(i)+1
if deep>firstmax:
firstmax,deep=deep,firstmax
if deep>secondmax:
secondmax=deep
blood_distance=max(firstmax+secondmax,blood_distance)
return firstmax
while 1:
x=int(input())
child={}
root=[]
for m in range(x):
root.append(m)
bottom=[]
for j in range(x):
child[j]=[]
for i in range(x-1):
k= list(map(int,input().split()))
child[k[0]].append(k[1])
for k in child:
if len(child[k])==0:
bottom.append(k)
for i in child[k]:
root.remove(i)
blood_distance=0
dfs(root[0])
print(blood_distance)
##print(child)
##print(bottom)
blood_distance=0
dfs(root[0])
print(blood_distance)
def dfs(x):
global child,blood_distance,bottom
if x in bottom:
return 0
elif len(child[x])==1:
blood_distance=max(dfs(child[x][0])+1,blood_distance)
return dfs(child[x][0])+1
else:
firstmax=0
secondmax=0
for i in child[x]:
deep=dfs(i)+1
if deep>firstmax:
firstmax,deep=deep,firstmax
if deep>secondmax:
secondmax=deep
blood_distance=max(firstmax+secondmax,blood_distance)
return firstmax
while 1:
try:
x=int(input())
child={}
root=[]
for m in range(x):
root.append(m)
bottom=[]
for j in range(x):
child[j]=[]
for i in range(x-1):
k= list(map(int,input().split()))
child[k[0]].append(k[1])
for k in child:
if len(child[k])==0:
bottom.append(k)
for i in child[k]:
root.remove(i)
blood_distance=0
dfs(root[0])
print(blood_distance)
except:
break
希望有人可以幫一下