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 typing import List

n, u, v = map(int, input().split())
graph = [[] for _ in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

def dfs(v: int) -> List[int]:
    dist = [-1] * n
    dist[v] = 0
    stack = [v]
    while stack:
        now = stack.pop()
        dw = dist[now] + 1
        for ne in graph[now]:
            if dist[ne] < 0:
                continue
            dist[ne] = dw
            stack.append(ne)
    return dist

Takahashi = dfs(u-1)
Aoki = dfs(v-1)

ans = 0
for t, a in zip(Takahashi, Aoki):
    if t < a:
        ans = max(ans, a - 1)

print(ans)

from typing import List

n, u, v = map(int, input().split())
graph = [[] for _ in range(n)]
for i in range(n):
    a, b = map(int, input().split())
    graph[a-1].append(b-1)
    graph[b-1].append(a-1)

def dfs(v: int) -> List[int]:
    dist = [-1] * n
    dist[v] = 0
    stack = [v]
    while stack:
        now = stack.pop()
        dw = dist[now] + 1
        for ne in graph[now]:
            if dist[ne] > 0:
                continue
            dist[ne] = dw
            stack.append(ne)
    return dist

Takahashi = dfs(u-1)
Aoki = dfs(v-1)

ans = 0
for t, a in zip(Takahashi, Aoki):
    if t < a:
        ans = max(ans, a - 1)

print(ans)

from typing import List

n, u, v = map(int, input().split())
graph = [[] for _ in range(n+1)]
for i in range(n):
    a, b = map(int, input().split())
    graph[a].append(b)
    graph[b].append(a)

def dfs(v: int) -> List[int]:
    dist = [-1] * n+1
    dist[v] = 0
    stack = [v]
    while stack:
        now = stack.pop()
        dw = dist[now] + 1
        for ne in graph[now]:
            if dist[ne] > 0:
                continue
            dist[ne] = dw
            stack.append(ne)
    return dist

Takahashi = dfs(u)
Aoki = dfs(v)

ans = 0
for t, a in zip(Takahashi, Aoki):
    if t < a:
        ans = max(ans, a - 1)

print(ans)

import sys

n, u, v = map(int, sys.stdin.readline().split())
graph = [[] for _ in range(n+1)]
for i in range(n-1):
    a, b = map(int, sys.stdin.readline().split())
    graph[a].append(b)
    graph[b].append(a)

def dfs(v):
    dist = [-1] * (n+1)
    dist[v] = 0
    stack = [v]
    while stack:
        now = stack.pop()
        dw = dist[now] + 1
        for ne in graph[now]:
            if dist[ne] >= 0:
                continue
            dist[ne] = dw
            stack.append(ne)
    return dist

Takahashi = dfs(u)
Aoki = dfs(v)

ans = 0
for t, a in zip(Takahashi[1:], Aoki[1:]):
    if t < a:
        ans = max(ans, a - 1)

print(ans)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Accepted