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
def main():
    N, u, v = [int(i) for i in input().split()]
    u -= 1
    v -= 1
    graph = [[] for _ in range(N)]
    for i in range(N-1):
        a,b = [int(i) for i in input().split()]
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)
    tree = [[] for _ in range(N)]
    parents = [-1] * N
    
    stack = deque([(v, -1)])
    while len(stack) > 0:
        current, parent = stack.pop()
        parents[current] = parent
        for n in graph[current]:
            if parent == n:
                continue
            stack.append((n, current))
            tree[current].append(n)
    
    current = u
    distance = 0
    while current != v:
        current = parents[current]
        distance += 1
    current = u
    cnt = 0
    res = 0
    print(distance)
    from_n = -1
    while distance != 0:
        # print("checking %d" % current)
        stack = deque([(current, 0)])
        cnmax = 0
        # print(current)
        while len(stack) > 0:
            root, cn = stack.pop()
            cnmax = max(cn, cnmax)
            for c in tree[root]:
                if(c != from_n):
                    stack.append((c, cn+1))
            # print(cn)
        # print("---")
        res = max(res, cnmax + ((distance+1) // 2))
        from_n = current
        current = parents[current]
        if current == -1:
            break
        cnt += 1
        # res = max(res, cnmax + cnt)
        distance -= 2
    print(res)
        
main()

from collections import deque
def main():
    N, u, v = [int(i) for i in input().split()]
    u -= 1
    v -= 1
    graph = [[] for _ in range(N)]
    for i in range(N-1):
        a,b = [int(i) for i in input().split()]
        a -= 1
        b -= 1
        graph[a].append(b)
        graph[b].append(a)
    atree, aparents, adistances = maketree(v, graph, N)
    ttree, tparents, tdistances = maketree(u, graph, N)
    res = 0
    # print(adistances)
    # print(tdistances)
    for i in range(N):
        a = adistances[i]
        t = tdistances[i]
        if(a > t):
            res = max(res, a - 1)
    print(res)

def maketree(v, graph, N):
    tree = [[] for _ in range(N)]
    parents = [-1] * N
    distances = [0]*N
    
    stack = deque([(v, -1, 0)])
    while len(stack) > 0:
        current, parent, distance = stack.pop()
        distances[current] = distance
        parents[current] = parent
        for n in graph[current]:
            if parent == n:
                continue
            stack.append((n, current, distance+1))
            tree[current].append(n)
    return tree, parents, distances
    
main()

Verdicts: Wrong Answer

Accepted