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):
#1-index
import sys
input = sys.stdin.readline

from collections import deque

N, u, v = map(int, input().split())
G = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    A, B = map(int, input().split())
    G[A].append(B)
    G[B].append(A)

queT = deque()
queA = deque()

checkT = [-1] * (N + 1)
checkA = [-1] * (N + 1)

checkT[u] = 0
checkA[v] = 0

queT.append(u)
queA.append(v)
flag = True
while queT or queA: 
    tmp_lst = []
    while queT:
        tmp = queT.popleft()
        d = checkT[tmp]
        for next_ in G[tmp]:
            if checkT[next_] == -1:
                if checkA[next_] != -1:
                    flag = False
                    turn = True 
                    break
                checkT[next_] = d + 1
                tmp_lst.append(next_)
        if not flag:
            break
    if not flag:
        break
    for i in tmp_lst:
        queT.append(i)
    
    tmp_lst = []
    while queA:
        tmp = queA.popleft()
        d = checkA[tmp]
        for next_ in G[tmp]:
            if checkA[next_] == -1:
                if checkT[next_] != -1:
                    flag = False
                    turn = False 
                    break
                checkA[next_] = d + 1
                tmp_lst.append(next_)
        if not flag:
            break
    if not flag:
        break
    for i in tmp_lst:
        queA.append(i)

# print (checkT)
# print (checkA)
# print (d)
# print (turn)



ans = 0
check = [-1] * (N + 1)
check[tmp] = 0
stack = [tmp]
while stack:
    tmp1 = stack.pop()
    tmp_d = check[tmp1]
    
    for next_ in G[tmp1]:
        if check[next_] == -1 and checkA[next_] == -1:
            check[next_] = tmp_d + 1
            stack.append(next_)
            
    # if flag:
    ans = max(ans, tmp_d)

# print (check)
print (d + ans - 2)
exit()
if turn:
    print (max(d + ans, 0))
else:
    print (max(d + ans - 1, 0))

#1-index
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

from collections import deque

N, u, v = map(int, input().split())
G = [[] for _ in range(N + 1)]
for _ in range(N - 1):
    A, B = map(int, input().split())
    G[A].append(B)
    G[B].append(A)

queT = deque()
queA = deque()

checkT = [-1] * (N + 1)
checkA = [-1] * (N + 1)

checkT[u] = 0
checkA[v] = 0

queT.append(u)
queA.append(v)
flag = True
while queT or queA: 
    tmp_lst = []
    while queT:
        tmp = queT.popleft()
        d = checkT[tmp]
        for next_ in G[tmp]:
            if checkT[next_] == -1:
                if checkA[next_] != -1:
                    flag = False
                    turn = True 
                    break
                checkT[next_] = d + 1
                tmp_lst.append(next_)
        if not flag:
            break
    if not flag:
        break
    for i in tmp_lst:
        queT.append(i)
    
    tmp_lst = []
    while queA:
        tmp = queA.popleft()
        d = checkA[tmp]
        for next_ in G[tmp]:
            if checkA[next_] == -1:
                if checkT[next_] != -1:
                    flag = False
                    turn = False 
                    break
                checkA[next_] = d + 1
                tmp_lst.append(next_)
        if not flag:
            break
    if not flag:
        break
    for i in tmp_lst:
        queA.append(i)

# print (checkT)
# print (checkA)
# print (d)
# print (turn)



if turn:
    ans = 0
    check = [-1] * (N + 1)
    check[tmp] = 0
    stack = [tmp]
    while stack:
        tmp1 = stack.pop()
        tmp_d = check[tmp1]
        
        for next_ in G[tmp1]:
            if check[next_] == -1 and checkA[next_] == -1:
                check[next_] = tmp_d + 1
                stack.append(next_)
                
        # if flag:
        ans = max(ans, tmp_d)

else:
    ans = 0
    check = [-1] * (N + 1)
    check[tmp] = 0
    stack = [tmp]

    tmp1 = stack.pop()
    tmp_d = check[tmp1]
    
    for next_ in G[tmp1]:
        if check[next_] == -1 and checkT[next_] != -1:
            check[next_] = tmp_d + 1
            stack.append(next_)
            
    # if flag:
    ans = max(ans, tmp_d)

    while stack:
        tmp1 = stack.pop()
        tmp_d = check[tmp1]
        
        for next_ in G[tmp1]:
            if check[next_] == -1 and checkA[next_] == -1:
                check[next_] = tmp_d + 1
                stack.append(next_)
                
        # if flag:
        ans = max(ans, tmp_d)    


# print (check)
if turn:
    print (max(d + ans, 0))
else:
    print (max(d + ans - 1, 0))

Verdicts: Wrong Answer

Accepted