Problem: We have a tree with N vertices. The i-th edge connects Vertex A_i and B_i bidirectionally.  Takahashi is standing at Vertex u, and Aoki is standing at Vertex v.  Now, they will play a game of tag as follows:    * 1. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Takahashi moves to a vertex of his choice that is adjacent to his current vertex.    * 2. If Takahashi and Aoki are standing at the same vertex, the game ends. Otherwise, Aoki moves to a vertex of his choice that is adjacent to his current vertex.    * 3. Go back to step 1.  Takahashi performs his moves so that the game ends as late as possible, while Aoki performs his moves so that the game ends as early as possible.  Find the number of moves Aoki will perform before the end of the game if both Takahashi and Aoki know each other's position and strategy.  It can be proved that the game is bound to end.

Submission attempts (in order):
from collections import deque
n,u,v=map(int,input().split())
u-=1
v-=1
g=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
q=deque([[v,0,0]])
x=[0]*n
x[v]=1
ans=0
while q:
    p=q.popleft()
    for i in g[p[0]]:
        if x[i]==0:
            x[i]=1
            if i==u:
                q.append([i,p[1]+1,1])
                ans=max(ans,p[1]+1)
            else:
                q.append([i,p[1]+1,p[2]])
                if p[2]==1:
                    ans=max(ans,p[1]+1)
print(ans)
   
  
  
exit()
uv=f
if len(uv)%2==0:
    nu=uv[len(uv)//2-1]
    ans=len(uv)//2-1
else:
    nu=uv[len(uv)//2-1]
    ans=len(uv)//2-1
y=[0]*n
y[len(uv)//2]=1
y[nu]=1
q=deque([[nu,0]])
z=0
while q:
    p=q.popleft()
    for i in g[p[0]]:
        if y[i]==0:
            y[i]=1
            q.append([i,p[1]+1])
            z=max(z,p[1]+1)
print(ans+z)


from collections import deque
n,u,v=map(int,input().split())
u-=1
v-=1
g=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
q=deque([[u,[u]]])
x=[0]*n
x[u]=1
f=[]
while q:
    p=q.popleft()
    for i in g[p[0]]:
        if i==v:
            f=p[1]+[v]
            break
        if x[i]==0:
            x[i]=1
            q.append([i,p[1]+[i]])
    if len(f)>0:
        break
uv=f
if len(uv)%2==0:
    nu=uv[len(uv)//2-1]
    ans=len(uv)//2-1
else:
    nu=uv[len(uv)//2-1]
    ans=len(uv)//2-1
exit()
y=[0]*n
y[len(uv)//2]=1
y[nu]=1
q=deque([[nu,0]])
z=0
while q:
    p=q.popleft()
    for i in g[p[0]]:
        if y[i]==0:
            y[i]=1
            q.append([i,p[1]+1])
            z=max(z,p[1]+1)
print(ans+z)


from collections import deque
n,u,v=map(int,input().split())
u-=1
v-=1
g=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
q=deque([[v,0,0]])
x=[0]*n
x[v]=1
ans=0
while q:
    p=q.popleft()
    for i in g[p[0]]:
        if x[i]==0:
            x[i]=1
            if i==u:
                q.append([i,p[1]+1,1])
                ans=max(ans,p[1]+1)
            else:
                q.append([i,p[1]+1,p[2]])
                if p[2]==1:
                    ans=max(ans,p[1]+1)
    print(ans)
   
  
  
exit()
uv=f
if len(uv)%2==0:
    nu=uv[len(uv)//2-1]
    ans=len(uv)//2-1
else:
    nu=uv[len(uv)//2-1]
    ans=len(uv)//2-1
y=[0]*n
y[len(uv)//2]=1
y[nu]=1
q=deque([[nu,0]])
z=0
while q:
    p=q.popleft()
    for i in g[p[0]]:
        if y[i]==0:
            y[i]=1
            q.append([i,p[1]+1])
            z=max(z,p[1]+1)
print(ans+z)


from collections import deque
n,u,v=map(int,input().split())
u-=1
v-=1
g=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int,input().split())
    a-=1
    b-=1
    g[a].append(b)
    g[b].append(a)
q=deque([[u,[]]])
x=[0]*n
x[u]=1
f=[]
while q:
    p=q.popleft()
    for i in g[p[0]]:
        if i==v:
            f=p[1]+[v]
            break
        if x[i]==0:
            x[i]=1
            q.append([i,p[1]+[i]])
    if len(f)>0:
        break
uv=f
if len(uv)%2==0:
    nu=uv[len(uv)//2-1]
    ans=len(uv)//2-1
else:
    nu=uv[len(uv)//2-1]
    ans=len(uv)//2-1
y=[0]*n
y[len(uv)//2]=1
y[nu]=1
q=deque([[nu,0]])
z=0
while q:
    p=q.popleft()
    for i in g[p[0]]:
        if y[i]==0:
            y[i]=1
            q.append([i,p[1]+1])
            z=max(z,p[1]+1)
print(ans+z)


N, U, V = map(int, input().split())
A = [[] for _ in range(N)]
import sys
sys.setrecursionlimit(10**8)
for _ in range(N - 1):
    a, b = map(int, input().split())

    A[a - 1].append(b - 1)
    A[b - 1].append(a - 1)

C = [0] * N
D = [0] * N

def DFS(x, pre, cnt, C):
    C[x] = cnt
    cnt += 1
    for a in A[x]:
        if a == pre:
            continue

        DFS(a, x, cnt, C)


DFS(V - 1, -1, 0, C)
DFS(U - 1, -1, 0, D)
for i in range(N):
    if C[i]>D[i]:
        ans =max(ans, C[i]-1)
print(ans)


N, U, V = map(int, input().split())
A = [[] for _ in range(N)]
import sys
sys.setrecursionlimit(10**8)
for _ in range(N - 1):
    a, b = map(int, input().split())

    A[a - 1].append(b - 1)
    A[b - 1].append(a - 1)

C = [0] * N
D = [0] * N

def DFS(x, pre, cnt, C):
    C[x] = cnt
    cnt += 1
    for a in A[x]:
        if a == pre:
            continue

        DFS(a, x, cnt, C)


DFS(V - 1, -1, 0, C)
DFS(U - 1, -1, 0, D)
ans=0
for i in range(N):
    if C[i]>D[i]:
        ans =max(ans, C[i]-1)
print(ans)


Verdicts: Wrong Answer

Wrong Answer

Wrong Answer

Wrong Answer

Runtime Error

Accepted