AtCoder Beginner Contest 148 - Playing Tag on Tree

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.

-----Constraints-----
 - 2 \leq N \leq 10^5
 - 1 \leq u,v \leq N
 - u \neq v
 - 1 \leq A_i,B_i \leq N
 - The given graph is a tree.

-----Input-----
Input is given from Standard Input in the following format:
N u v
A_1 B_1
:
A_{N-1} B_{N-1}

-----Output-----
Print the number of moves Aoki will perform before the end of the game.

-----Sample Input-----
5 4 1
1 2
2 3
3 4
3 5

-----Sample Output-----
2

If both players play optimally, the game will progress as follows:
 - Takahashi moves to Vertex 3.
 - Aoki moves to Vertex 2.
 - Takahashi moves to Vertex 5.
 - Aoki moves to Vertex 3.
 - Takahashi moves to Vertex 3.
Here, Aoki performs two moves.
Note that, in each move, it is prohibited to stay at the current vertex.

Difficulty: MEDIUM

Starter code:

Solutions:
from sys import exit
(N, u, v) = map(int, input().split())
if u == v:
	print(0)
	exit()
edges = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(A, B) = map(int, input().split())
	edges[A].append(B)
	edges[B].append(A)

def calDist(start, edges):
	dest = [-1] * (N + 1)
	dest[start] = 0
	q = [start]
	while len(q) != 0:
		current = q.pop()
		for n in edges[current]:
			if dest[n] != -1:
				continue
			dest[n] = dest[current] + 1
			q.append(n)
	return dest
T = calDist(u, edges)
A = calDist(v, edges)
result = 0
for i in range(1, N + 1):
	A_i = A[i]
	if T[i] >= A_i:
		continue
	if A_i > result:
		result = A_i
print(result - 1)


import sys
sys.setrecursionlimit(10 ** 6)
(N, u, v) = map(int, input().split())
edges = [list(map(int, input().split())) for _ in range(N - 1)]
E = [[] for _ in range(N + 1)]
for (a, b) in edges:
	E[a].append(b)
	E[b].append(a)
depth = [[-1, -1] for _ in range(N + 1)]
depth[v][0] = 0
depth[u][1] = 0

def dfs1(E, v):
	vv = E[v]
	for vvv in vv:
		if depth[vvv][0] == -1:
			depth[vvv][0] = depth[v][0] + 1
			dfs1(E, vvv)

def dfs2(E, v):
	vv = E[v]
	for vvv in vv:
		if depth[vvv][1] == -1:
			depth[vvv][1] = depth[v][1] + 1
			dfs2(E, vvv)
dfs1(E, v)
dfs2(E, u)
depth = sorted(depth, reverse=True)
idx = 0
while True:
	if depth[idx][0] - depth[idx][1] >= 1:
		print(depth[idx][0] - 1)
		return
	else:
		idx += 1


from sys import setrecursionlimit
setrecursionlimit(10 ** 5)
(n, u, v) = map(int, input().split())
ab = [tuple(map(lambda x: int(x) - 1, input().split())) for _ in range(n - 1)]
u -= 1
v -= 1
e = [[] for _ in range(n)]
for (a, b) in ab:
	e[a].append(b)
	e[b].append(a)
t = [0] * n
a = [0] * n

def dfs(x, p, d, c):
	if len(e[x]) == 1:
		if e[x][0] == p:
			return
	for y in e[x]:
		if y == p:
			continue
		if c == 0:
			t[y] = d
		else:
			a[y] = d
		dfs(y, x, d + 1, c)
dfs(u, -1, 1, 0)
dfs(v, -1, 1, 1)
ans = 0
for i in range(n):
	if t[i] < a[i]:
		ans = max(a[i], ans)
print(ans - 1)


import sys
from collections import deque

def main():
	(N, p, q, *AB) = list(map(int, sys.stdin.buffer.read().split()))
	p -= 1
	q -= 1
	G = [[] for _ in range(N)]
	for (a, b) in zip(*[iter(AB)] * 2):
		G[a - 1].append(b - 1)
		G[b - 1].append(a - 1)
	if len(G[p]) == 1 and G[p][0] == q:
		print(0)
		return
	dist1 = [-1] * N
	dist1[p] = 0
	queue = deque([p])
	while queue:
		v = queue.popleft()
		for nv in G[v]:
			if dist1[nv] == -1:
				dist1[nv] = dist1[v] + 1
				queue.append(nv)
	dist2 = [-1] * N
	dist2[q] = 0
	queue = deque([q])
	while queue:
		v = queue.popleft()
		for nv in G[v]:
			if dist2[nv] == -1:
				dist2[nv] = dist2[v] + 1
				queue.append(nv)
	max_d = 0
	for (d1, d2) in zip(dist1, dist2):
		if d1 < d2 and max_d < d2:
			max_d = d2
	print(max_d - 1)
	return

def __starting_point():
	main()
__starting_point()


import sys
sys.setrecursionlimit(10 ** 7)
(N, T, A) = map(int, input().split())
INF = float('inf')
T -= 1
A -= 1
G = [[] for _ 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)
disT = [INF for _ in range(N)]
disT[T] = 0
disA = [INF for _ in range(N)]
disA[A] = 0

def dfs(v, L):
	for u in G[v]:
		if L[u] != INF:
			continue
		L[u] = L[v] + 1
		dfs(u, L)
dfs(T, disT)
dfs(A, disA)
ans = 0
for i in range(N):
	if disT[i] < disA[i]:
		ans = max(ans, disA[i] - 1)
print(ans)


from collections import deque

def BFS(t):
	tree = [[] for i in range(n)]
	for i in list:
		tree[i[0] - 1].append(i[1] - 1)
		tree[i[1] - 1].append(i[0] - 1)
	dist = [-1 for i in range(n)]
	dist[t - 1] = 0
	que = deque()
	que.append(t - 1)
	while que:
		x = que.popleft()
		for p in tree[x]:
			if dist[p] == -1:
				que.append(p)
				dist[p] = dist[x] + 1
	return dist
(n, u, v) = map(int, input().split())
list = [list(map(int, input().split())) for i in range(n - 1)]
u_dist = BFS(u)
v_dist = BFS(v)
l = 0
for (u_d, v_d) in zip(u_dist, v_dist):
	if u_d < v_d:
		l = max(l, v_d)
print(l - 1)


import sys
sys.setrecursionlimit(10 ** 6)
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
edges = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	edges[a].append(b)
	edges[b].append(a)
dist = [0] * N

def dfs(v, d=0, p=-1):
	global dist
	dist[v] = d
	for u in edges[v]:
		if u == p:
			continue
		dfs(u, d + 1, v)

def calc_dist(s):
	global dist
	dist = [0] * N
	dfs(s)
	return dist
distS = calc_dist(u)
distT = calc_dist(v)
mx = 0
for i in range(N):
	if distS[i] < distT[i]:
		mx = max(mx, distT[i])
ans = mx - 1
print(ans)


import sys
sys.setrecursionlimit(10 ** 5 + 1)

def dfsu(s, p):
	for t in g[s]:
		if t == p:
			continue
		du[t] = du[s] + 1
		dfsu(t, s)

def dfsv(s, p):
	for t in g[s]:
		if t == p:
			continue
		dv[t] = dv[s] + 1
		dfsv(t, s)
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(lambda x: int(x) - 1, input().split())
	g[a].append(b)
	g[b].append(a)
du = [0] * n
dv = [0] * n
dfsu(u, -1)
dfsv(v, -1)
mx = 0
for i in range(n):
	if du[i] < dv[i]:
		mx = max(mx, dv[i])
print(mx - 1)


import sys
sys.setrecursionlimit(10 ** 8)
input = sys.stdin.readline
(n, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
graph = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	graph[a].append(b)
	graph[b].append(a)

def dfs(v, d, count):
	count[v] = d
	for v_next in graph[v]:
		if count[v_next] >= 0:
			continue
		dfs(v_next, d + 1, count)
count_tak = [-1] * n
dfs(u, 0, count_tak)
count_aok = [-1] * n
dfs(v, 0, count_aok)
ans = 0
for i in range(n):
	if count_tak[i] < count_aok[i]:
		ans = max(ans, count_aok[i] - 1)
print(ans)


def dfs(c, lst):
	n = len(lst)
	ans = [0] * n
	stack = [c - 1]
	check = [0] * n
	while stack != []:
		d = stack.pop()
		if check[d] == 0:
			check[d] = 1
			for i in lst[d]:
				if check[i] == 0:
					stack.append(i)
					ans[i] = ans[d] + 1
	return ans
import sys
input = sys.stdin.readline
(N, u, v) = map(int, input().split())
ki = [[] for f in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	ki[a - 1].append(b - 1)
	ki[b - 1].append(a - 1)
U = dfs(u, ki)
V = dfs(v, ki)
ans = 0
for i in range(N):
	if V[i] > U[i]:
		ans = max(ans, V[i] - 1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 7)

def dfs(v, d=0, p=-1):
	global dist
	dist[v] = d
	for u in to[v]:
		if u == p:
			continue
		dfs(u, d + 1, v)

def calcDist(s):
	global dist
	dist = [0 for _ in range(N)]
	dfs(s)
	return dist
(N, s, t) = map(int, input().split())
s -= 1
t -= 1
to = [[] for _ in range(N)]
dist = [0 for _ in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	to[a].append(b)
	to[b].append(a)
distS = calcDist(s)
distT = calcDist(t)
mx = 0
for i in range(N):
	if distS[i] < distT[i]:
		mx = max(mx, distT[i])
ans = mx - 1
print(ans)


import sys
sys.setrecursionlimit(1000000)

def dfs_a(N, v, adjlist, cur):
	nonlocal depth_a
	depth_a[v] = cur
	for node in adjlist[v]:
		if depth_a[node] < 0:
			dfs_a(N, node, adjlist, cur + 1)

def dfs_t(N, v, adjlist, cur):
	nonlocal depth_t
	depth_t[v] = cur
	for node in adjlist[v]:
		if depth_t[node] < 0:
			dfs_t(N, node, adjlist, cur + 1)
from collections import deque
(N, u, v) = list(map(int, input().split()))
adjlist = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = list(map(int, input().split()))
	adjlist[a - 1].append(b - 1)
	adjlist[b - 1].append(a - 1)
depth_a = [-1] * N
depth_t = [-1] * N
dfs_a(N, v - 1, adjlist, 0)
dfs_t(N, u - 1, adjlist, 0)
M = 0
for i in range(N):
	if depth_a[i] > depth_t[i]:
		curm = depth_a[i] - 1
		if curm > M:
			M = curm
print(M)


from collections import deque
(N, u, v) = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N - 1)]
graph = [[] for i in range(N + 1)]
for (i, j) in AB:
	graph[i].append(j)
	graph[j].append(i)
r = [-1 for i in range(N + 1)]
Q = deque()
Q.append(u)
r[u] = 0
while Q:
	x = Q.pop()
	for to in graph[x]:
		if r[to] == -1:
			r[to] = r[x] + 1
			Q.append(to)
c = [-1 for i in range(N + 1)]
Q = deque()
Q.append(v)
c[v] = 0
while Q:
	x = Q.pop()
	for to in graph[x]:
		if c[to] == -1:
			c[to] = c[x] + 1
			Q.append(to)
ans = 0
for i in range(N + 1):
	if r[i] < c[i]:
		ans = max(ans, c[i])
print(max(0, ans - 1))


f = lambda : map(int, input().split())
(n, st, sa) = f()
g = [set() for _ in range(n)]
for _ in range(n - 1):
	(a, b) = f()
	g[a - 1].add(b - 1)
	g[b - 1].add(a - 1)

def bfs(s):
	l = [-1] * n
	l[s] = 0
	q = [s]
	while q:
		v = q.pop()
		d = l[v] + 1
		for c in g[v]:
			if l[c] < 0:
				l[c] = d
				q += [c]
	return l
lt = bfs(st - 1)
la = bfs(sa - 1)
print(max((la[i] for i in range(n) if lt[i] < la[i])) - 1)


(n, u, v) = map(int, input().split())
u -= 1
v -= 1
path = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = [int(x) - 1 for x in input().split()]
	path[a].append(b)
	path[b].append(a)
parent = [-1] * n
rank = [-1] * n
que = []
que.append(v)
rank[v] = 0
while que:
	cur = que.pop()
	for nxt in path[cur]:
		if rank[nxt] == -1:
			rank[nxt] = rank[cur] + 1
			parent[nxt] = cur
			que.append(nxt)
up = (rank[u] - 1) // 2
upto = u
for i in range(up):
	upto = parent[upto]
depth = 0
que = [upto]
while que:
	cur = que.pop()
	if len(path[cur]) == 1:
		depth = max(depth, rank[cur])
	else:
		for nxt in path[cur]:
			if nxt != parent[cur]:
				que.append(nxt)
print(depth - 1)


from collections import deque
(N, u, v) = map(int, input().split())
edges = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	edges[a].append(b)
	edges[b].append(a)
distA = {}
que = deque([(u, 0)])
while que:
	(n, step) = que.popleft()
	if n not in distA:
		distA[n] = step
		for nx in edges[n]:
			que.append((nx, step + 1))
distB = {}
que = deque([(v, 0)])
while que:
	(n, step) = que.popleft()
	if n not in distB:
		distB[n] = step
		for nx in edges[n]:
			que.append((nx, step + 1))
ans = 0
for i in range(1, N + 1):
	dist = distB[i] - distA[i]
	if dist > 0:
		ans = max(ans, distB[i] - 1)
print(ans)


from collections import deque

def main():
	(N, u, v) = map(int, input().split())
	s = u - 1
	t = v - 1
	adj = [[] for _ in range(N)]
	for _ in range(N - 1):
		(a, b) = map(lambda x: int(x) - 1, input().split())
		adj[a].append(b)
		adj[b].append(a)
	queue = deque([t])
	visit = [-1] * N
	visit[t] = 0
	while queue:
		now = queue.popleft()
		for u in adj[now]:
			if visit[u] < 0:
				queue.append(u)
				visit[u] = visit[now] + 1
	queue = deque([s])
	visit2 = [-1] * N
	visit2[s] = 0
	while queue:
		now = queue.popleft()
		for u in adj[now]:
			if visit2[u] < 0:
				queue.append(u)
				visit2[u] = visit2[now] + 1
	ans = [-1, -1]
	for i in range(N):
		if visit[i] >= visit2[i] and visit[i] > ans[0]:
			ans = [visit[i], i]
	print(visit[ans[1]] - 1)

def __starting_point():
	main()
__starting_point()


import os
import sys
import numpy as np

def solve(N, U, V, AB):
	G = [[0] * 0 for _ in range(N + 1)]
	for idx_ab in range(len(AB)):
		(a, b) = AB[idx_ab]
		G[a].append(b)
		G[b].append(a)
	P1 = np.zeros(N + 1, dtype=np.int64)

	def dfs1(u):
		st = [u]
		while st:
			v = st.pop()
			p = P1[v]
			for u in G[v]:
				if p != u:
					st.append(u)
					P1[u] = v
	dfs1(U)
	path_u2v = [U]
	v = V
	while v != U:
		v = P1[v]
		path_u2v.append(v)
	path_u2v.reverse()
	l = len(path_u2v)
	half = (l - 2) // 2
	c = path_u2v[half]
	ng = path_u2v[half + 1]
	Depth = np.zeros(N + 1, dtype=np.int64)

	def dfs2(c):
		st = [c]
		P = np.zeros(N + 1, dtype=np.int64)
		while st:
			v = st.pop()
			p = P[v]
			d = Depth[v]
			for u in G[v]:
				if p != u and u != ng:
					st.append(u)
					P[u] = v
					Depth[u] = d + 1
	dfs2(c)
	c_ = path_u2v[l - 1 - half]
	v = c_
	while v != c:
		Depth[v] = 0
		v = P1[v]
	d = l % 2
	ans = max(Depth) + half + d
	return ans
numba_config = [[solve, 'i8(i8,i8,i8,i8[:,:])']]
if sys.argv[-1] == 'ONLINE_JUDGE':
	from numba import njit
	from numba.pycc import CC
	cc = CC('my_module')
	for (func, signature) in numba_config:
		vars()[func.__name__] = njit(signature)(func)
		cc.export(func.__name__, signature)(func)
	cc.compile()
	return
elif os.name == 'posix':
	exec(f"from my_module import {','.join((func.__name__ for (func, _) in numba_config))}")
else:
	from numba import njit
	for (func, signature) in numba_config:
		vars()[func.__name__] = njit(signature, cache=True)(func)
	print('compiled!', file=sys.stderr)

def main():
	(N, u, v) = map(int, input().split())
	if N == 2:
		print(0)
		return
	AB = np.array(sys.stdin.read().split(), dtype=np.int64).reshape(N - 1, 2)
	ans = solve(N, u, v, AB)
	print(ans)
main()


import sys
sys.setrecursionlimit(10 ** 6)
(n, a, b) = list(map(int, input().split()))
D = [[0] * 2 for _ in range(101010)]
E = [[] for _ in range(n)]

def dfs(cur, pre, d, id):
	D[cur][id] = d
	for nv in E[cur]:
		if nv != pre:
			dfs(nv, cur, d + 1, id)
a -= 1
b -= 1
for i in range(n - 1):
	(x, y) = list(map(int, input().split()))
	x -= 1
	y -= 1
	E[x].append(y)
	E[y].append(x)
dfs(a, a, 0, 0)
dfs(b, b, 0, 1)
max_d = 0
for i in range(n):
	if D[i][0] < D[i][1]:
		max_d = max(max_d, D[i][1] - 1)
print(max_d)


import sys

def input():
	return sys.stdin.readline().strip()

def mapint():
	return map(int, input().split())
sys.setrecursionlimit(10 ** 9)
(N, u, v) = mapint()
query = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = mapint()
	query[a - 1].append(b - 1)
	query[b - 1].append(a - 1)
from collections import deque

def dfs(start):
	Q = deque([start])
	dist = [10 ** 18] * N
	dist[start] = 0
	while Q:
		now = Q.pop()
		for nx in query[now]:
			if dist[nx] >= 10 ** 18:
				dist[nx] = dist[now] + 1
				Q.append(nx)
	return dist
taka = dfs(u - 1)
aoki = dfs(v - 1)
ans = 0
for i in range(N):
	(t, a) = (taka[i], aoki[i])
	if t <= a:
		ans = max(ans, a - 1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
graph = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	graph[a - 1].append(b - 1)
	graph[b - 1].append(a - 1)
pre = [None] * n

def dfs(node, p_node=-1):
	for c_node in graph[node]:
		if c_node == p_node:
			continue
		pre[c_node] = node
		dfs(c_node, node)
dfs(u)
path = [v]
now = v
while now != u:
	now = pre[now]
	path.append(now)
s1 = path[len(path) // 2]
p1 = path[len(path) // 2 - 1]

def dfs2(node, depth, p_node):
	mx = depth
	for c_node in graph[node]:
		if c_node == p_node:
			continue
		mx = max(mx, dfs2(c_node, depth + 1, node))
	return mx
print(dfs2(s1, 0, p1) + len(path) // 2 - 1)


import sys
sys.setrecursionlimit(10 ** 6)
(n, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
edges = [[] for i in range(n)]
for i in range(n - 1):
	(ai, bi) = map(int, input().split())
	(ai, bi) = (ai - 1, bi - 1)
	edges[ai].append(bi)
	edges[bi].append(ai)

def dfs(already, node, depth):
	depth += 1
	children = edges[node]
	for child in children:
		if already[child] == 0:
			already[child] = depth
			already = dfs(already, child, depth)
	return already
already_u = [0 for i in range(n)]
already_u[u] = -1
already_u = dfs(already_u, u, 0)
already_u[u] = 0
already_v = [0 for i in range(n)]
already_v[v] = -1
already_v = dfs(already_v, v, 0)
already_v[v] = 0
maxi = 0
for i in range(n):
	if already_u[i] < already_v[i]:
		maxi = max(maxi, already_v[i])
print(maxi - 1)


(n, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
tree = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	tree[a - 1].append(b - 1)
	tree[b - 1].append(a - 1)
from queue import Queue
aoki = [-1 for _ in range(n)]
aoki[v] = 0
todo = Queue()
todo.put(v)
while not todo.empty():
	i = todo.get()
	for j in tree[i]:
		if aoki[j] == -1:
			todo.put(j)
			aoki[j] = aoki[i] + 1
taka = [-1 for _ in range(n)]
taka[u] = 0
todo = Queue()
todo.put(u)
while not todo.empty():
	i = todo.get()
	for j in tree[i]:
		if taka[j] == -1:
			if taka[i] + 1 < aoki[j]:
				todo.put(j)
			taka[j] = taka[i] + 1
ans = 0
for i in range(n):
	if taka[i] != -1:
		if aoki[i] - 1 > ans:
			ans = aoki[i] - 1
print(ans)


from collections import deque
(n, u, v) = map(int, input().split())
tree = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	tree[a - 1].append(b - 1)
	tree[b - 1].append(a - 1)
not_yet = deque([u - 1])
already = [False] * n
already[u - 1] = True
dist = [0] * n
while not_yet:
	key = not_yet.popleft()
	for value in tree[key]:
		if already[value]:
			continue
		dist[value] = dist[key] + 1
		already[value] = True
		not_yet.append(value)
not_yet = deque([v - 1])
already = [False] * n
already[v - 1] = True
dist2 = [0] * n
while not_yet:
	key = not_yet.popleft()
	for value in tree[key]:
		if already[value]:
			continue
		dist2[value] = dist2[key] + 1
		already[value] = True
		not_yet.append(value)
judge = dist[v - 1]
ans = 0
for i in range(n):
	if dist[i] + dist2[i] > judge:
		if dist[i] >= dist2[i]:
			continue
		sub = dist2[i] - 1
	else:
		sub = judge - 1
	if sub > ans:
		ans = sub
print(ans)


import sys
f = lambda : map(int, sys.stdin.readline().split())
(n, st, sa) = f()
st -= 1
sa -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = f()
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)

def bfs(s):
	l = [-1] * n
	l[s] = 0
	q = [s]
	while q:
		v = q.pop()
		d = l[v] + 1
		for c in g[v]:
			if l[c] < 0:
				l[c] = d
				q += [c]
	return l
lt = bfs(st)
la = bfs(sa)
m = 0
for i in range(n):
	if lt[i] < la[i]:
		m = max(m, la[i])
print(m - 1)


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

def dfs(v):
	dist = [-1 for _ in range(N + 1)]
	stack = [v]
	dist[v] = 0
	while stack:
		v = stack.pop()
		dist_x = dist[v] + 1
		for child in graph[v]:
			if 0 <= dist[child]:
				continue
			dist[child] = dist_x
			stack.append(child)
	return dist

def solve():
	(dist_u, dist_v) = (dfs(u), dfs(v))
	ans = 0
	for (a, b) in zip(dist_u[1:], dist_v[1:]):
		if a < b:
			ans = max(ans, b - 1)
	print(ans)
solve()


I = [int(_) for _ in open(0).read().split()]
(n, u, v) = I[:3]
(A, B) = (I[3::2], I[4::2])
S = [set() for _ in range(n + 1)]
for (a, b) in zip(A, B):
	S[a].add(b)
	S[b].add(a)

def cal(x):
	dist = [None] * (n + 1)
	dist[x] = 0
	st = set([x])
	while st:
		x = st.pop()
		for i in S[x]:
			if dist[i] == None:
				dist[i] = dist[x] + 1
				st.add(i)
	return dist
T = cal(u)
A = cal(v)
ans = 0
for (t, a) in zip(T[1:], A[1:]):
	if t < a:
		ans = max(ans, a - 1)
print(ans)


import sys
stdin = sys.stdin
ns = lambda : stdin.readline().rstrip()
ni = lambda : int(ns())
na = lambda : list(map(int, stdin.readline().split()))
(n, u, v) = na()
u -= 1
v -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = na()
	a -= 1
	b -= 1
	g[a].append(b)
	g[b].append(a)

def dfs(v):
	dist = [-1] * n
	stack = [v]
	dist[v] = 0
	while stack:
		v = stack.pop()
		dw = dist[v] + 1
		for w in g[v]:
			if dist[w] == -1:
				dist[w] = dw
				stack.append(w)
	return dist
dist_v = dfs(v)
dist_u = dfs(u)
ans = 0
for (rv, ru) in zip(dist_v, dist_u):
	if rv > ru:
		ans = max(ans, rv - 1)
print(ans)


from collections import deque
(N, u, v) = map(int, input().split())
G = [[] for _ in range(N + 1)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	G[a].append(b)
	G[b].append(a)

def bfs(s):
	dist = [-1] * (N + 1)
	dist[s] = 0
	d = deque([s])
	while len(d) > 0:
		v = d.popleft()
		for w in G[v]:
			if dist[w] < 0:
				dist[w] = dist[v] + 1
				d.append(w)
	return dist
tkhs = bfs(u)
aok = bfs(v)
ans = 0
for i in range(1, N + 1):
	if tkhs[i] < aok[i]:
		ans = max(ans, aok[i] - 1)
print(ans)


from collections import deque
(n, u, v) = list(map(int, input().split()))
edgelist = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = list(map(int, input().split()))
	edgelist[a - 1].append(b - 1)
	edgelist[b - 1].append(a - 1)
dist = [[0, 0] for _ in range(n)]

def distance(start, id):
	que = deque()
	visited = [0] * n
	found = False
	que.append([edgelist[start], start])
	while que:
		ver = que.popleft()
		visited[ver[1]] = 1
		for next_v in ver[0]:
			if not visited[next_v]:
				dist[next_v][id] = dist[ver[1]][id] + 1
				que.append([edgelist[next_v], next_v])
distance(u - 1, 0)
distance(v - 1, 1)
dist.sort(key=lambda x: x[1], reverse=True)
ans = 0
for i in range(n):
	if dist[i][1] - dist[i][0] >= 1:
		print(dist[i][1] - 1)
		return


import sys
sys.setrecursionlimit(10 ** 8)
(N, U, V) = map(int, input().split())
(U, V) = (U - 1, V - 1)
AB = [tuple(map(int, input().split())) for i in range(N - 1)]
es = [[] for _ in range(N)]
for (a, b) in AB:
	(a, b) = (a - 1, b - 1)
	es[a].append(b)
	es[b].append(a)
dist1 = [N] * N
dist1[V] = 0

def dfs1(v, p=-1):
	for to in es[v]:
		if to == p:
			continue
		dist1[to] = dist1[v] + 1
		dfs1(to, v)
dfs1(V)
D = dist1[U]
dist2 = [N] * N
dist2[U] = 0

def dfs2(v, p=-1):
	for to in es[v]:
		if to == p:
			continue
		if (D + 1) // 2 > dist1[to]:
			continue
		dist2[to] = dist2[v] + 1
		dfs2(to, v)
dfs2(U)
ans = 0
for (a, b) in zip(dist1, dist2):
	if b == N:
		continue
	ans = max(ans, a - 1)
print(ans)


(N, u, v) = map(int, input().split())
import sys
sys.setrecursionlimit(10 ** 6)
T = [[] for _ in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	T[a - 1].append(b - 1)
	T[b - 1].append(a - 1)

def dfs(start):
	tolist = T[start]
	nowd = done[start]
	for next_n in tolist:
		if done[next_n] == -1:
			done[next_n] = nowd + 1
			dfs(next_n)
done = [-1] * N
done[u - 1] = 0
dfs(u - 1)
dtaka = done.copy()
done = [-1] * N
done[v - 1] = 0
dfs(v - 1)
dao = done.copy()
ans = 0
for i in range(N):
	t = dtaka[i]
	a = dao[i]
	if a > t:
		ans = max(ans, a)
print(ans - 1)


(N, u, v) = map(int, input().split())
edges = [[] for _ in range(N)]
for _ in range(N - 1):
	(fr, to) = map(int, input().split())
	fr -= 1
	to -= 1
	edges[fr].append(to)
	edges[to].append(fr)

def calc(s):
	minDist = [10 ** 18] * N
	st = [(s, 0)]
	while st:
		(now, dist) = st.pop()
		if minDist[now] < dist:
			continue
		minDist[now] = dist
		for to in edges[now]:
			if minDist[to] > dist + 1:
				st.append((to, dist + 1))
	return minDist
distTaka = calc(u - 1)
distAoki = calc(v - 1)
ans = 0
for i in range(N):
	if distTaka[i] < distAoki[i]:
		ans = max(ans, distAoki[i] - 1)
print(ans)


(N, U, V) = map(int, input().split())
edge = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(x, y) = map(int, input().split())
	edge[x].append(y)
	edge[y].append(x)

def dist(s):
	dist = [-1] * (N + 1)
	node = [s]
	dist[s] = 0
	while node:
		s = node.pop()
		for t in edge[s]:
			if dist[t] == -1:
				dist[t] = dist[s] + 1
				node.append(t)
	return dist
taka = dist(U)
aoki = dist(V)
can_go = taka[V]
ans = 0
for (t, a) in zip(taka[1:], aoki[1:]):
	if t <= a:
		ans = max(ans, a - 1)
print(ans)


(n, u, v) = [int(j) for j in input().split()]
(u, v) = (u - 1, v - 1)
l = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = [int(j) - 1 for j in input().split()]
	l[a].append(b)
	l[b].append(a)
k = [10 ** 18 for i in range(n)]
from collections import deque
q = deque([u])
k[u] = 0
s = set()
while q:
	p = q.popleft()
	s.add(p)
	for i in l[p]:
		if i not in s:
			q.append(i)
			k[i] = k[p] + 1
k2 = [10 ** 18 for i in range(n)]
from collections import deque
q = deque([v])
k2[v] = 0
s = set()
while q:
	p = q.popleft()
	s.add(p)
	for i in l[p]:
		if i not in s:
			q.append(i)
			k2[i] = k2[p] + 1
ans = 0
for i in range(n):
	if k[i] <= k2[i]:
		if ans < k2[i]:
			ans = k2[i]
print(ans - 1)


(n, u, v) = map(int, input().split())
g = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)
d1 = [-1] * n
d1[u - 1] = 0
s = [u - 1]
while s:
	d = s.pop()
	for node in g[d]:
		if d1[node] == -1:
			s.append(node)
			d1[node] = d1[d] + 1
d2 = [-1] * n
d2[v - 1] = 0
s = [v - 1]
while s:
	d = s.pop()
	for node in g[d]:
		if d2[node] == -1:
			s.append(node)
			d2[node] = d2[d] + 1
m = 0
mi = 0
for i in range(n):
	if d1[i] < d2[i]:
		if m < d2[i]:
			m = d2[i]
			mi = i
print(m - 1)


import sys
sys.setrecursionlimit(10 ** 6)
(n, g, s) = map(int, input().split())
G = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	G[a - 1].append(b - 1)
	G[b - 1].append(a - 1)

def dfs(V, v, d):
	if V[v] != -1:
		return
	V[v] = d
	for nv in G[v]:
		dfs(V, nv, d + 1)
if len(G[g - 1]) == 1 and G[g - 1][0] == s - 1:
	print(0)
else:
	vs = [-1] * n
	vg = [-1] * n
	dfs(vs, s - 1, 0)
	dfs(vg, g - 1, 0)
	ans = 0
	for i in range(n):
		if vs[i] > vg[i]:
			ans = max(ans, vs[i])
	print(ans - 1)


import sys
sys.setrecursionlimit(1000000000)
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
G = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	G[a].append(b)
	G[b].append(a)
distT = [-1] * n
distA = [-1] * n
distT[u] = 0
distA[v] = 0

def dfs(now, dist):
	for nxt in G[now]:
		if dist[nxt] != -1:
			continue
		dist[nxt] = dist[now] + 1
		dfs(nxt, dist)
dfs(u, distT)
dfs(v, distA)
mx = 0
for i in range(n):
	if distT[i] < distA[i]:
		mx = max(mx, distA[i])
ans = mx - 1
print(ans)


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
(N, U, V) = map(int, readline().split())
m = map(int, read().split())
AB = zip(m, m)
graph = [[] for _ in range(N + 1)]
for (a, b) in AB:
	graph[a].append(b)
	graph[b].append(a)

def dfs(v):
	dist = [-1] * (N + 1)
	stack = [v]
	dist[v] = 0
	while stack:
		v = stack.pop()
		dw = dist[v] + 1
		for w in graph[v]:
			if dist[w] >= 0:
				continue
			dist[w] = dw
			stack.append(w)
	return dist
(DU, DV) = (dfs(U), dfs(V))
answer = 0
for (u, v) in zip(DU[1:], DV[1:]):
	if u < v:
		x = v - 1
		if answer < x:
			answer = x
print(answer)


from collections import deque
import sys
input = sys.stdin.readline
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
tree = [[] for _ in range(N)]
for i in range(N - 1):
	(A, B) = map(int, input().split())
	tree[A - 1].append(B - 1)
	tree[B - 1].append(A - 1)

def bfs(start):
	q = deque()
	q.append([start, 0])
	visited = set()
	lis = [0] * N
	while q:
		(now, dis) = q.popleft()
		if now in visited:
			continue
		lis[now] = dis
		visited.add(now)
		for node in tree[now]:
			q.append([node, dis + 1])
	return lis
ut = bfs(u)
vt = bfs(v)
ans = 0
for i in range(N):
	if ut[i] < vt[i]:
		ans = max(ans, vt[i])
print(ans - 1)


(N, U, V) = map(int, input().split())
if U == V:
	print(0)
	exit()
G = [[] * N for _ in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	G[a - 1].append(b - 1)
	G[b - 1].append(a - 1)
from collections import deque

def calc(s):
	que = deque()
	que.append(s - 1)
	visited = [-1] * N
	visited[s - 1] = 0
	while que:
		v = que.pop()
		for u in G[v]:
			if visited[u] == -1:
				que.appendleft(u)
				visited[u] = visited[v] + 1
	return visited
taka = calc(U)
aoki = calc(V)
ans = 0
for i in range(N):
	if taka[i] <= aoki[i]:
		ans = max(ans, aoki[i])
print(ans - 1)


from collections import deque
import sys
si = sys.stdin.readline

def BFS(g, s):
	d = 0
	dt = dict()
	visited = set()
	visited.add(s)
	Q = deque([s])
	while len(Q):
		d += 1
		Qs = len(Q)
		for _ in range(Qs):
			Qpop = Q.popleft()
			for node in g[Qpop]:
				if node not in visited:
					Q.append(node)
					visited.add(node)
					dt[node] = d
	return dt

def main():
	l = [int(e) for e in si().split()]
	(n, u, v) = (l[0], l[1], l[2])
	graph = [[] for _ in range(n + 1)]
	for _ in range(n - 1):
		l = [int(e) for e in si().split()]
		graph[l[0]].append(l[1])
		graph[l[1]].append(l[0])
	udt = BFS(graph, u)
	vdt = BFS(graph, v)
	(udt[u], vdt[v]) = (0, 0)
	vdmax = 0
	for i in range(1, n + 1):
		if vdt[i] - udt[i] > 0 and vdt[i] > vdmax:
			vdmax = vdt[i]
	print(vdmax - 1)

def __starting_point():
	main()
__starting_point()


(N, u, v) = map(int, input().split())
u -= 1
v -= 1
tonari = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	tonari[a].append(b)
	tonari[b].append(a)
d_from_u = [float('inf') for _ in range(N)]
d_from_v = [float('inf') for _ in range(N)]
d_from_u[u] = 0
d_from_v[v] = 0
now = [v]
d = 0
while now:
	d += 1
	nextvisit = []
	for n in now:
		for t in tonari[n]:
			if d_from_v[t] > d:
				d_from_v[t] = d
				nextvisit.append(t)
	now = nextvisit
now = [u]
d = 0
while now:
	d += 1
	nextvisit = []
	for n in now:
		for t in tonari[n]:
			if d_from_v[t] <= d:
				continue
			elif d_from_u[t] > d:
				d_from_u[t] = d
				nextvisit.append(t)
	now = nextvisit
distance = []
ans = 0
for (U, V) in zip(d_from_u, d_from_v):
	if U < N + 1:
		ans = max(ans, V - 1)
print(ans)


import sys
sys.setrecursionlimit(700000)
from collections import deque
(N, u, v) = map(int, input().split())
tree = deque([deque() for i in range(N + 1)])

def dfs(s, dist, d=0, p=-1):
	dist[s] = d
	for tmp in tree[s]:
		if tmp != p:
			dfs(tmp, dist, d + 1, s)
for i in range(N - 1):
	(A, B) = map(int, input().split())
	tree[A - 1].append(B - 1)
	tree[B - 1].append(A - 1)
(distT, distA) = ([0] * N, [0] * N)
dfs(u - 1, distT)
dfs(v - 1, distA)
ans = 0
for i in range(N):
	if distT[i] < distA[i]:
		ans = max(ans, distA[i])
print(ans - 1)


import sys
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
(N, U, V) = lr()
graph = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(a, b) = lr()
	graph[a].append(b)
	graph[b].append(a)

def dfs(start):
	dist = [-1] * (N + 1)
	dist[start] = 0
	stack = [start]
	while stack:
		s = stack.pop()
		for x in graph[s]:
			if dist[x] != -1:
				continue
			dist[x] = dist[s] + 1
			stack.append(x)
	return dist
dist_U = dfs(U)
dist_V = dfs(V)
answer = max((v for (u, v) in zip(dist_U, dist_V) if v > u)) - 1
print(answer)


(n, u, v) = map(int, input().split())
matrix = [[] for _ in range(n)]
import sys
sys.setrecursionlimit(10 ** 8)
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	matrix[a - 1].append(b - 1)
	matrix[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 matrix[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)


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

def bfs(start, N):
	d = [-1] * (N + 1)
	d[start] = 0
	q = [start]
	while q:
		v = q.pop()
		cnt = d[v] + 1
		for i in E[v]:
			if d[i] == -1:
				q.append(i)
				d[i] = cnt
	return d
taka = bfs(u, N)
aoki = bfs(v, N)
fin = 0
for i in range(N + 1):
	if taka[i] < aoki[i]:
		fin = max(fin, aoki[i])
print(fin - 1)


from collections import deque
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
a = []
b = []
dist = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	dist[a].append(b)
	dist[b].append(a)

def bfs(u):
	d = [-1] * n
	stack = deque([u])
	d[u] = 0
	while len(stack) != 0:
		s = stack.popleft()
		for t in dist[s]:
			if d[t] == -1:
				d[t] = d[s] + 1
				stack.append(t)
	return d
A = bfs(v)
T = bfs(u)
ans = 0
for i in range(n):
	if A[i] > T[i]:
		ans = max(A[i] - 1, ans)
print(ans)


from collections import deque
(N, u, v) = map(int, input().split())
neighbors = [[] for i in range(N + 1)]
for _ in range(N - 1):
	(A, B) = map(int, input().split())
	neighbors[A].append(B)
	neighbors[B].append(A)

def BFS(neighbors, s):
	q = deque([s])
	dist = [-1] * len(neighbors)
	dist[s] = 0
	while q:
		t = q.popleft()
		d = dist[t]
		nodes = neighbors[t]
		for n in nodes:
			if dist[n] < 0:
				dist[n] = d + 1
				q.append(n)
	return dist
u_cost = BFS(neighbors, u)
v_cost = BFS(neighbors, v)
ans = 0
for i in range(1, N + 1):
	if u_cost[i] < v_cost[i]:
		ans = max(ans, v_cost[i])
print(ans - 1)


(n, u, v) = map(int, input().split())
u -= 1
v -= 1
e = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	e[a].append(b)
	e[b].append(a)
cho = [u]
cho_dis = [10 ** 7] * n
cho_dis[u] = 0
cho_visited = {u}
countt = 1
while cho:
	CHO = []
	for i in cho:
		for j in e[i]:
			if j in cho_visited:
				continue
			cho_visited.add(j)
			CHO.append(j)
			cho_dis[j] = countt
	countt += 1
	cho = CHO
aok = [v]
aok_dis = [10 ** 7] * n
aok_dis[v] = 0
aok_visited = {v}
countt = 1
while aok:
	AOK = []
	for i in aok:
		for j in e[i]:
			if j in aok_visited:
				continue
			aok_visited.add(j)
			AOK.append(j)
			aok_dis[j] = countt
	countt += 1
	aok = AOK
ans = 0
for i in range(n):
	if cho_dis[i] < aok_dis[i]:
		ans = max(ans, aok_dis[i] - 1)
print(ans)


(N, U, V) = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N - 1)]
inf = 10 ** 20
c = [[] for i in range(N)]
for (u, v) in AB:
	c[u - 1].append(v - 1)
	c[v - 1].append(u - 1)
from heapq import heappop, heappush

def dij(c, N, s):
	d = [inf] * N
	d[s] = 0
	prev = [-1] * N
	q = []
	heappush(q, (0, s))
	while q:
		u = heappop(q)[1]
		for v in c[u]:
			temp = d[u] + 1
			if d[v] > temp:
				d[v] = temp
				prev[v] = u
				heappush(q, (d[v], v))
	return d
d1 = dij(c, N, U - 1)
d2 = dij(c, N, V - 1)
r = 0
for i in range(N):
	if d1[i] < d2[i]:
		r = max(r, d2[i] - 1)
print(0 if len(c[U - 1]) == 1 and c[U - 1][0] == V - 1 else r)


from collections import deque
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
a = [[] for i in range(n)]
for i in range(n - 1):
	(s, t) = map(int, input().split())
	a[s - 1].append(t - 1)
	a[t - 1].append(s - 1)

def bfs(x):
	dist = [-1] * n
	dist[x] = 0
	Q = deque()
	Q.append(x)
	while Q:
		q = Q.popleft()
		for i in a[q]:
			if dist[i] == -1:
				dist[i] = dist[q] + 1
				Q.append(i)
	return dist
taka = bfs(u)
aoki = bfs(v)
ans = 0
for i in range(n):
	if taka[i] < aoki[i]:
		ans = max(ans, aoki[i] - 1)
print(ans)


(N, u, v) = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N - 1)]
graph = [[] for i in range(N + 1)]
for (i, j) in AB:
	graph[i].append(j)
	graph[j].append(i)

def dfs(v):
	dist = [-1] * (N + 1)
	stack = [v]
	dist[v] = 0
	while stack:
		v = stack.pop()
		dw = dist[v] + 1
		for w in graph[v]:
			if dist[w] >= 0:
				continue
			dist[w] = dw
			stack.append(w)
	return dist
(DU, DV) = (dfs(u), dfs(v))
answer = 0
for (u, v) in zip(DU[1:], DV[1:]):
	if u < v:
		x = v - 1
		if answer < x:
			answer = x
print(answer)


import sys
sys.setrecursionlimit(10 ** 5)
(N, u, v) = map(int, input().split())
(du, dv) = ([0] * -~N, [0] * -~N)
T = [list() for i in range(-~N)]

def dfs(p, t, v, d):
	d[v] = t
	for i in T[v]:
		if i != p:
			dfs(v, t + 1, i, d)
for i in range(N - 1):
	(a, b) = map(int, input().split())
	T[a].append(b)
	T[b].append(a)
dfs(0, 0, u, du)
dfs(0, 0, v, dv)
c = 0
for i in range(1, -~N):
	if len(T[i]) == 1 and i != v:
		if du[i] < dv[i]:
			c = max(c, ~-dv[i])
print(c)


import sys
sys.setrecursionlimit(10 ** 5)
(n, u, v) = list(map(int, input().split()))
u -= 1
v -= 1
m_mat = [[] for i in range(n)]
for _ in range(n - 1):
	(a, b) = list(map(int, input().split()))
	a -= 1
	b -= 1
	m_mat[a].append(b)
	m_mat[b].append(a)
u_map = [-1] * n
v_map = [-1] * n
u_map[u] = 0
v_map[v] = 0

def dfs(current, depth, ma):
	for nex in m_mat[current]:
		if ma[nex] > -1:
			continue
		ma[nex] = depth
		dfs(nex, depth + 1, ma)
dfs(u, 1, u_map)
dfs(v, 1, v_map)
ans = -1
for i in range(n):
	if u_map[i] < v_map[i] and v_map[i] > ans:
		ans = v_map[i]
print(ans - 1)


import sys
from collections import deque
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7

def resolve():

	def bfs(start):
		dist = [f_inf] * n
		dist[start] = 0
		que = deque([start])
		while que:
			v = que.popleft()
			for u in edge[v]:
				if dist[u] == f_inf:
					dist[u] = dist[v] + 1
					que.append(u)
		return dist
	(n, u, v) = list(map(int, input().split()))
	edge = [[] for _ in range(n)]
	for _ in range(n - 1):
		(a, b) = list(map(int, input().split()))
		edge[a - 1].append(b - 1)
		edge[b - 1].append(a - 1)
	distA = bfs(u - 1)
	distT = bfs(v - 1)
	res = 0
	for i in range(n):
		if distA[i] < distT[i]:
			res = max(res, distT[i])
	print(res - 1)

def __starting_point():
	resolve()
__starting_point()


(n, r0, g0) = map(int, input().split())
edges = [tuple(map(int, input().split())) for _ in range(n - 1)]
connects = [[] for i in range(n + 1)]
for e in edges:
	connects[e[0]].append(e[1])
	connects[e[1]].append(e[0])
(rL, gL) = ([-1] * (n + 1), [-1] * (n + 1))
(rL[r0], gL[g0]) = (0, 0)
from collections import deque
(rQ, gQ) = (deque([r0]), deque([g0]))
for (L, Q) in [(rL, rQ), (gL, gQ)]:
	while Q:
		node = Q.popleft()
		for ni in connects[node]:
			if L[ni] == -1:
				L[ni] = L[node] + 1
				Q.append(ni)
print(max((gL[i] for i in range(1, n + 1) if rL[i] < gL[i])) - 1)


(n, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
g = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	g[a].append(b)
	g[b].append(a)
dt = [-1] * n
da = [-1] * n
from collections import deque

def dfs(g, s, d_list):
	stack = deque()
	stack.append(s)
	d_list[s] = 0
	while stack:
		x = stack.pop()
		for new_x in g[x]:
			if d_list[new_x] == -1:
				stack.append(new_x)
				d_list[new_x] = d_list[x] + 1
	return d_list
dt = dfs(g, u, dt)
da = dfs(g, v, da)
d = 0
for i in range(n):
	if dt[i] < da[i]:
		d = max(d, da[i])
print(d - 1)


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

def dfs(s):
	d = [-1] * n
	d[s] = 0
	stack = [s]
	while stack:
		frm = stack.pop()
		for to in adj[frm]:
			if d[to] == -1:
				d[to] = d[frm] + 1
				stack.append(to)
	return d
du = dfs(u)
dv = dfs(v)
ans = 0
for (eu, ev) in zip(du, dv):
	if eu < ev:
		ans = max(ans, ev - 1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 5 + 1)

def cur(p):
	for i in x[p]:
		if rank[i] == -1:
			rank[i] = rank[p] + 1
			par[i] = p
			cur(i)

def cur2(p):
	if spar[p] == -1:
		spar[p] = cur2(par[p])
	return spar[p]
(n, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
x = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	x[a].append(b)
	x[b].append(a)
rank = [-1 for i in range(n)]
rank[u] = 0
par = [-1 for i in range(n)]
cur(u)
spar = [-1 for i in range(n)]
i = v
while i != u:
	spar[i] = i
	i = par[i]
spar[u] = u
for p in range(n):
	cur2(p)
ans = rank[v] - 1
for i in range(n):
	z = rank[v] - rank[spar[i]] * 2
	if z > 0:
		ans = max(ans, rank[i] + z - 1)
print(ans)


(n, x, y) = map(int, input().split())
s = [[] for i in range(n + 1)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	s[a].append(b)
	s[b].append(a)
p = [float('INF') for i in range(n + 1)]
q = [float('INF') for i in range(n + 1)]
p[x] = 0
q[y] = 0
c = set([x])
while c:
	d = set()
	for i in c:
		for j in s[i]:
			if p[j] > p[i] + 1:
				p[j] = p[i] + 1
				d.add(j)
	c = d
c = set([y])
while c:
	d = set()
	for i in c:
		for j in s[i]:
			if q[j] > q[i] + 1:
				q[j] = q[i] + 1
				d.add(j)
	c = d
a = 0
for i in range(1, n + 1):
	if p[i] < q[i]:
		a = max(a, q[i] - 1)
print(a)


import sys
sys.setrecursionlimit(10 ** 9)
(n, u, v) = list(map(int, input().split()))
u -= 1
v -= 1
ab = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = list(map(int, input().split()))
	ab[a - 1].append(b - 1)
	ab[b - 1].append(a - 1)

def dfs1(pos, before_pos):
	for next_pos in ab[pos]:
		if next_pos == before_pos:
			continue
		if next_pos == u:
			return (True, [pos])
		(tf, root) = dfs1(next_pos, pos)
		if tf:
			root.append(pos)
			return (True, root)
	return (False, [])
root = dfs1(v, -1)[1]
root = root[::-1]
root.append(u)
n = (len(root) + 1) // 2
pos = root[n]
before_pos = root[n - 1]
max_len = n

def dfs2(pos, before_pos, cnt):
	nonlocal max_len
	cnt += 1
	max_len = max(max_len, cnt)
	if cnt < n - 1:
		dfs2(root[cnt + 1], pos, cnt)
	else:
		for next_pos in ab[pos]:
			if next_pos == before_pos:
				continue
			dfs2(next_pos, pos, cnt)
dfs2(pos, before_pos, n - 1)
ans = max_len - 1
ans = max(ans, 0)
print(ans)


import sys
input = sys.stdin.readline
(n, u, v) = map(int, input().split())
Edge = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	Edge[a - 1].append(b - 1)
	Edge[b - 1].append(a - 1)
from collections import deque

def BFS(s):
	dist = [-1] * n
	que = deque([s - 1])
	dist[s - 1] = 0
	while que:
		v = que.popleft()
		d = dist[v]
		for w in Edge[v]:
			if dist[w] > -1:
				continue
			dist[w] = d + 1
			que.append(w)
	return dist
(U_d, V_d) = (BFS(u), BFS(v))
ans = 0
for i in range(n):
	if U_d[i] < V_d[i]:
		ans = max(ans, V_d[i] - 1)
print(ans)


(N, U, V) = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N - 1)]
graph = [[] for _ in range(N + 1)]
for (A, B) in AB:
	graph[A].append(B)
	graph[B].append(A)
INF = 10 ** 15
dist_U = [INF] * (N + 1)
dist_V = [INF] * (N + 1)

def dfs(start, dist):
	stack = [start]
	dist[start] = 0
	while stack:
		s = stack.pop()
		for g in graph[s]:
			if dist[g] > dist[s] + 1:
				dist[g] = dist[s] + 1
				stack.append(g)
dfs(U, dist_U)
dfs(V, dist_V)
answer = []
for i in range(1, N + 1):
	if dist_U[i] < dist_V[i]:
		answer.append(dist_V[i] - 1)
print(max(answer))


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)


import sys
sys.setrecursionlimit(10 ** 7)
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	graph[a - 1].append(b - 1)
	graph[b - 1].append(a - 1)
dist = [[-1, -1] for _ in range(n)]
dist[v][0] = 0
dist[u][1] = 0

def dfs(graph, v, k):
	ver = graph[v]
	for vers in ver:
		if dist[vers][k] == -1:
			dist[vers][k] = dist[v][k] + 1
			dfs(graph, vers, k)
dfs(graph, v, 0)
dfs(graph, u, 1)
dist.sort(reverse=True)
for i in range(10 ** 6):
	if dist[i][0] - dist[i][1] >= 1:
		print(dist[i][0] - 1)
		break


from collections import *
(n, u, v) = map(int, input().split())
e = [[] for _ in range(n + 1)]
INF = 10 ** 18
d = [INF] * (n + 1)
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	e[a] += [b]
	e[b] += [a]
q = deque([(v, 0)])
while q:
	(now, c) = q.popleft()
	d[now] = c
	for to in e[now]:
		if d[to] != INF:
			continue
		q.append((to, c + 1))
q = [(u, 0)]
ans = 0
vis = [1] * (n + 1)
while q:
	(now, c) = q.pop()
	vis[now] = 0
	f = 1
	for to in e[now]:
		if vis[to]:
			f = 0
			if d[to] > c + 1:
				q += [(to, c + 1)]
			if d[to] == c + 1:
				ans = max(c + 1, ans)
			else:
				ans = max(c, ans)
	if len(e[now]) == 1:
		ans = max(d[to], ans)
print(ans)


def dfs(g, root, ds):
	q = [root]
	ds[root] = 0
	while q:
		v = q.pop()
		for c in g[v]:
			if ds[c] == -1:
				ds[c] = ds[v] + 1
				q.append(c)
import sys
sys.setrecursionlimit(10 ** 6)
readline = sys.stdin.readline
(n, u, v) = [int(i) for i in readline().split()]
u -= 1
v -= 1
g = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = [int(i) for i in readline().split()]
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)
ds = [-1] * n
dg = [-1] * n
dfs(g, u, ds)
dfs(g, v, dg)
ans = 0
for (i, j) in zip(ds, dg):
	if i < j:
		ans = max(j - 1, ans)
print(ans)


from collections import deque
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
edges = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	edges[a - 1].append(b - 1)
	edges[b - 1].append(a - 1)

def bfs(u):
	visited = [-1] * n
	Q = deque()
	visited[u] = 0
	Q.append(u)
	while Q:
		p = Q.popleft()
		for v in edges[p]:
			if visited[v] == -1:
				visited[v] = visited[p] + 1
				Q.append(v)
	return visited
tak_dist = bfs(u)
aok_dist = bfs(v)
ans = []
for i in range(n):
	if tak_dist[i] < aok_dist[i]:
		ans.append(aok_dist[i])
print(max(ans) - 1)


import sys
sys.setrecursionlimit(10 ** 5)
(n, u, v) = map(int, list(input().split()))
tree = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, list(input().split()))
	tree[a - 1].append(b - 1)
	tree[b - 1].append(a - 1)

def getdis(idx, num, dis):
	dis[idx] = num
	for i in tree[idx]:
		if dis[i] < 0:
			dis = getdis(i, num + 1, dis)
	return dis
dis_u = [-1] * n
dis_u = getdis(u - 1, 0, dis_u)
dis_v = [-1] * n
dis_v = getdis(v - 1, 0, dis_v)
ans = 0
for i in range(n):
	if dis_u[i] <= dis_v[i]:
		ans = max(ans, dis_v[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)


(n, u, v) = map(int, input().split())
u -= 1
v -= 1
edge = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	edge[a].append(b)
	edge[b].append(a)
aoki = [10 ** 10] * n
taka = [10 ** 10] * n
aoki[v] = 0
taka[u] = 0
Q = [v]
visited = {v}
co = 1
while Q:
	P = []
	for i in Q:
		for j in edge[i]:
			if j in visited:
				continue
			aoki[j] = co
			visited.add(j)
			P.append(j)
	Q = P
	co += 1
Q = [u]
visited = {u}
co = 1
while Q:
	P = []
	for i in Q:
		for j in edge[i]:
			if j in visited:
				continue
			taka[j] = co
			visited.add(j)
			P.append(j)
	Q = P
	co += 1
ans = 0
for i in range(n):
	if taka[i] < aoki[i]:
		ans = max(ans, aoki[i] - 1)
print(ans)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
(n, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
l = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	l[a].append(b)
	l[b].append(a)

def dfs(G, cr, seen, dist):
	seen[cr] = 1
	for i in G[cr]:
		if seen[i] == 0:
			dist[i] = dist[cr] + 1
			dfs(G, i, seen, dist)
	return dist
seen_u = [0] * n
seen_v = [0] * n
dist_u = [0] * n
dist_v = [0] * n
T1 = dfs(l, u, seen_u, dist_u)
T2 = dfs(l, v, seen_v, dist_v)
for i in range(n):
	if dist_u[i] >= dist_v[i]:
		dist_v[i] = -1
print(max(dist_v) - 1)


from collections import deque
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
adj = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	adj[a].append(b)
	adj[b].append(a)

def bfs(v):
	visited = [False] * N
	q = deque([v])
	span = [-1] * N
	s = 0
	while q:
		l = len(q)
		newq = deque([])
		for _ in range(l):
			node = q.popleft()
			visited[node] = True
			span[node] = s
			for nei in adj[node]:
				if not visited[nei]:
					newq.append(nei)
		q = newq
		s += 1
	return span
t = bfs(u)
a = bfs(v)
ans = 0
for i in range(N):
	if t[i] <= a[i]:
		ans = max(ans, a[i] - 1)
print(ans)


from collections import deque

def bfs(s):
	q = deque()
	q.append(s)
	dist = [-1] * (n + 1)
	dist[s] = 0
	while q:
		p = q.popleft()
		for i in G[p]:
			if dist[i] == -1:
				dist[i] = dist[p] + 1
				q.append(i)
	return dist
(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)
dist1 = bfs(u)
dist2 = bfs(v)
ans = 0
for i in range(1, n + 1):
	if dist1[i] <= dist2[i]:
		ans = max(ans, dist2[i] - 1)
print(ans)


from collections import deque

def main():
	(N, u, v, *AB) = map(int, open(0).read().split())
	E = [[] for _ in range(N + 1)]
	for (a, b) in zip(*[iter(AB)] * 2):
		E[a].append(b)
		E[b].append(a)

	def game(s):
		D = [-1] * (N + 1)
		D[s] = 0
		Q = deque([s])
		while Q:
			a = Q.popleft()
			for b in E[a]:
				if D[b] != -1:
					continue
				D[b] = D[a] + 1
				Q.append(b)
		return D[1:]
	taka = game(u)
	aoki = game(v)
	print(-1 + max((a for (t, a) in zip(taka, aoki) if t < a)))
main()


from collections import deque
(N, u, v) = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	G[a - 1].append(b - 1)
	G[b - 1].append(a - 1)

def bfs(s):
	seen = [0] * N
	d = [0] * N
	todo = deque()
	seen[s] = 1
	todo.append(s)
	while len(todo):
		a = todo.popleft()
		for b in G[a]:
			if seen[b] == 0:
				seen[b] = 1
				todo.append(b)
				d[b] += d[a] + 1
	return d
d1 = bfs(u - 1)
d2 = bfs(v - 1)
print(max([d2[i] for i in range(N) if d1[i] <= d2[i]]) - 1)


from collections import deque

def calc_dist(head, my_dict, dist_a):
	deq = deque()
	deq.append((head, 0))
	visited = set()
	visited.add(head)
	while deq:
		(ptr, length) = deq.pop()
		dist_a[ptr] = length
		for item in my_dict[ptr]:
			if item not in visited:
				visited.add(item)
				deq.append((item, length + 1))
(n, u, v) = list(map(int, input().split()))
my_dict = dict()
for i in range(n - 1):
	(a, b) = list(map(int, input().split()))
	if a in my_dict:
		my_dict[a].append(b)
	else:
		my_dict[a] = [b]
	if b in my_dict:
		my_dict[b].append(a)
	else:
		my_dict[b] = [a]
dist_a = dict()
dist_b = dict()
calc_dist(u, my_dict, dist_a)
calc_dist(v, my_dict, dist_b)
ret = 0
for i in range(1, n + 1):
	if dist_a[i] > dist_b[i]:
		continue
	else:
		ret = max(ret, dist_b[i] - 1)
print(ret)


import numpy as np
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
(N, u, v) = map(int, input().split())
E = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(ta, tb) = map(int, input().split())
	E[ta].append(tb)
	E[tb].append(ta)
E = np.array(E)
inf = 10 ** 9
taka = np.full(N + 1, inf, dtype=np.int64)
ao = np.full(N + 1, inf, dtype=np.int64)
taka[u] = 0
ao[v] = 0

def solve(dist, start):
	q = [start]
	while q:
		cp = q.pop()
		for nep in E[cp]:
			if dist[nep] != inf:
				continue
			else:
				dist[nep] = dist[cp] + 1
				q.append(nep)
solve(taka, u)
solve(ao, v)
ind = taka < ao
ao_max = np.max(ao[ind])
print(ao_max - 1)


import sys
sys.setrecursionlimit(10 ** 8)
(n, u, v) = map(int, input().split())
to = [[] for _ in range(n + 1)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	to[a].append(b)
	to[b].append(a)

def DFS(s, dist, to, d=0, p=-1):
	dist[s] = d
	for v in to[s]:
		if v == p:
			continue
		DFS(v, dist, to, d + 1, s)

def calcDist(s, dist, to):
	DFS(s, dist, to)
distU = [0] * (n + 1)
distV = [0] * (n + 1)
calcDist(u, distU, to)
calcDist(v, distV, to)
ans = 0
for i in range(n + 1):
	if distU[i] < distV[i]:
		ans = max(ans, distV[i])
print(ans - 1)


(n, u, v) = map(int, input().split())
u -= 1
v -= 1
Tr = [[] for _ in range(n)]
for i in range(n - 1):
	(ai, bi) = map(int, input().split())
	ai -= 1
	bi -= 1
	Tr[ai].append(bi)
	Tr[bi].append(ai)
Dep_u = [-1] * n
Dep_v = [-1] * n
Dep_u[u] = 0
Dep_v[v] = 0
from collections import deque

def bfs(L, x):
	Q = deque()
	Q.append(x)
	while Q:
		y = Q.popleft()
		for z in Tr[y]:
			if L[z] == -1:
				L[z] = L[y] + 1
				Q.append(z)
bfs(Dep_u, u)
bfs(Dep_v, v)
ans = 0
for i in range(n):
	if Dep_v[i] > Dep_u[i]:
		ans = max(ans, Dep_v[i] - 1)
print(ans)


import copy
from collections import deque
(n, st, sa) = map(int, input().split())
st -= 1
sa -= 1
e = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	e[a].append(b)
	e[b].append(a)
visited = [False] * n
visited[st] = True
d = deque()
d.append([st, 0])
tx = {}
if len(e[st]) == 1:
	tx[st] = 0
cnt = 0
while d:
	(f, cnt) = d.popleft()
	flg = True
	for t in e[f]:
		if not visited[t]:
			flg = False
			d.append([t, cnt + 1])
			visited[t] = True
	if flg:
		tx[f] = cnt
visited = [False] * n
visited[sa] = True
d = deque()
d.append([sa, 0])
ax = {}
if len(e[sa]) == 1:
	ax[sa] = 0
cnt = 0
while d:
	(f, cnt) = d.popleft()
	flg = True
	for t in e[f]:
		if not visited[t]:
			flg = False
			d.append([t, cnt + 1])
			visited[t] = True
	if flg:
		ax[f] = cnt
ax = sorted(ax.items(), key=lambda x: x[1], reverse=True)
for i in range(len(ax)):
	(x, d) = (ax[i][0], ax[i][1])
	if d > tx[x]:
		ans = d - 1
		break
print(ans)


def f(s):
	d = [-1] * -~n
	d[s] = 0
	q = [s]
	while q:
		v = q.pop()
		c = d[v] + 1
		for w in e[v]:
			if d[w] < 0:
				d[w] = c
				q += (w,)
	return d
(n, u, v, *t) = map(int, open(0).read().split())
e = [[] for _ in range(n + 1)]
for (a, b) in zip(*[iter(t)] * 2):
	e[a] += (b,)
	e[b] += (a,)
m = 0
for (a, b) in zip(*map(f, (u, v))):
	if a < b:
		m = max(m, b - 1)
print(m)


import queue
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
path = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	path[a].append(b)
	path[b].append(a)
t = [10 ** 9] * n
t[u] = 0
q = queue.Queue()
q.put(u)
while not q.empty():
	p = q.get()
	for x in path[p]:
		if t[x] > t[p] + 1:
			q.put(x)
			t[x] = t[p] + 1
a = [10 ** 9] * n
a[v] = 0
q = queue.Queue()
q.put(v)
while not q.empty():
	p = q.get()
	for x in path[p]:
		if a[x] > a[p] + 1:
			q.put(x)
			a[x] = a[p] + 1
c = [a[i] for i in range(n) if a[i] > t[i]]
print(max(c) - 1)


from collections import deque
import sys
read = sys.stdin.read
readline = sys.stdin.readline
(n, u, v) = map(int, readline().split())
m = map(int, read().split())
data = list(zip(m, m))
graph = [[] for _ in range(n + 1)]
for (a, b) in data:
	graph[a].append(b)
	graph[b].append(a)

def dfs(v):
	dist = [None] * (n + 1)
	dist[v] = 0
	stack = deque([v])
	while stack:
		x = stack.popleft()
		for y in graph[x]:
			if dist[y] is None:
				dist[y] = dist[x] + 1
				stack.append(y)
	return dist
res = 0
for (i, j) in zip(dfs(u), dfs(v)):
	if i is None:
		continue
	if i <= j:
		if j > res:
			res = j
print(res - 1)


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

def dfs(v: int, tree: int, N: int) -> int:
	dist = [-1] * (N + 1)
	dist[v] = 0
	stack = [v]
	while stack:
		now = stack.pop()
		dw = dist[now] + 1
		for ne in tree[now]:
			if dist[ne] >= 0:
				continue
			dist[ne] = dw
			stack.append(ne)
	return dist
T = dfs(u, tree, N)
A = dfs(v, tree, N)
ans = 0
for (t, a) in zip(T[1:], A[1:]):
	if t < a:
		ans = max(ans, a - 1)
print(ans)


from collections import deque
(n, u, v) = list(map(int, input().split()))
a = [[] for _ in range(n + 1)]
for _ in range(n - 1):
	(x, y) = map(int, input().split())
	a[x].append(y)
	a[y].append(x)

def dis(a, v):
	d = [[1, 0] for _ in range(n + 1)]
	c = 1
	for i in a[v]:
		d[i][0] = 1
		d[i][1] = c
		c += 1
	b = deque(a[v])
	while b:
		x = b.pop()
		for i in a[x]:
			if d[i][1] == 0:
				d[i][0] = d[x][0] + 1
				d[i][1] = d[x][1]
				b.append(i)
	d[v] = [0, 0]
	return d
du = dis(a, u)
dv = dis(a, v)
dis = max([dv[i][0] for i in range(n + 1) if dv[i][1] == dv[u][1] and dv[i][0] > du[i][0]])
print(dis - 1)


import sys
sys.setrecursionlimit(10 ** 9)
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
E = [set() for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	E[a].add(b)
	E[b].add(a)
DU = [-1 for _ in range(N)]
DV = [-1 for _ in range(N)]

def dfs(p, c, D):
	D[p] = c
	for q in E[p]:
		if D[q] == -1:
			dfs(q, c + 1, D)
dfs(u, 0, DU)
dfs(v, 0, DV)
mx = 0
for (du, dv) in zip(DU, DV):
	if du < dv:
		mx = max(dv, mx)
print(mx - 1)


import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
(N, u, v) = map(int, input().split())
edge = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	edge[a].append(b)
	edge[b].append(a)

def calc_dist(s):
	dist = [-1] * (N + 1)
	dist[s] = 0
	node = [s]
	while node:
		s = node.pop()
		d = dist[s]
		for t in edge[s]:
			if dist[t] != -1:
				continue
			dist[t] = d + 1
			node.append(t)
	return dist[1:]
taka = calc_dist(u)
aoki = calc_dist(v)
ans = 0
for (x, y) in zip(taka, aoki):
	if x <= y:
		ans = max(ans, y - 1)
print(ans)


import sys
from collections import deque
sys.setrecursionlimit(10 ** 5)

def input():
	return sys.stdin.readline().strip()

def main():
	(N, u, v) = list(map(int, input().split()))
	u -= 1
	v -= 1
	repn = [[] for _ in range(N)]
	for i in range(N - 1):
		(a, b) = list(map(int, input().split()))
		repn[a - 1].append((b - 1, i))
		repn[b - 1].append((a - 1, i))
	path = []

	def connected(v, tv, p=-1):
		if v == tv:
			return True
		for (w, _) in repn[v]:
			if w == p:
				continue
			elif connected(w, tv, v):
				path.append(w)
				return True
		return False
	connected(v, u)
	path.append(v)

	def dfs(v, p):
		d = -1
		for (w, _) in repn[v]:
			if w == p:
				continue
			else:
				d = max(d, dfs(w, v))
		return d + 1
	dist = len(path)
	mid = path[dist // 2 - 1]
	par = path[dist // 2]
	ans = dfs(mid, par)
	print(ans + (dist - 1) // 2)

def __starting_point():
	main()
__starting_point()


from collections import deque
(n, u, v) = map(int, input().split())
graph = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	graph[a].append(b)
	graph[b].append(a)
Q = deque([v - 1])
Vert = [[0, 0] for i in range(n)]
visited = [False] * n
visited[v - 1] = True
while Q:
	V = Q.popleft()
	visited[V] = True
	for i in graph[V]:
		if visited[i]:
			continue
		if i == V:
			continue
		Vert[i][0] = Vert[V][0] + 1
		Q.append(i)
visited = [False] * n
visited[u - 1] = True
Q.append(u - 1)
while Q:
	V = Q.popleft()
	visited[V] = True
	for i in graph[V]:
		if visited[i]:
			continue
		if i == V:
			continue
		Vert[i][1] = Vert[V][1] + 1
		Q.append(i)
ans = 0
for i in Vert:
	if i[0] > i[1]:
		if i[0] > ans:
			ans = i[0]
print(ans - 1)


import sys
input = sys.stdin.readline

def treeDist(u, E):
	D = [-1] * len(E)
	q = [(u, 0)]
	while q:
		(i, d) = q.pop()
		if D[i] != -1:
			continue
		D[i] = d
		for j in E[i]:
			q.append((j, d + 1))
	return D

def main():
	(N, u, v) = map(int, input().split())
	E = [[] for i in range(N)]
	for _ in range(N - 1):
		(a, b) = map(int, input().split())
		E[a - 1].append(b - 1)
		E[b - 1].append(a - 1)
	D1 = treeDist(u - 1, E)
	D2 = treeDist(v - 1, E)
	ans = 0
	for (d1, d2) in zip(D1, D2):
		if d1 < d2:
			ans = max(ans, d2)
	print(ans - 1)
main()


from collections import deque
(N, u, v) = map(int, input().split())
ab_list = [tuple(map(int, input().split())) for _ in range(N - 1)]
graph = [[] for _ in range(N + 1)]
for (a, b) in ab_list:
	graph[a].append(b)
	graph[b].append(a)

def dfs(v):
	dist = [-1] * (N + 1)
	stack = [v]
	dist[v] = 0
	while stack:
		v = stack.pop()
		dw = dist[v] + 1
		for w in graph[v]:
			if dist[w] >= 0:
				continue
			dist[w] = dw
			stack.append(w)
	return dist
DU = dfs(u)
DV = dfs(v)
answer = 0
for (u, v) in zip(DU[1:], DV[1:]):
	if u < v:
		x = v - 1
		answer = max(answer, x)
print(answer)


import sys
sys.setrecursionlimit(10 ** 5)
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
es = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	es[a - 1].append(b - 1)
	es[b - 1].append(a - 1)

def dfs(v, dist, p=-1, d=0):
	dist[v] = d
	for nv in es[v]:
		if nv == p:
			continue
		dfs(nv, dist, v, d + 1)

def calc_dist(x):
	dist = [-1] * n
	dfs(x, dist)
	return dist
dist_t = calc_dist(u)
dist_a = calc_dist(v)
mx = 0
for i in range(n):
	if dist_t[i] < dist_a[i]:
		mx = max(mx, dist_a[i])
ans = mx - 1
print(ans)


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

def dfs(v, p=-1):
	for to in es[v]:
		if to == p:
			continue
		prev[to] = v
		dfs(to, v)
dfs(U)
path = [V]
while prev[path[-1]] >= 0:
	path.append(prev[path[-1]])
ban = set(path[:(len(path) + 1) // 2])
reach = [0] * N
reach[U] = 1

def dfs2(v, p=-1):
	for to in es[v]:
		if to == p:
			continue
		if to in ban:
			continue
		reach[to] = 1
		dfs2(to, v)
dfs2(U)
depth = [0] * N
ans = 0

def dfs3(v, p=-1):
	nonlocal ans
	for to in es[v]:
		if to == p:
			continue
		depth[to] = depth[v] + 1
		if reach[to]:
			ans = max(ans, depth[v])
		dfs3(to, v)
dfs3(V)
print(ans)


import sys
from collections import deque
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(N, p, q, *AB) = list(map(int, read().split()))
	p -= 1
	q -= 1
	G = [[] for _ in range(N)]
	for (a, b) in zip(*[iter(AB)] * 2):
		G[a - 1].append(b - 1)
		G[b - 1].append(a - 1)
	if len(G[p]) == 1 and G[p][0] == q:
		print(0)
		return
	dist1 = [-1] * N
	dist1[p] = 0
	queue = deque([p])
	while queue:
		v = queue.popleft()
		for nv in G[v]:
			if dist1[nv] == -1:
				dist1[nv] = dist1[v] + 1
				queue.append(nv)
	dist2 = [-1] * N
	dist2[q] = 0
	queue = deque([q])
	while queue:
		v = queue.popleft()
		for nv in G[v]:
			if dist2[nv] == -1:
				dist2[nv] = dist2[v] + 1
				queue.append(nv)
	max_d = 0
	for (d1, d2) in zip(dist1, dist2):
		if d1 < d2 and max_d < d2:
			max_d = d2
	print(max_d - 1)
	return

def __starting_point():
	main()
__starting_point()


import sys
read = sys.stdin.read
(N, u, v, *AB) = map(int, read().split())
graph = [[] for _ in range(N + 1)]
for (a, b) in zip(*[iter(AB)] * 2):
	graph[a].append(b)
	graph[b].append(a)

def dfs(v):
	dist = [-1] * (N + 1)
	stack = [v]
	dist[v] = 0
	while stack:
		v = stack.pop()
		dw = dist[v] + 1
		for w in graph[v]:
			if dist[w] >= 0:
				continue
			dist[w] = dw
			stack.append(w)
	return dist
(DU, DV) = (dfs(u), dfs(v))
answer = 0
for (u, v) in zip(DU[1:], DV[1:]):
	if u < v:
		tmp = v - 1
		if answer < tmp:
			answer = tmp
print(answer)


import sys
stdin = sys.stdin
ns = lambda : stdin.readline().rstrip()
na = lambda : list(map(int, stdin.readline().split()))
ni = lambda : int(ns())
sys.setrecursionlimit(200000)
(n, u, v) = na()
u -= 1
v -= 1
g = [[] for _ in range(n)]
for i in range(0, n - 1):
	(a, b) = na()
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)
dep1 = [0] * n
dep2 = [0] * n

def dfs(now, pre, d, dep):
	dep[now] = d
	for nex in g[now]:
		if pre == nex:
			continue
		dfs(nex, now, d + 1, dep)
dfs(u, -1, 0, dep1)
dfs(v, -1, 0, dep2)
ret = 0
for i in range(n):
	if dep1[i] < dep2[i]:
		ret = max(ret, dep2[i] - 1)
print(ret)


from collections import deque
(N, u, v) = map(int, input().split())
edge = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	edge[a - 1].append(b - 1)
	edge[b - 1].append(a - 1)
u -= 1
v -= 1

def bfs(s):
	dist = [-1] * N
	prev = [-1] * N
	que = deque([s])
	dist[s] = 0
	while que:
		v = que.popleft()
		for w in edge[v]:
			if dist[w] < 0:
				dist[w] = dist[v] + 1
				prev[w] = v
				que.append(w)
	return (dist, prev)
(takahashi, _) = bfs(u)
(aoki, prev) = bfs(v)
node = 0
dist = -1
for (i, (t, a)) in enumerate(zip(takahashi, aoki)):
	if t < a and a > dist:
		dist = a
		node = i
print(aoki[prev[node]])


import sys
sys.setrecursionlimit(10 ** 7)
(n, u, v) = map(int, input().split())
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)
ftakahasi = [-1 for i in range(n)]
faoki = [-1 for i in range(n)]

def dfst(i, n):
	ftakahasi[i] = n
	for sg in g[i]:
		if ftakahasi[sg] == -1:
			dfst(sg, n + 1)

def dfsa(i, n):
	faoki[i] = n
	for sg in g[i]:
		if faoki[sg] == -1:
			dfsa(sg, n + 1)
dfst(u - 1, 0)
dfsa(v - 1, 0)
res1 = 0
for i in range(n):
	pt = ftakahasi[i]
	pa = faoki[i]
	if pt < pa:
		tres = pa - 1
		res1 = max(res1, tres)
print(res1)


(n, u, v) = map(int, input().split())
u -= 1
v -= 1
ki = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(lambda x: int(x) - 1, input().split())
	ki[a].append(b)
	ki[b].append(a)
du = [0] * n
dv = [0] * n
from collections import deque

def distance(i):
	seen = [-1] * n
	seen[i] = 0
	todo = deque([i])
	while todo:
		t = todo.popleft()
		l = ki[t]
		d = seen[t]
		for li in l:
			if seen[li] == -1:
				seen[li] = d + 1
				todo.append(li)
	return seen
du = distance(u)
dv = distance(v)
ans = 0
for i in range(n):
	if dv[i] > du[i]:
		ans = max(ans, dv[i])
print(ans - 1)


from collections import deque
INF = 10 ** 9 + 7
(N, u, v) = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N - 1)]
edges = [[] for _ in range(N)]
for (x, y) in X:
	edges[x - 1].append(y - 1)
	edges[y - 1].append(x - 1)

def bfs(s):
	d = [INF] * N
	d[s] = 0
	visited = [False] * N
	visited[s] = True
	q = deque()
	q.append(s)
	while q:
		u = q.popleft()
		for v in edges[u]:
			if visited[v]:
				continue
			visited[v] = True
			d[v] = d[u] + 1
			q.append(v)
	return d
ans = 0
for (x, y) in zip(bfs(u - 1), bfs(v - 1)):
	if x <= y:
		ans = max(ans, y - 1)
print(ans)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)

def dfs(G, cr, seen, dist):
	seen[cr] = 1
	for i in G[cr]:
		if seen[i] == 0:
			dist[i] = dist[cr] + 1
			dfs(G, i, seen, dist)
(n, u, v) = (int(x) for x in input().split())
(u, v) = (u - 1, v - 1)
a = [[] for i in range(n)]
for i in range(n - 1):
	(l, m) = (int(x) - 1 for x in input().split())
	a[l].append(m)
	a[m].append(l)
dist_u = [0] * n
dist_v = [0] * n
dfs(a, u, [0] * n, dist_u)
dfs(a, v, [0] * n, dist_v)
for i in range(n):
	if dist_u[i] >= dist_v[i]:
		dist_v[i] = -1
print(max(dist_v) - 1)


import sys
readline = sys.stdin.readline
(N, u, v) = list(map(int, readline().split()))
G = [[] for i in range(N)]
for i in range(N - 1):
	(a, b) = list(map(int, readline().split()))
	G[a - 1].append(b - 1)
	G[b - 1].append(a - 1)
dist_from_u = [0 for i in range(N)]
dist_from_v = [0 for i in range(N)]
stack = []
stack.append([u - 1, 0, -1])
while stack:
	(e, dist, parent) = stack.pop()
	dist_from_u[e] = dist
	for child in G[e]:
		if child == parent:
			continue
		stack.append([child, dist + 1, e])
stack = []
stack.append([v - 1, 0, -1])
while stack:
	(e, dist, parent) = stack.pop()
	dist_from_v[e] = dist
	for child in G[e]:
		if child == parent:
			continue
		stack.append([child, dist + 1, e])
maxdiff_from_v = 0
for i in range(N):
	if dist_from_u[i] < dist_from_v[i]:
		if maxdiff_from_v < dist_from_v[i]:
			maxdiff_from_v = dist_from_v[i]
print(maxdiff_from_v - 1)


import sys
sys.setrecursionlimit(1000000)

def input():
	return sys.stdin.readline()
(n, u, v) = [int(i) for i in input().split()]
u -= 1
v -= 1
e = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = [int(i) - 1 for i in input().split()]
	e[a].append(b)
	e[b].append(a)
uu = [0] * n
vv = [0] * n

def dfs(ve, r=u, d=0, rr=-1):
	ve[r] = d
	for i in e[r]:
		if i == rr:
			continue
		dfs(ve, i, d + 1, r)
dfs(uu, u)
dfs(vv, v)
ans = 0
for (i, j) in zip(uu, vv):
	if i <= j:
		ans = max(ans, j)
print(ans - 1)


import sys
sys.setrecursionlimit(10 ** 9)
(N, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
edge = [[] for _ in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	edge[a].append(b)
	edge[b].append(a)
taka = [0] * N
aoki = [0] * N

def dfs(v, pre, cost, i):
	for e in edge[v]:
		if e == pre:
			continue
		cost = dfs(e, v, cost, i + 1)
	cost[v] = i
	return cost
taka = dfs(u, -1, [0] * N, 0)
aoki = dfs(v, -1, [0] * N, 0)
m = 0
for i in range(N):
	if taka[i] < aoki[i]:
		m = max(m, aoki[i])
print(m - 1)


import sys
input = sys.stdin.readline
from collections import deque

def bfs(s):
	dist = [-1] * N
	dist[s] = 0
	q = deque([s])
	while q:
		v = q.popleft()
		for nv in adj_list[v]:
			if dist[nv] == -1:
				dist[nv] = dist[v] + 1
				q.append(nv)
	return dist
(N, u, v) = map(int, input().split())
adj_list = [[] for _ in range(N)]
for _ in range(N - 1):
	(Ai, Bi) = map(int, input().split())
	adj_list[Ai - 1].append(Bi - 1)
	adj_list[Bi - 1].append(Ai - 1)
d1 = bfs(u - 1)
d2 = bfs(v - 1)
ans = 0
for i in range(N):
	if d1[i] < d2[i]:
		ans = max(ans, d2[i] - 1)
print(ans)


from collections import deque
(n, u, v) = list(map(int, input().split()))
g = [set([]) for _ in range(n + 1)]
for i in range(n - 1):
	(a, b) = list(map(int, input().split()))
	g[a].add(b)
	g[b].add(a)
leaf = []
for i in range(1, n + 1):
	if len(g[i]) == 1:
		leaf.append(i)
d_u = [-1 for _ in range(n + 1)]
d_v = [-1 for _ in range(n + 1)]

def bfs(start, d):
	d[start] = 0
	q = deque([start])
	while len(q) > 0:
		qi = q.popleft()
		di = d[qi]
		next_qi = g[qi]
		for i in next_qi:
			if d[i] == -1:
				d[i] = di + 1
				q.append(i)
	return d
d_u = bfs(u, d_u)
d_v = bfs(v, d_v)
if u in leaf and list(g[u])[0] == v:
	print(0)
else:
	ans = 0
	for li in leaf:
		if d_u[li] < d_v[li]:
			ans = max(ans, d_v[li] - 1)
	print(ans)


(N, u, v) = map(int, input().split())
G = [[] for i in range(N)]
for _ in range(N - 1):
	(A, B) = map(int, input().split())
	G[A - 1].append(B - 1)
	G[B - 1].append(A - 1)
(isLeaf, dU, dV) = ([len(g) == 1 for g in G], [-1] * N, [-1] * N)
for (D, P) in [(dU, u - 1), (dV, v - 1)]:
	S = [(P, 0)]
	while len(S) > 0:
		(p, d) = S.pop()
		D[p] = d
		for g in G[p]:
			if D[g] == -1:
				S.append((g, d + 1))
ans = 0
for i in range(N):
	if isLeaf[i] and dU[i] < dV[i]:
		ans = max(ans, dV[i] - 1)
print(ans)


(N, S, T) = map(int, input().split())
S -= 1
T -= 1
edges = [[] for _ in range(N)]
for _ in range(N - 1):
	(fr, to) = map(lambda a: int(a) - 1, input().split())
	edges[fr].append(to)
	edges[to].append(fr)

def calc(s):
	minDist = [10 ** 18] * N
	minDist[s] = 0
	st = [s]
	while st:
		now = st.pop()
		d = minDist[now] + 1
		for to in edges[now]:
			if minDist[to] > d:
				minDist[to] = d
				st.append(to)
	return minDist
minDistS = calc(S)
minDistT = calc(T)
ans = 0
for (s, t) in zip(minDistS, minDistT):
	if s <= t:
		ans = max(ans, t - 1)
print(ans)


nii = lambda : map(int, input().split())
from collections import deque
(n, u, v) = nii()
u -= 1
v -= 1
tree = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = nii()
	a -= 1
	b -= 1
	tree[a].append(b)
	tree[b].append(a)

def BFS(s):
	dist = [-1 for i in range(n)]
	dist[s] = 0
	que = deque()
	que.append(s)
	while que:
		x = que.popleft()
		for i in tree[x]:
			if dist[i] == -1:
				que.append(i)
				dist[i] = dist[x] + 1
	return dist
dist_t = BFS(u)
dist_a = BFS(v)
ans = 0
for i in range(n):
	if dist_a[i] > dist_t[i]:
		ans = max(ans, dist_a[i] - 1)
print(ans)


from collections import deque
(N, U, V) = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N - 1)]
tree = [[] for _ in range(N + 1)]
for (a, b) in X:
	tree[a].append(b)
	tree[b].append(a)

def find_dist(s):
	dist = [-1] * (N + 1)
	dist[s] = 0
	q = deque()
	q.append(s)
	while q:
		u = q.popleft()
		for v in tree[u]:
			if dist[v] >= 0:
				continue
			dist[v] = dist[u] + 1
			q.append(v)
	return dist
dist_u = find_dist(U)
dist_v = find_dist(V)
ans = 0
for i in range(1, N + 1):
	if dist_u[i] <= dist_v[i]:
		ans = max(ans, dist_v[i] - 1)
print(ans)


import sys
rd = sys.stdin.readline
sys.setrecursionlimit(10000000)
(n, T, A) = map(int, rd().split())
(T, A) = (T - 1, A - 1)
graph = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = map(int, rd().split())
	graph[a - 1].append(b - 1)
	graph[b - 1].append(a - 1)

def dfs(v, p=-1):
	deg = len(graph[v])
	if v == T:
		root.append(v)
		return True
	if deg == 1 and v != A:
		return False
	ok = False
	for i in range(deg):
		u = graph[v][i]
		if u == p:
			continue
		if dfs(u, v):
			root.append(v)
			ok = True
	return ok

def dfs2(v, p=-1):
	deg = len(graph[v])
	if deg == 1 and v != root[x]:
		return 0
	depth = 0
	for i in range(deg):
		u = graph[v][i]
		if u == p:
			continue
		d = dfs2(u, v)
		depth = max(depth, d + 1)
	return depth
root = []
dfs(A)
x = len(root) // 2 - 1
dp = [0] * n
ans = len(root) // 2 + dfs2(root[x], root[x + 1])
if len(root) % 2 == 0:
	ans -= 1
print(ans)


from heapq import heappop, heappush
INF = float('inf')

def dijkstra(n, G, s):
	dist = [INF] * n
	dist[s] = 0
	hq = [(0, s)]
	while hq:
		(d, v) = heappop(hq)
		if dist[v] < d:
			continue
		for (child, child_d) in G[v]:
			if dist[child] > dist[v] + child_d:
				dist[child] = dist[v] + child_d
				heappush(hq, (dist[child], child))
	return dist
(n, u, v) = list(map(int, input().split()))
graph = [[] for _ in range(n)]
edge = [list(map(int, input().split())) for _ in range(n - 1)]
for (a, b) in edge:
	graph[a - 1].append((b - 1, 1))
	graph[b - 1].append((a - 1, 1))
from_u = dijkstra(n, graph, u - 1)
from_v = dijkstra(n, graph, v - 1)
fil = [x for x in [[fu, fv] for (fu, fv) in zip(from_u, from_v)] if x[0] < x[1]]
sfil = sorted(list(fil), key=lambda x: [-x[1], -x[0]])
print(sfil[0][1] - 1)


import sys
(n, u, v, *ab) = map(int, sys.stdin.read().split())
graph = [[] for _ in range(n + 1)]
for (a, b) in zip(*[iter(ab)] * 2):
	graph[a].append(b)
	graph[b].append(a)

def dfs(v):
	dist = [None] * (n + 1)
	dist[v] = 0
	stack = [v]
	while stack:
		x = stack.pop()
		for y in graph[x]:
			if dist[y] is None:
				dist[y] = dist[x] + 1
				stack.append(y)
	return dist

def main():
	dist_u = dfs(u)
	dist_v = dfs(v)
	d = 1
	for i in range(1, n + 1):
		if dist_v[i] >= dist_u[i]:
			d = max(d, dist_v[i])
	ans = d - 1
	return ans
ans = main()
print(ans)


(N, u, v) = map(int, input().split())
G = [[] for i in range(N)]
for i in range(N - 1):
	(s, t) = map(int, input().split())
	G[s - 1].append(t - 1)
	G[t - 1].append(s - 1)

def expro(u):
	D = [0] * N
	D[u] = 0
	S = []
	S.append(u)
	L = []
	q = 0
	while q < N:
		l = S[q]
		for i in range(len(G[l])):
			m = G[l][i]
			if len(G[l]) == 1 and l != u:
				L.append(l)
			if D[m] == 0 and m != u:
				D[m] = D[l] + 1
				S.append(m)
		q += 1
	return (L, D)
(L, D) = expro(u - 1)
(M, E) = expro(v - 1)
k = 0
ans = 0
for i in M:
	if D[i] < E[i]:
		if k < E[i]:
			k = E[i]
			ans = E[i] - 1
print(ans)


from collections import deque

def bfs(s):
	que = deque([(s, 0)])
	dist = [None] * n
	while que:
		(cur, cst) = que.popleft()
		if dist[cur] != None:
			continue
		dist[cur] = cst
		for nxt in es[cur]:
			que.append((nxt, cst + 1))
	return dist
(n, u, v) = map(int, input().split())
es = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	es[a - 1].append(b - 1)
	es[b - 1].append(a - 1)
dist_u = bfs(u - 1)
dist_v = bfs(v - 1)
ans = 0
for i in range(n):
	if dist_v[i] - dist_u[i] >= 1:
		ans = max(ans, dist_v[i])
print(max(0, ans - 1))


from collections import deque
(n, u, v) = map(int, input().split())
Tree = [[] for _ in range(n)]

def calc(s, tree):
	S = deque([s])
	Dist = [-1] * n
	Dist[s] = 0
	while S:
		u = S.pop()
		for v in tree[u]:
			if Dist[v] < 0:
				Dist[v] = Dist[u] + 1
				S.append(v)
	return Dist
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	Tree[a - 1].append(b - 1)
	Tree[b - 1].append(a - 1)
(uDist, vDist) = (calc(u - 1, Tree), calc(v - 1, Tree))
ans = 0
for i in range(n):
	if uDist[i] < vDist[i]:
		ans = max(ans, vDist[i] - 1)
print(ans)


from collections import deque
(N, T, A, *AB) = map(int, open(0).read().split())
E = [[] for _ in range(N + 1)]
for (a, b) in zip(*[iter(AB)] * 2):
	E[a].append(b)
	E[b].append(a)
takahashi = [-1] * (N + 1)
takahashi[T] = 0
Q = deque([T])
while Q:
	a = Q.popleft()
	for b in E[a]:
		if takahashi[b] != -1:
			continue
		takahashi[b] = takahashi[a] + 1
		Q.append(b)
aoki = [-1] * (N + 1)
aoki[A] = 0
Q = deque([A])
while Q:
	a = Q.popleft()
	for b in E[a]:
		if aoki[b] != -1:
			continue
		aoki[b] = aoki[a] + 1
		Q.append(b)
ma = 0
for (t, a) in zip(takahashi[1:], aoki[1:]):
	if t < a:
		ma = max(ma, a)
print(ma - 1)


(N, u, v) = (int(i) for i in input().split())
adj = []
for i in range(N + 1):
	adj.append([])
for i in range(N - 1):
	(a, b) = map(int, input().split())
	adj[a] += [b]
	adj[b] += [a]

def distances(sommet):
	d = [-1] * (N + 1)
	File = [sommet]
	d[sommet] = 0
	while File:
		s = File.pop()
		for x in adj[s]:
			if d[x] == -1:
				d[x] = 1 + d[s]
				File.append(x)
	return d
t = distances(u)
a = distances(v)
ans = -1
for i in range(1, N + 1):
	if t[i] < a[i]:
		ans = max(ans, a[i] - 1)
print(ans)


import sys
sys.setrecursionlimit(1000000)
(n, u, v) = map(int, input().split())
Graph = []
for i in range(n):
	Graph.append([])
for i in range(n - 1):
	(a, b) = map(int, input().split())
	Graph[a - 1].append(b - 1)
	Graph[b - 1].append(a - 1)

def dfs(s, b=-1):
	for i in Graph[s]:
		if i != b:
			depth[i] = depth[s] + 1
			dfs(i, s)
depth = [0] * n
dfs(u - 1)
dT = depth
depth = [0] * n
dfs(v - 1)
dA = depth
dArrest = 0
for i in range(n):
	if dA[i] > dT[i]:
		dArrest = max(dArrest, dA[i])
print(dArrest - 1)


import sys
sys.setrecursionlimit(1 << 30)

def dfs(x, dis):
	for y in Tree[x]:
		if parent[y] != x:
			parent[x] = y
			dis[y] = dis[x] + 1
			dfs(y, dis)
(N, u, v) = map(int, input().split())
Tree = [[] for _ in range(N + 1)]
for i in range(N - 1):
	(A, B) = map(int, input().split())
	Tree[A].append(B)
	Tree[B].append(A)
disT = [-1] * (N + 1)
disA = [-1] * (N + 1)
disT[u] = 0
disA[v] = 0
parent = [-1] * (N + 1)
dfs(u, disT)
parent = [-1] * (N + 1)
dfs(v, disA)
Ans = 0
for i in range(len(disA)):
	if disA[i] >= disT[i]:
		Ans = max(Ans, disA[i] - 1)
print(Ans)


import sys
input = sys.stdin.readline
(N, u, v) = map(int, input().split())
E = [tuple(map(int, input().split())) for i in range(N - 1)]
EDGE = [[] for i in range(N + 1)]
for (x, y) in E:
	EDGE[x].append(y)
	EDGE[y].append(x)
from collections import deque
T = [-1] * (N + 1)
Q = deque()
Q.append(u)
T[u] = 0
while Q:
	x = Q.pop()
	for to in EDGE[x]:
		if T[to] == -1:
			T[to] = T[x] + 1
			Q.append(to)
A = [-1] * (N + 1)
Q = deque()
Q.append(v)
A[v] = 0
while Q:
	x = Q.pop()
	for to in EDGE[x]:
		if A[to] == -1:
			A[to] = A[x] + 1
			Q.append(to)
OK = [0] * (N + 1)
for i in range(N + 1):
	if T[i] < A[i]:
		OK[i] = 1
ANS = 0
for i in range(N + 1):
	if OK[i] == 1:
		ANS = max(ANS, A[i])
print(max(0, ANS - 1))


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

def bfs(x):
	q = deque()
	seen = [False] * n
	level = [-1] * n
	parent = [-1] * n
	q.append(x)
	seen[x] = True
	level[x] = 0
	parent[x] = 0
	while len(q) > 0:
		cur = q.popleft()
		lvl = level[cur]
		for i in g[cur]:
			if seen[i] == False:
				seen[i] = True
				level[i] = lvl + 1
				parent[i] = cur
				q.append(i)
	return level
ul = bfs(u - 1)
vl = bfs(v - 1)
ret = 0
for (i, j) in zip(ul, vl):
	if i < j:
		ret = max(ret, j - 1)
print(ret)


import sys
sys.setrecursionlimit(10 ** 7)
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
graph = [[] for _ in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	graph[a].append(b)
	graph[b].append(a)
dist = []

def dfs(v, depth=0, parent=-1):
	global dist
	dist[v] = depth
	for u in graph[v]:
		if u == parent:
			continue
		dfs(u, depth + 1, v)

def calc_dist(s):
	global dist
	dist = [0 for _ in range(N)]
	dfs(s)
	return dist
distS = calc_dist(u)
distT = calc_dist(v)
mx = 0
for i in range(N):
	if distS[i] < distT[i]:
		mx = max(mx, distT[i])
print(mx - 1)


(N, u, v) = map(int, input().split())
from collections import deque
g = [[] for _ in range(N)]
for i in range(N - 1):
	(A, B) = map(int, input().split())
	g[A - 1].append(B - 1)
	g[B - 1].append(A - 1)

def bfs(n):
	stack = deque([])
	depth = [-1] * N
	depth[n - 1] = 0
	stack.append(n - 1)
	while stack:
		a = stack.popleft()
		for b in g[a]:
			if depth[b] == -1:
				depth[b] = depth[a] + 1
				stack.append(b)
	return depth
U = bfs(u)
V = bfs(v)
List = []
for i in range(N):
	if len(g[i]) == 1 and U[i] < V[i]:
		List.append(i)
ans = 0
for i in List:
	ans = max(ans, V[i] - 1)
print(ans)


import sys
sys.setrecursionlimit(200000)

def dfs(array, current, count):
	array[current - 1] = count
	for neighbour in graph[current]:
		if array[neighbour - 1] == -1:
			array = dfs(array, neighbour, count + 1)
	else:
		return array
T = [-1 for x in range(100000)]
A = [-1 for x in range(100000)]
graph = {}
(N, u, v) = map(int, input().split())
for i in range(N - 1):
	(a, b) = map(int, input().split())
	try:
		graph[a].append(b)
	except KeyError:
		graph[a] = [b]
	try:
		graph[b].append(a)
	except KeyError:
		graph[b] = [a]
T = dfs(T, u, 0)
A = dfs(A, v, 0)
ans = -1
for i in range(N):
	if T[i] < A[i]:
		ans = max(ans, A[i] - 1)
print(max(0, ans))


I = [int(_) for _ in open(0).read().split()]
(N, U, V) = I[:3]
(A, B) = (I[3::2], I[4::2])
G = [set() for _ in range(N + 1)]
for (a, b) in zip(A, B):
	G[a].add(b)
	G[b].add(a)

def dfs(x):
	dist = [None] * (N + 1)
	dist[x] = 0
	stack = set([x])
	while stack:
		x = stack.pop()
		for xn in G[x]:
			if dist[xn] is None:
				dist[xn] = dist[x] + 1
				stack.add(xn)
	return dist
distt = dfs(U)
dista = dfs(V)
ans = 0
for (t, a) in zip(distt[1:], dista[1:]):
	if t < a:
		ans = max(ans, a - 1)
print(ans)


import sys
from collections import deque
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(N, ta, ao, *AB) = list(map(int, read().split()))
	ta -= 1
	ao -= 1
	G = [[] for _ in range(N)]
	for (a, b) in zip(*[iter(AB)] * 2):
		G[a - 1].append(b - 1)
		G[b - 1].append(a - 1)
	dist_ta = [-1] * N
	dist_ta[ta] = 0
	queue = deque([ta])
	while queue:
		v = queue.popleft()
		for nv in G[v]:
			if dist_ta[nv] == -1:
				dist_ta[nv] = dist_ta[v] + 1
				queue.append(nv)
	dist_ao = [-1] * N
	dist_ao[ao] = 0
	queue = deque([ao])
	while queue:
		v = queue.popleft()
		for nv in G[v]:
			if dist_ao[nv] == -1:
				dist_ao[nv] = dist_ao[v] + 1
				queue.append(nv)
	d_ao = 0
	for (d1, d2) in zip(dist_ta, dist_ao):
		if d1 < d2 and d_ao < d2:
			d_ao = d2
	if d_ao == 0:
		if dist_ta[ao] == 1:
			print(0)
		else:
			print(1)
	print(d_ao - 1)
	return

def __starting_point():
	main()
__starting_point()


from collections import deque
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
G = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	G[a].append(b)
	G[b].append(a)

def bfs(x):
	dist = [-1] * n
	dist[x] = 0
	que = deque([x])
	while que:
		now = que.pop()
		for nex in G[now]:
			if dist[nex] != -1:
				continue
			dist[nex] = dist[now] + 1
			que.appendleft(nex)
	return dist
dist_u = bfs(u)
dist_v = bfs(v)
ans = 0
for i in range(n):
	if dist_u[i] < dist_v[i]:
		ans = max(ans, dist_v[i] - 1)
print(ans)


import sys
input = sys.stdin.readline
I = lambda : int(input())
MI = lambda : map(int, input().split())
LI = lambda : list(map(int, input().split()))
from collections import deque
res = 0
INF = 10 ** 9
(N, u, v) = MI()
G = [[] for _ in [0] * (N + 1)]
for i in range(N - 1):
	(a, b) = MI()
	G[a].append(b)
	G[b].append(a)

def bfs(a):
	q = deque()
	q.append(a)
	d = [INF] * (N + 1)
	d[a] = 0
	res = 0
	while q:
		r = q.popleft()
		for nr in G[r]:
			if d[nr] == INF:
				q.append(nr)
				d[nr] = d[r] + 1
				res += 1
	return d
aoki = bfs(v)
taka = bfs(u)
m = 0
for i in range(1, N + 1):
	if taka[i] < aoki[i]:
		m = max(aoki[i], m)
print(m - 1)


from collections import defaultdict
(N, u, v) = map(int, input().split())
d = defaultdict(list)
for _ in range(N - 1):
	(A, B) = map(int, input().split())
	d[A].append(B)
	d[B].append(A)

def get_dist(s):
	dist = [-1] * (N + 1)
	dist[s] = 0
	q = [s]
	while q:
		a = q.pop()
		for b in d[a]:
			if dist[b] != -1:
				continue
			dist[b] = dist[a] + 1
			q.append(b)
	return dist
(du, dv) = (get_dist(u), get_dist(v))
ds = [(i, j[0], j[1]) for (i, j) in enumerate(zip(du, dv)) if j[0] < j[1]]
ds.sort(key=lambda x: -x[2])
(a, b, c) = ds[0]
print(c - 1)


import sys
import math
import collections
import bisect
import itertools
import fractions
import copy
import decimal
import queue
sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7
ni = lambda : int(sys.stdin.readline())
ns = lambda : list(map(int, sys.stdin.readline().split()))
na = lambda : list(map(int, sys.stdin.readline().split()))

def main():
	(n, u, v) = ns()
	edge = [[] for _ in range(n)]
	for _ in range(n - 1):
		(a, b) = ns()
		a -= 1
		b -= 1
		edge[a].append(b)
		edge[b].append(a)
	dist_takahashi = [-1] * n
	dist_aoki = [-1] * n

	def bfs(pos, dist):
		que = queue.Queue()
		que.put(pos)
		dist[pos] = 0
		while not que.empty():
			current = que.get()
			for e in edge[current]:
				if dist[e] == -1:
					dist[e] = dist[current] + 1
					que.put(e)
		return dist
	dist_takahashi = bfs(u - 1, dist_takahashi)
	dist_aoki = bfs(v - 1, dist_aoki)
	ans = 0
	for (dt, da) in zip(dist_takahashi, dist_aoki):
		if dt < da:
			ans = max(ans, da - 1)
	print(ans)

def __starting_point():
	main()
__starting_point()


import sys
sys.setrecursionlimit(10 ** 9)
f = lambda : map(int, input().split())
(n, st, sa) = f()
st -= 1
sa -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = f()
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)

def dfs(v, p=-1, d=0):
	l[v] = d
	for c in g[v]:
		if c == p:
			continue
		dfs(c, v, d + 1)

def dist(s):
	nonlocal l
	l = [0] * n
	dfs(s)
	return l
lt = dist(st)
la = dist(sa)
m = 0
for i in range(n):
	if lt[i] < la[i]:
		m = max(m, la[i])
print(m - 1)


from collections import deque
(n, u, v) = map(int, input().split())
G = [[] for i in range(n)]
dist_u = [-1] * n
dist_v = [-1] * n
for i in range(n - 1):
	(a, b) = map(int, input().split())
	G[a - 1].append(b - 1)
	G[b - 1].append(a - 1)
u -= 1
v -= 1
dist_u[u] = 0
q = deque([u])
while q:
	cur = q.popleft()
	for nx in G[cur]:
		if dist_u[nx] == -1:
			q.append(nx)
			dist_u[nx] = dist_u[cur] + 1
dist_v[v] = 0
q = deque([v])
while q:
	cur = q.popleft()
	for nx in G[cur]:
		if dist_v[nx] == -1:
			q.append(nx)
			dist_v[nx] = dist_v[cur] + 1
ans = 0
for i in range(n):
	if dist_u[i] < dist_v[i] and dist_v[i] >= ans:
		ans = dist_v[i]
print(ans - 1)


import sys
input = sys.stdin.readline
from collections import *

def bfs(s):
	q = deque([s])
	dist = [-1] * N
	dist[s] = 0
	while q:
		v = q.popleft()
		for nv in G[v]:
			if dist[nv] == -1:
				dist[nv] = dist[v] + 1
				q.append(nv)
				if s == V - 1:
					is_leaf[v] = False
	return dist
(N, U, V) = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(N - 1):
	(A, B) = map(int, input().split())
	G[A - 1].append(B - 1)
	G[B - 1].append(A - 1)
is_leaf = [True] * N
d1 = bfs(V - 1)
d2 = bfs(U - 1)
ans = 0
for i in range(N):
	if not is_leaf[i] and d2[i] <= d1[i]:
		ans = max(ans, d1[i])
print(ans)


import sys
sys.setrecursionlimit(10 ** 7)
(n, u, v) = list(map(int, input().split()))
u -= 1
v -= 1
graph = [[] for _ in range(n + 1)]
for i in range(n - 1):
	(a, b) = list(map(int, input().split()))
	graph[a - 1].append(b - 1)
	graph[b - 1].append(a - 1)
dist = [[-1, -1] for _ in range(n)]
dist[v][0] = 0
dist[u][1] = 0

def dfs1(graph, v):
	vv = graph[v]
	for vvv in vv:
		if dist[vvv][0] == -1:
			dist[vvv][0] = dist[v][0] + 1
			dfs1(graph, vvv)

def dfs2(graph, v):
	vv = graph[v]
	for vvv in vv:
		if dist[vvv][1] == -1:
			dist[vvv][1] = dist[v][1] + 1
			dfs2(graph, vvv)
dfs1(graph, v)
dfs2(graph, u)
dist.sort(reverse=True)
for i in range(10 ** 6):
	if dist[i][0] - dist[i][1] >= 1:
		print(dist[i][0] - 1)
		break


(n, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
e = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(lambda x: int(x) - 1, input().split())
	e[a].append(b)
	e[b].append(a)
mt = [-1 for i in range(n)]
de = [-1 for i in range(n)]
dpl = []
s = [v]
t = 0
while s:
	dpl.append([])
	ns = []
	for i in s:
		de[i] = t
		dpl[-1].append(i)
		for j in e[i]:
			if de[j] != -1:
				continue
			ns.append(j)
			mt[j] = i
	s = ns
	t += 1
maxd = [de[i] for i in range(n)]
for i in dpl[:0:-1]:
	for j in i:
		maxd[mt[j]] = max(maxd[mt[j]], maxd[j])
t = de[u] // 2 + 1
while de[u] != t:
	u = mt[u]
print(maxd[u] - 1)


import sys
sys.setrecursionlimit(1000000)

def make_adlist(n):
	adlist = [[] for _ in range(n)]
	for _ in range(n - 1):
		(a, b) = map(lambda x: int(x) - 1, input().split())
		adlist[a] += [b]
		adlist[b] += [a]
	return adlist

def dfs(adlist, s, dist, d):
	for nxt in adlist[s]:
		if dist[nxt] == -1:
			dist[nxt] = d
			dfs(adlist, nxt, dist, d + 1)

def main():
	(n, u, v) = map(int, input().split())
	u -= 1
	v -= 1
	adlist = make_adlist(n)
	t_dist = [-1] * n
	t_dist[u] = 0
	dfs(adlist, u, t_dist, 1)
	a_dist = [-1] * n
	a_dist[v] = 0
	dfs(adlist, v, a_dist, 1)
	ans = 0
	for i in range(n):
		if t_dist[i] < a_dist[i]:
			ans = max(ans, a_dist[i] - 1)
	print(ans)
main()


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

def dfs(v):
	dist = [-1] * (n + 1)
	stack = [v]
	dist[v] = 0
	while stack:
		v = stack.pop()
		dw = dist[v] + 1
		for w in graph[v]:
			if dist[w] >= 0:
				continue
			dist[w] = dw
			stack.append(w)
	return dist
(du, dv) = (dfs(u), dfs(v))
ans = 0
for (u, v) in zip(du[1:], dv[1:]):
	if u < v:
		x = v - 1
		if ans < x:
			ans = x
print(ans)


from collections import deque
(n, u, v) = map(int, input().split())
tree = [[] for _ in range(n + 1)]
for _i in range(n - 1):
	(a, b) = map(int, input().split())
	tree[a].append(b)
	tree[b].append(a)

def solve(x):
	visit = [-1 for _ in range(n + 1)]
	visit[x] = 0
	q = deque([x])
	while q:
		p = q.popleft()
		for i in tree[p]:
			if visit[i] < 0:
				visit[i] = visit[p] + 1
				q.append(i)
	return visit
visit_a = solve(v)
visit_t = solve(u)
(x, y) = ([], [])
for i in range(1, n + 1):
	if visit_a[i] >= visit_t[i]:
		x.append(visit_a[i])
		y.append(visit_t[i])
p = x.index(max(x))
print(x[p] - 1)


import sys
sys.setrecursionlimit(10 ** 6)
(n, u, v) = list(map(int, input().split()))
u -= 1
v -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = [int(x) - 1 for x in input().split()]
	g[a].append(b)
	g[b].append(a)
INF = 1 << 30
t = [INF] * n
a = [INF] * n
t[u] = 0
a[v] = 0

def dfs(v, p, d):
	for nv in g[v]:
		if nv == p:
			continue
		if d[nv] == INF:
			d[nv] = d[v] + 1
			dfs(nv, v, d)
dfs(u, -1, t)
dfs(v, -1, a)
ans = 0
for i in range(n):
	if t[i] < a[i]:
		ans = max(ans, a[i] - 1)
print(ans)


(N, u, v) = map(int, input().split())
ABs = [tuple(map(int, input().split())) for _ in range(N - 1)]

def BFS(K, edges, N):
	roots = [[] for _ in range(N)]
	for (a, b) in edges:
		roots[a - 1] += [b - 1]
		roots[b - 1] += [a - 1]
	dist = [-1] * N
	stack = []
	stack.append(K)
	dist[K] = 0
	while stack:
		label = stack.pop(-1)
		for i in roots[label]:
			if dist[i] == -1:
				dist[i] = dist[label] + 1
				stack += [i]
	return dist
U = BFS(u - 1, ABs, N)
V = BFS(v - 1, ABs, N)
max_ = 0
for (a, b) in zip(U, V):
	if a < b:
		max_ = max(max_, b - 1)
print(max_)


import sys
sys.setrecursionlimit(1000000)
(n, u, v) = map(int, input().split())
E = [[] for _ in range(n + 1)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	E[a].append(b)
	E[b].append(a)
depth = [-1] * (n + 1)
dis = [-1] * (n + 1)

def dsf_1(p, d):
	depth[p] = d
	for c in E[p]:
		if depth[c] == -1:
			dsf_1(c, d + 1)
dsf_1(v, 0)

def dsf_2(p, d):
	dis[p] = d
	for c in E[p]:
		if dis[c] == -1:
			dsf_2(c, d + 1)
dsf_2(u, 0)
ans = 0
for i in range(1, n + 1):
	if len(E[i]) == 1 and i != v:
		if dis[i] < depth[i]:
			ans = max(ans, depth[i] - 1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
(N, u, v) = list(map(int, readline().split()))
AB = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(a, b) = list(map(int, readline().split()))
	AB[a].append(b)
	AB[b].append(a)
tdist = [0] * (N + 1)
adist = [0] * (N + 1)

def tdfs(x, pre):
	for to in AB[x]:
		if to == pre:
			continue
		tdist[to] = tdist[x] + 1
		tdfs(to, x)

def adfs(x, pre):
	for to in AB[x]:
		if to == pre:
			continue
		adist[to] = adist[x] + 1
		adfs(to, x)
tdfs(u, -1)
adfs(v, -1)
ans = 0
for i in range(1, N + 1):
	if tdist[i] < adist[i]:
		k = adist[i] - tdist[i]
		ans = max(ans, tdist[i] + k - 1)
print(ans)


from collections import deque, defaultdict
import copy

def main():
	from collections import defaultdict
	import copy
	(n, u, v) = map(int, input().split())
	u -= 1
	v -= 1
	graph = defaultdict(deque)
	for _ in range(n - 1):
		(a, b) = map(int, input().split())
		a -= 1
		b -= 1
		graph[a].append(b)
		graph[b].append(a)
	depth = dfs(graph, u, n)
	depth2 = dfs(graph, v, n)
	ans = 0
	ans2 = 10 ** 6
	for (i, j) in zip(depth, depth2):
		if i < j:
			ans = max(j, ans)
	print(ans - 1)

def dfs(G, s, n):
	stack = deque([s])
	graph = copy.deepcopy(G)
	depth = [None] * n
	depth[s] = 0
	while stack:
		u = stack[-1]
		if graph[u]:
			v = graph[u].popleft()
			if depth[v] == None:
				depth[v] = depth[u] + 1
				stack.append(v)
		else:
			stack.pop()
	return depth
main()


from collections import deque
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
graph = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(lambda x: int(x) - 1, input().split())
	graph[a] += [b]
	graph[b] += [a]

def bfs(s):
	dist = [-1] * N
	q = deque([s])
	dist[s] = 0
	while q:
		u = q.popleft()
		for v in graph[u]:
			if dist[v] == -1:
				dist[v] = dist[u] + 1
				q.append(v)
	return dist
d1 = bfs(u)
d2 = bfs(v)
ans = 0
for i in range(N):
	if d1[i] < d2[i]:
		ans = max(ans, d2[i] - 1)
print(ans)


from collections import deque
(N, U, V) = map(int, input().split())
(U, V) = (U - 1, V - 1)
E = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	E[a].append(b)
	E[b].append(a)

def BFS(root):
	D = [0 for _ in range(N)]
	visited = [False for _ in range(N)]
	willSearch = [False for _ in range(N)]
	Q = deque()
	Q.append(root)
	willSearch[root] = True
	while len(Q) > 0:
		now = Q.pop()
		visited[now] = True
		for nx in E[now]:
			if visited[nx] or willSearch[nx]:
				continue
			willSearch[nx] = True
			D[nx] = D[now] + 1
			Q.append(nx)
	return D
UD = BFS(U)
VD = BFS(V)
if E[U] == [V]:
	print(0)
	return
ans = 0
for i in range(N):
	if UD[i] <= VD[i]:
		ans = max(ans, VD[i] - 1)
print(ans)


import sys, collections, math, random
sys.setrecursionlimit(10 ** 7)
from fractions import gcd

def Is():
	return [int(x) for x in sys.stdin.readline().split()]

def Ss():
	return sys.stdin.readline().split()

def I():
	return int(sys.stdin.readline())

def S():
	return input()
(n, u, v) = Is()
tree = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = Is()
	tree[a - 1].append(b - 1)
	tree[b - 1].append(a - 1)
(md_u, md_v) = ([-1] * n, [-1] * n)
qs = ([[u - 1, 0]], [[v - 1, 0]])
for (md, q) in zip([md_u, md_v], qs):
	while q:
		(n, d) = q.pop()
		md[n] = d
		for e in tree[n]:
			if md[e] == -1:
				q.append([e, d + 1])
ans = -2
for (a, b) in zip(md_u, md_v):
	if a < b:
		ans = max(b, ans)
print(ans - 1)


(n, u, v) = map(int, input().split())
u -= 1
v -= 1
g = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	g[a].append(b)
	g[b].append(a)
from collections import deque
q = deque()
q.append(u)
t = [-1] * n
t[u] = 0
while q:
	x = q.popleft()
	for new_x in g[x]:
		if t[new_x] == -1:
			t[new_x] = t[x] + 1
			q.append(new_x)
q = deque()
q.append(v)
a = [-1] * n
a[v] = 0
while q:
	x = q.popleft()
	for new_x in g[x]:
		if a[new_x] == -1:
			a[new_x] = a[x] + 1
			q.append(new_x)
max_ = 0
for i in range(n):
	if t[i] < a[i]:
		max_ = max(max_, a[i])
print(max_ - 1)


import sys
sys.setrecursionlimit(10 ** 9)
f = lambda : map(int, sys.stdin.readline().split())
(n, st, sa) = f()
g = [set() for _ in range(n)]
for _ in range(n - 1):
	(a, b) = f()
	g[a - 1].add(b - 1)
	g[b - 1].add(a - 1)

def dfs(l, v, p=-1, d=0):
	l[v] = d
	for c in g[v]:
		if c != p:
			dfs(l, c, v, d + 1)
lt = [0] * n
dfs(lt, st - 1)
la = [0] * n
dfs(la, sa - 1)
print(max((la[i] for i in range(n) if lt[i] < la[i])) - 1)


(N, u, v) = map(int, input().split())
G = [set() for i in range(N)]
for i in range(N - 1):
	(A, B) = map(int, input().split())
	G[A - 1].add(B - 1)
	G[B - 1].add(A - 1)
disA = [-1] * N
disB = [-1] * N
disA[u - 1] = 0
disB[v - 1] = 0
import collections
q = collections.deque([u - 1])
while q:
	x = q.popleft()
	for y in G[x]:
		if disA[y] == -1:
			disA[y] = disA[x] + 1
			q.append(y)
q.append(v - 1)
while q:
	x = q.popleft()
	for y in G[x]:
		if disB[y] == -1:
			disB[y] = disB[x] + 1
			q.append(y)
ans = 0
for i in range(N):
	if disA[i] < disB[i]:
		ans = max(ans, disB[i] - 1)
print(ans)


(N, u, v) = map(int, input().split())
AB = [list(map(int, input().split())) for i in range(N - 1)]
u -= 1
v -= 1
G = [set() for _ in range(N)]
for (a, b) in AB:
	G[a - 1].add(b - 1)
	G[b - 1].add(a - 1)
du = [-1] * N
du[u] = 0
q = set([u])
while q:
	i = q.pop()
	for j in G[i]:
		if du[j] == -1:
			du[j] = du[i] + 1
			q.add(j)
dv = [-1] * N
dv[v] = 0
q = set([v])
while q:
	i = q.pop()
	for j in G[i]:
		if dv[j] == -1:
			dv[j] = dv[i] + 1
			q.add(j)
ans = 0
for i in range(N):
	if du[i] < dv[i]:
		ans = max(ans, dv[i])
ans -= 1
print(ans)


import sys
input = sys.stdin.readline
from collections import deque
(N, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
X = [[] for i in range(N)]
for i in range(N - 1):
	(x, y) = map(int, input().split())
	X[x - 1].append(y - 1)
	X[y - 1].append(x - 1)
P = [-1] * N
Q = deque([v])
R = []
while Q:
	i = deque.popleft(Q)
	R.append(i)
	for a in X[i]:
		if a != P[i]:
			P[a] = i
			X[a].remove(i)
			deque.append(Q, a)
DE = [0] * N
for i in R[1:]:
	DE[i] = DE[P[i]] + 1
D = [0] * N
for i in R[::-1]:
	D[i] = max([D[j] for j in X[i]] + [DE[i]])
i = u
ma = D[i]
while True:
	i = P[i]
	if DE[i] * 2 <= DE[u]:
		break
	ma = max(ma, D[i])
print(ma - 1)


def resolve():

	def dfs(v):
		dist = [-1] * N
		stack = [v]
		dist[v] = 0
		while stack:
			v = stack.pop()
			for to in G[v]:
				if dist[to] != -1:
					continue
				dist[to] = dist[v] + 1
				stack.append(to)
		return dist
	(N, taka, aoki) = map(int, input().split())
	taka -= 1
	aoki -= 1
	G = [[] for _ in range(N)]
	for i in range(N - 1):
		(a, b) = map(lambda x: int(x) - 1, input().split())
		G[a].append(b)
		G[b].append(a)
	dist_Tk = dfs(taka)
	dist_Ao = dfs(aoki)
	ans = 0
	for i in range(N):
		if dist_Tk[i] < dist_Ao[i]:
			ans = max(ans, dist_Ao[i] - 1)
	print(ans)

def __starting_point():
	resolve()
__starting_point()


import sys
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
(n, u, v) = map(int, input().split())
graph = defaultdict(list)
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	graph[a].append(b)
	graph[b].append(a)

def dfs(u):
	dist = [-1] * (n + 1)
	stack = [u]
	dist[u] = 0
	while stack:
		u = stack.pop()
		nxt = dist[u] + 1
		for v in graph[u]:
			if dist[v] >= 0:
				continue
			dist[v] = nxt
			stack.append(v)
	return dist
dist_u = dfs(u)
dist_v = dfs(v)
ans = 0
for (x, y) in zip(dist_u, dist_v):
	if x < y:
		ans = max(ans, y - 1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 5)
(n, u, v) = map(int, input().split())
u = u - 1
v = v - 1
root = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	root[a - 1].append(b - 1)
	root[b - 1].append(a - 1)
dp = [[-1] * n for i in range(2)]
dp[0][u] = 0
dp[1][v] = 0

def dfs(node, depth, par, flag):
	for vertex in root[node]:
		if vertex == par or dp[flag][vertex] != -1:
			continue
		dp[flag][vertex] = depth + 1
		dfs(vertex, depth + 1, node, flag)
	return
dfs(u, 0, u, 0)
dfs(v, 0, v, 1)
buf = -1
buf1 = 0
for i in range(n):
	if dp[0][i] < dp[1][i] and dp[1][i] > buf:
		buf = dp[1][i]
		buf1 = dp[0][i]
print(buf - 1)


from collections import deque
import sys
input = sys.stdin.readline
(N, U, V) = list(map(int, input().split()))
U -= 1
V -= 1
G = [[] for _ in [0] * N]
for _ in [0] * (N - 1):
	(a, b) = list(map(int, input().split()))
	a -= 1
	b -= 1
	G[a].append(b)
	G[b].append(a)
INF = 10 ** 10
T = [INF] * N
T[U] = 0
A = [INF] * N
A[V] = 0

def bfs(v, dist):
	q = deque()
	q.append(v)
	k = 0
	visited = [0] * N
	while q:
		v = q.popleft()
		visited[v] = 1
		k = dist[v]
		for u in G[v]:
			if visited[u]:
				continue
			q.append(u)
			dist[u] = k + 1
bfs(U, T)
bfs(V, A)
if A[U] <= 1:
	print(0)
	return
farthest = 0
node = -1
for (i, (t, a)) in enumerate(zip(T, A)):
	if t < a and farthest < a:
		farthest = a
		node = i
print(farthest - 1)


from collections import deque
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
route = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(lambda x: int(x) - 1, input().split())
	route[a].append(b)
	route[b].append(a)
takyori = [-1 for i in range(n)]
takyori[u] = 0
d = deque()
d.append(u)
while d:
	now = d.popleft()
	for i in route[now]:
		if takyori[i] == -1:
			takyori[i] = takyori[now] + 1
			d.append(i)
aokyori = [-1 for i in range(n)]
aokyori[v] = 0
d = deque()
d.append(v)
while d:
	now = d.popleft()
	for i in route[now]:
		if aokyori[i] == -1:
			aokyori[i] = aokyori[now] + 1
			d.append(i)
ans = 0
for i in range(n):
	if takyori[i] <= aokyori[i]:
		ans = max(ans, aokyori[i] - 1)
print(ans)


from collections import deque
(N, U, V) = map(int, input().split())
U -= 1
V -= 1
tree = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	tree[a].append(b)
	tree[b].append(a)

def get_rank(v):
	rank = [-1] * N
	next_v = deque([v])
	rank[v] = 0
	while next_v:
		node = next_v.popleft()
		r = rank[node]
		for i in tree[node]:
			if rank[i] != -1:
				continue
			rank[i] = r + 1
			next_v.append(i)
	return rank
takahashi = get_rank(U)
aoki = get_rank(V)
ans = 0
for (x, y) in zip(takahashi, aoki):
	if x <= y:
		ans = max(ans, y - 1)
print(ans)


import sys
from collections import deque
input = sys.stdin.readline
(n, u, v) = map(int, input().split())
E = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	E[a - 1].append(b - 1)
	E[b - 1].append(a - 1)
memo = [[0, 0] for i in range(n)]

def dfs(cur, pre, p):
	stack = deque([[cur, pre]])
	while stack:
		(cur, pre) = stack.pop()
		if pre != -1:
			memo[cur][p] = memo[pre][p] + 1
		for e in E[cur]:
			if e != pre:
				stack.append([e, cur])
dfs(u - 1, -1, 0)
dfs(v - 1, -1, 1)
ans = 0
for (a, b) in memo:
	if a <= b:
		ans = max(ans, b - 1)
print(ans)


import sys
import math
import fractions
from collections import deque
from collections import defaultdict
stdin = sys.stdin
ns = lambda : stdin.readline().rstrip()
ni = lambda : int(stdin.readline().rstrip())
nm = lambda : list(map(int, stdin.readline().split()))
nl = lambda : list(map(int, stdin.readline().split()))
(N, u, v) = nm()
u = u - 1
v = v - 1
INF = 10 ** 18
edge = [[] for i in range(N)]

def bfs(start, edge):
	d = [INF] * N
	d[start] = 0
	q = deque([start])
	while q:
		e = q.popleft()
		for i in edge[e]:
			if d[e] + 1 < d[i]:
				d[i] = d[e] + 1
				q.append(i)
	return d
for i in range(N - 1):
	(A, B) = nm()
	edge[B - 1].append(A - 1)
	edge[A - 1].append(B - 1)
d_A = bfs(u, edge)
d_B = bfs(v, edge)
ans = 0
for i in range(N):
	if d_A[i] < d_B[i]:
		tmp = max(d_A[i], d_B[i])
		ans = max(ans, tmp)
print(ans - 1)


(n, u, v) = map(int, input().split())
ki = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	ki[a].append(b)
	ki[b].append(a)
dn = [len(ki[i]) for i in range(n)]
inf = float('inf')
dt = [inf] * n
da = [inf] * n
(u, v) = (u - 1, v - 1)
from collections import deque
todo = deque([u])
dt[u] = 0
while todo:
	x = todo.popleft()
	for nx in ki[x]:
		if dt[nx] > dt[x] + 1:
			dt[nx] = dt[x] + 1
			todo.append(nx)
todo = deque([v])
da[v] = 0
while todo:
	x = todo.popleft()
	for nx in ki[x]:
		if da[nx] > da[x] + 1:
			da[nx] = da[x] + 1
			todo.append(nx)
ans = 0
for i in range(n):
	if dn[i] > 1 and dt[i] <= da[i]:
		ans = max(ans, da[i])
print(ans)
exit()
print(dn)
print(dt)
print(da)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
(N, u, v) = list(map(int, input().split()))
(u, v) = (u - 1, v - 1)
adjL = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = list(map(int, input().split()))
	(a, b) = (a - 1, b - 1)
	adjL[a].append(b)
	adjL[b].append(a)

def bfsMinCosts(adjList, vSt, INF):
	numV = len(adjList)
	costs = [INF] * numV
	costs[vSt] = cost = 0
	vs = [vSt]
	while vs:
		cost += 1
		v2s = []
		for v in vs:
			for v2 in adjList[v]:
				if costs[v2] == INF:
					costs[v2] = cost
					v2s.append(v2)
		vs = v2s
	return costs
costUs = bfsMinCosts(adjL, u, INF)
costVs = bfsMinCosts(adjL, v, INF)
ans = 0
for x in range(N):
	if costUs[x] < costVs[x]:
		if costVs[x] > ans:
			ans = costVs[x]
print(ans - 1)


def bfs(k):
	flag = [-1] * (n + 1)
	que = [k]
	flag[k] = 0
	cnt = 1
	while que:
		h = []
		for uu in que:
			for vv in data[uu]:
				if flag[vv] == -1:
					flag[vv] = cnt
					h.append(vv)
		que = h
		cnt += 1
	return flag
(n, u, v) = map(int, input().split())
data = [[] for i in range(n + 1)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	data[a].append(b)
	data[b].append(a)
T = bfs(u)
A = bfs(v)
ans = 0
for i in range(1, n + 1):
	if T[i] < A[i]:
		ans = max(ans, A[i] - 1)
print(ans)


import sys
sr = lambda : sys.stdin.readline().rstrip()
ir = lambda : int(sr())
lr = lambda : list(map(int, sr().split()))
(N, U, V) = lr()
edges = [[] for _ in range(N + 1)]
for _ in range(N - 1):
	(a, b) = lr()
	edges[a].append(b)
	edges[b].append(a)

def dfs(v):
	dist = [-1] * (N + 1)
	dist[v] = 0
	stack = [v]
	while stack:
		s = stack.pop()
		for x in edges[s]:
			if dist[x] != -1:
				continue
			dist[x] = dist[s] + 1
			stack.append(x)
	return dist
(DU, DV) = (dfs(U), dfs(V))
answer = 0
for (u, v) in zip(DU[1:], DV[1:]):
	if v > u:
		x = v - 1
		if x > answer:
			answer = x
print(answer)


(n, u, v) = map(int, input().split())
edges = [[] for i in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	edges[a - 1].append(b - 1)
	edges[b - 1].append(a - 1)
aolevel = [-1] * n
aolevel[v - 1] = 0
q = [v - 1]
while len(q) > 0:
	i = q.pop(0)
	for j in edges[i]:
		if aolevel[j] < 0:
			aolevel[j] = aolevel[i] + 1
			q.append(j)
ret = 0
takalevel = [-1] * n
takalevel[u - 1] = 0
q = [u - 1]
while len(q) > 0:
	i = q.pop(0)
	ret = max(ret, aolevel[i] - 1)
	if takalevel[i] < aolevel[i]:
		for j in edges[i]:
			if takalevel[j] < 0:
				takalevel[j] = takalevel[i] + 1
				q.append(j)
print(ret)


(N, u, v) = map(int, input().split())
u -= 1
v -= 1
tonari = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	tonari[a].append(b)
	tonari[b].append(a)
d_from_u = [float('inf') for _ in range(N)]
d_from_v = [float('inf') for _ in range(N)]
d_from_u[u] = 0
d_from_v[v] = 0
now = [u]
d = 0
while now:
	d += 1
	nextvisit = []
	for n in now:
		for t in tonari[n]:
			if d_from_u[t] > d:
				d_from_u[t] = d
				nextvisit.append(t)
	now = nextvisit
now = [v]
d = 0
while now:
	d += 1
	nextvisit = []
	for n in now:
		for t in tonari[n]:
			if d_from_v[t] > d:
				d_from_v[t] = d
				nextvisit.append(t)
	now = nextvisit
distance = []
for (U, V) in zip(d_from_u, d_from_v):
	distance.append((U, V))
distance.sort(key=lambda x: x[1])
ans = 0
while distance:
	d = distance.pop()
	if d[0] < d[1]:
		ans = d[1] - 1
		break
print(ans)


import sys
sys.setrecursionlimit(10 ** 8)

def dfs(v, dist, d=0, p=-1):
	dist[v] = d
	for u in to[v]:
		if u == p:
			continue
		dfs(u, dist, d + 1, v)

def calcDist(s, dist):
	dfs(s, dist)
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
to = [[] for _ in range(n + 1)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	to[a].append(b)
	to[b].append(a)
distS = [0] * n
distT = [0] * n
calcDist(u, distS)
calcDist(v, distT)
mx = 0
for i in range(n):
	if distS[i] < distT[i]:
		mx = max(mx, distT[i])
ans = mx - 1
print(ans)


def main():
	(n, u, v) = list(map(int, input().split()))
	u -= 1
	v -= 1
	adj = [[] for i in range(n)]
	for i in range(n - 1):
		(a, b) = list(map(int, input().split()))
		a -= 1
		b -= 1
		adj[a].append(b)
		adj[b].append(a)
	st = [u]
	prev = [None for i in range(n)]
	while st:
		x = st.pop()
		for y in adj[x]:
			if y == prev[x]:
				continue
			prev[y] = x
			st.append(y)
	path = [v]
	while path[-1] != u:
		path.append(prev[path[-1]])
	path = list(reversed(path))
	l = len(path) - 1
	cross = path[(l - 1) // 2]
	square = path[(l - 1) // 2 + 1]
	st = [(cross, 0)]
	prev = [None for i in range(n)]
	dist = [-1 for i in range(n)]
	while st:
		(x, d) = st.pop()
		dist[x] = d
		for y in adj[x]:
			if y == prev[x]:
				continue
			if y == square:
				continue
			prev[y] = x
			st.append((y, d + 1))
	star_square = max(dist)
	if l % 2 == 1:
		res = (l - 1) // 2 + star_square
	else:
		res = (l - 1) // 2 + star_square + 1
	print(res)

def __starting_point():
	main()
__starting_point()


import queue
(n, u, v) = map(int, input().split())
inf = 10 ** 6
ans = 0
edges = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	edges[a - 1].append(b - 1)
	edges[b - 1].append(a - 1)

def bfs(d, start):
	d[start] = 0
	que = queue.Queue()
	que.put(start)
	while not que.empty():
		x = que.get()
		for to in edges[x]:
			if d[to] == inf:
				que.put(to)
				d[to] = d[x] + 1
(du, dv) = ([inf] * n, [inf] * n)
bfs(du, u - 1)
bfs(dv, v - 1)
for i in range(n):
	if dv[i] > du[i]:
		ans = max(ans, dv[i] - 1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 7)
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
ki = [[] for i in range(n)]
distt = [0] * n
dista = [0] * n
for i in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	ki[a].append(b)
	ki[b].append(a)

def dfst(now, last=-1):
	for next in ki[now]:
		if next == last:
			continue
		distt[next] = distt[now] + 1
		dfst(next, now)

def dfsa(now, last=-1):
	for next in ki[now]:
		if next == last:
			continue
		dista[next] = dista[now] + 1
		dfsa(next, now)
dfst(u)
dfsa(v)
ans = 0
for i in range(n):
	if dista[i] <= distt[i]:
		continue
	ans = max(ans, dista[i] - 1)
print(ans)


from collections import deque
(n, u, v) = map(int, input().split())
edge = [[] for i in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	edge[a].append(b)
	edge[b].append(a)
visitedu = [-1] * n
visitedv = [-1] * n

def bfs(s, visited):
	q = deque([s])
	visited[s] = 0
	while q:
		x = q.popleft()
		for nx in edge[x]:
			if visited[nx] == -1:
				visited[nx] = visited[x] + 1
				q.append(nx)
	return
bfs(u - 1, visitedu)
bfs(v - 1, visitedv)
a = 0
b = 10 ** 10
for i in range(n):
	if visitedu[i] < visitedv[i]:
		a = max(a, visitedv[i])
	elif visitedu[i] == visitedv[i]:
		b = min(b, visitedv[i])
if b == 10 ** 10:
	b = -1
if a <= b:
	print(b)
else:
	print(a - 1)


import sys
sys.setrecursionlimit(10 ** 5)
input = sys.stdin.readline
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
Tree = [[] for _ in range(N)]
for _ in range(N - 1):
	(A, B) = map(lambda x: int(x) - 1, input().split())
	Tree[A].append(B)
	Tree[B].append(A)
path = []

def connected(v, tv, p=-1):
	if v == tv:
		return True
	for w in Tree[v]:
		if w == p:
			continue
		elif connected(w, tv, v):
			path.append(w)
			return True
	return False
connected(v, u)
path.append(v)

def dfs(v, p):
	d = -1
	for w in Tree[v]:
		if w == p:
			continue
		else:
			d = max(d, dfs(w, v))
	return d + 1
dist = len(path)
mid = path[dist // 2 - 1]
par = path[dist // 2]
ans = dfs(mid, par)
print(ans + (dist - 1) // 2)


import sys
sys.setrecursionlimit(10 ** 6)
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
graph = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	graph[a - 1].append(b - 1)
	graph[b - 1].append(a - 1)
d1 = [None] * n
d2 = [None] * n

def dfs(node, d, p_node=-1):
	for c_node in graph[node]:
		if c_node == p_node:
			continue
		d[c_node] = d[node] + 1
		dfs(c_node, d, node)
d1[u] = 0
d2[v] = 0
dfs(u, d1)
dfs(v, d2)
ans = 0
for (i, j) in zip(d1, d2):
	if i < j:
		ans = max(ans, j - 1)
print(ans)


from collections import deque, defaultdict
from copy import deepcopy
import sys
import resource
sys.setrecursionlimit(10 ** 5)
(N, u, v) = list(map(int, input().split()))
graph = defaultdict(deque)
for _ in range(1, N):
	(a, b) = list(map(int, input().split()))
	graph[a].append(b)
	graph[b].append(a)
depth = dict(list(zip(list(range(1, N + 1)), [-1] * N)))

def bfs(s):
	if graph[s]:
		for v in graph[s]:
			if depth[v] == -1:
				depth[v] = depth[s] + 1
				bfs(v)
depth = dict(list(zip(list(range(1, N + 1)), [-1] * N)))
depth[v] = 0
bfs(v)
depth_v = deepcopy(depth)
depth = dict(list(zip(list(range(1, N + 1)), [-1] * N)))
depth[u] = 0
bfs(u)
depth_u = depth
depth_v = sorted(list(depth_v.items()), key=lambda x: x[0])
depth_u = sorted(list(depth_u.items()), key=lambda x: x[0])
MAX = 0
for (v, u) in zip(depth_v, depth_u):
	if u[1] < v[1]:
		MAX = max(MAX, v[1])
print(MAX - 1)


import sys
sys.setrecursionlimit(1000000)
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
graph = [[] for i in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	graph[a].append(b)
	graph[b].append(a)
lst = []

def calcDist(cur, par, dst):
	global lst
	lst[cur] = dst
	for neighbour in graph[cur]:
		if neighbour == par:
			continue
		calcDist(neighbour, cur, dst + 1)
uDist = [None for i in range(N)]
vDist = [None for i in range(N)]
lst = uDist
calcDist(u, -1, 0)
lst = vDist
calcDist(v, -1, 0)
best = 0
for node in range(N):
	if uDist[node] < vDist[node]:
		best = max(best, vDist[node] - 1)
print(best)


import sys
sys.setrecursionlimit(10 ** 7)

def dfs(v, p, d):
	dist[v] = d
	for nv in G[v]:
		if nv == p:
			continue
		dfs(nv, v, d + 1)

def getSP(v):
	p = v
	if dist[v] == lim:
		return p
	for nv in G[v]:
		if dist[nv] < dist[v]:
			p = getSP(nv)
	return p

def depth(v, p, d):
	nonlocal maxd
	maxd = max(maxd, dist[v])
	for nv in G[v]:
		if dist[nv] < dist[v]:
			continue
		depth(nv, v, d + 1)
(N, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
if u == v:
	print(0)
	return
G = [[] for i in range(N)]
for i in range(N - 1):
	(a, b) = map(lambda x: int(x) - 1, input().split())
	G[a].append(b)
	G[b].append(a)
dist = [-1] * N
dfs(v, -1, 0)
lim = dist[u] - (dist[u] - 1) // 2
SP = getSP(u)
maxd = -1
depth(SP, -1, 0)
print(maxd - 1)


import sys
sys.setrecursionlimit(10000000)
import math
import bisect

def input():
	return sys.stdin.readline()[:-1]
(n, u, v) = list(map(int, input().split()))
u -= 1
v -= 1
g = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = list(map(int, input().split()))
	a -= 1
	b -= 1
	g[a].append(b)
	g[b].append(a)
seen = [-1 for i in range(n)]
taka = []
aok = []

def dfs(s, lis):
	nec = g[s]
	if len(nec) == 1:
		lis.append(s)
	for i in nec:
		if seen[i] != -1:
			continue
		seen[i] = seen[s] + 1
		dfs(i, lis)
seen[u] = 0
dfs(u, taka)
takadist = [i for i in seen]
seen = [-1 for i in range(n)]
seen[v] = 0
dfs(v, aok)
ans = 0
for i in aok:
	if takadist[i] < seen[i]:
		ans = max(ans, seen[i])
if g[u] == [v]:
	print(0)
else:
	print(ans - 1)


import os
import sys
import numpy as np

def solve(N, U, V, AB):
	G = [[] for _ in range(N + 1)]
	for ab in AB:
		(a, b) = ab
		G[a].append(b)
		G[b].append(a)
	P1 = np.zeros(N + 1, dtype=np.int64)

	def dfs1(u):
		st = [u]
		while st:
			v = st.pop()
			p = P1[v]
			for u in G[v]:
				if p != u:
					st.append(u)
					P1[u] = v
	dfs1(U)
	path_u2v = [U]
	v = V
	while v != U:
		v = P1[v]
		path_u2v.append(v)
	path_u2v.reverse()
	l = len(path_u2v)
	half = (l - 2) // 2
	c = path_u2v[half]
	ng = path_u2v[half + 1]
	Depth = np.zeros(N + 1, dtype=np.int64)

	def dfs2(c):
		st = [c]
		P = np.zeros(N + 1, dtype=np.int64)
		while st:
			v = st.pop()
			p = P[v]
			d = Depth[v]
			for u in G[v]:
				if p != u and u != ng:
					st.append(u)
					P[u] = v
					Depth[u] = d + 1
	dfs2(c)
	c_ = path_u2v[l - 1 - half]
	v = c_
	while v != c:
		Depth[v] = 0
		v = P1[v]
	d = l % 2
	ans = max(Depth) + half + d
	return ans

def main():
	(N, u, v) = list(map(int, input().split()))
	if N == 2:
		print(0)
		return
	AB = np.array(sys.stdin.read().split(), dtype=np.int64).reshape(N - 1, 2)
	ans = solve(N, u, v, AB)
	print(ans)
main()


import sys
sys.setrecursionlimit(200000)
(n, u, v) = map(int, input().split())
edge = [[] for i in range(n + 1)]
distt = [0 for i in range(n + 1)]
dista = [0 for i in range(n + 1)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	edge[a].append(b)
	edge[b].append(a)

def tdfs(x, last=-1):
	for to in edge[x]:
		if to == last:
			continue
		distt[to] = distt[x] + 1
		tdfs(to, x)

def adfs(x, last=-1):
	for to in edge[x]:
		if to == last:
			continue
		dista[to] = dista[x] + 1
		adfs(to, x)
tdfs(u)
adfs(v)
ans = 0
for i in range(1, n + 1):
	if distt[i] >= dista[i]:
		continue
	l = distt[i]
	k = dista[i] - distt[i]
	ans = max(ans, k - 1 + l)
print(ans)


(N, u, v) = map(int, input().split())
edge = [set() for i in range(N)]
u -= 1
v -= 1
for i in range(N - 1):
	(a, b) = map(int, input().split())
	edge[a - 1].add(b - 1)
	edge[b - 1].add(a - 1)

def calcDepth(root):
	depth = [-1] * N
	stack = [(root, 0)]
	while stack:
		(cur, curD) = stack.pop()
		depth[cur] = curD
		for c in edge[cur]:
			if depth[c] != -1:
				continue
			stack.append((c, curD + 1))
	return depth
Adepth = calcDepth(v)
Ddepth = calcDepth(u)
ans = 0
for i in range(N):
	if Adepth[i] >= Ddepth[i]:
		ans = max(ans, Adepth[i])
print(ans - 1)


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
(n, u, v) = map(int, readline().split())
m = map(int, read().split())
data = list(zip(m, m))
graph = [[] for _ in range(n + 1)]
for (a, b) in data:
	graph[a].append(b)
	graph[b].append(a)

def dfs(v):
	dist = [None] * (n + 1)
	dist[v] = 0
	stack = [v]
	while stack:
		x = stack.pop()
		for y in graph[x]:
			if dist[y] is None:
				dist[y] = dist[x] + 1
				stack.append(y)
	return dist
res = 0
(du, dv) = (dfs(u), dfs(v))
for i in range(1, n + 1):
	dvi = dv[i]
	if du[i] <= dvi:
		if dvi > res:
			res = dvi
print(res - 1)


(N, u, v) = map(int, input().split())
l = []
from collections import defaultdict
from collections import deque
d = defaultdict(list)
for i in range(N - 1):
	(ta, tb) = map(int, input().split())
	d[ta - 1].append(tb - 1)
	d[tb - 1].append(ta - 1)

def dfs(x):
	l = deque()
	l.append(x - 1)
	dp = [-1] * N
	dp[x - 1] = 0
	while l:
		t = l.pop()
		for i in d[t]:
			if dp[i] == -1:
				dp[i] = dp[t] + 1
				l.append(i)
	return dp
distu = dfs(u)
distv = dfs(v)
M = 0
for (i, j) in zip(distu, distv):
	if i < j:
		M = max(M, j - 1)
print(M)


(n, u, v) = map(int, input().split())
(u, v) = (u - 1, v - 1)
link = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	link[a].append(b)
	link[b].append(a)
tree = [[] for i in range(n)]
for i in [u, v]:
	stack = [i]
	vi = [0] * n
	tree[i].append(0)
	vi[i] = 1
	while stack:
		cur = stack.pop()
		for j in link[cur]:
			if vi[j] == 0:
				stack.append(j)
				vi[j] = 1
				tree[j].append(tree[cur][-1] + 1)
m = 0
c = 0
for (i, j) in tree:
	if i < j:
		if m < j:
			m = j
print(m - 1)


import sys
from collections import deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
in_n = lambda : int(readline())
in_nn = lambda : list(map(int, readline().split()))
in_s = lambda : readline().rstrip().decode('utf-8')
in_nl = lambda : list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda : [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda : list(map(int, read().split()))

def bfs(N, v0, edge):
	search = [-1] * N
	search[v0] = 0
	q = deque()
	q.append(v0)
	while q:
		v = q.popleft()
		for nv in edge[v]:
			if search[nv] == -1:
				q.append(nv)
				search[nv] = search[v] + 1
	return search

def bfs2(N, v0, edge, dis):
	q = deque()
	q.append(v0)
	max_dis = dis[v0]
	while q:
		v = q.popleft()
		for nv in edge[v]:
			if dis[v] < dis[nv]:
				q.append(nv)
				max_dis = max(max_dis, dis[nv])
	return max_dis

def main():
	(N, taka, aoki) = in_nn()
	(taka, aoki) = (taka - 1, aoki - 1)
	edge = [[] for _ in range(N)]
	for i in range(N - 1):
		(x, y) = in_nn()
		(x, y) = (x - 1, y - 1)
		edge[x].append(y)
		edge[y].append(x)
	dis = bfs(N, aoki, edge)
	if dis[taka] > 2:
		x = (dis[taka] + 1) // 2 - 1
		for _ in range(x):
			for v in edge[taka]:
				if dis[v] < dis[taka]:
					taka = v
					break
	ans = bfs2(N, taka, edge, dis) - 1
	print(ans)

def __starting_point():
	main()
__starting_point()


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

def dfs(node):
	sta = [node]
	dis = [-1] * (N + 1)
	dis[node] = 0
	while sta:
		no = sta.pop()
		tempD = dis[no] + 1
		for tempN in tree[no]:
			if dis[tempN] < 0:
				dis[tempN] = tempD
				sta.append(tempN)
	else:
		return dis
disU = dfs(u)
disV = dfs(v)
ans = 0
for (u_, v_) in zip(disU[1:], disV[1:]):
	if u_ < v_:
		ans = max(ans, v_)
else:
	print(ans - 1)


from collections import deque
(n, u, v) = map(int, input().split())
G = [[] for _ in range(n + 1)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	G[a].append(b)
	G[b].append(a)

def dfs(x, n):
	stack = deque([x])
	dist = [-1] * (n + 1)
	dist[x] = 0
	while len(stack) > 0:
		a = stack.pop()
		for k in G[a]:
			if dist[k] < 0:
				dist[k] = dist[a] + 1
				stack.append(k)
	return dist
distt = dfs(u, n)
dista = dfs(v, n)
ans = 0
for i in range(1, n + 1):
	if distt[i] < dista[i]:
		ans = max(ans, dista[i] - 1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
(n, u, v) = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	adj[a - 1].append(b - 1)
	adj[b - 1].append(a - 1)
tkhs = [-1 for _ in range(n)]
aoki = [-1 for _ in range(n)]
(tkhs[u - 1], aoki[v - 1]) = (0, 0)

def dfs(x, player):
	for y in adj[x]:
		if player[y] < 0:
			player[y] = player[x] + 1
			dfs(y, player)
	return
dfs(u - 1, tkhs)
dfs(v - 1, aoki)
ans = 0
for i in range(n):
	if tkhs[i] <= aoki[i] and len(adj[i]) > 1:
		ans = max(ans, aoki[i])
print(ans)


import sys
from collections import defaultdict
import heapq
g = defaultdict(list)
input = sys.stdin.buffer.readline
(N, u, v) = map(int, input().split())
for i in [0] * (N - 1):
	(A, B) = map(int, input().split())
	g[A].append(B)
	g[B].append(A)

def dijkstra(start):
	inf = 10 ** 10
	d = [inf] * (N + 1)
	d[start] = 0
	que = []
	heapq.heappush(que, (start, 0))
	while que:
		(s, dist) = heapq.heappop(que)
		if d[s] < dist:
			continue
		for t in g[s]:
			if d[t] > d[s] + 1:
				d[t] = d[s] + 1
				heapq.heappush(que, (t, d[t]))
	return d
d1 = dijkstra(u)
d2 = dijkstra(v)
ans = 0
for i in range(1, N + 1):
	if d1[i] < d2[i]:
		ans = max(ans, d2[i] - 1)
print(ans)


from heapq import heappop, heappush
(N, u, v) = map(int, input().split())
edges = [[] for i in range(N + 1)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	edges[a].append(b)
	edges[b].append(a)

def dfs(u):
	colors = ['white'] * (N + 1)
	d = [9999999999999999] * (N + 1)
	d[u] = 0
	stack = [u]
	while stack:
		u = stack.pop()
		colors[u] = 'black'
		for e in edges[u]:
			if colors[e] != 'black':
				d[e] = d[u] + 1
				stack.append(e)
	return d
u_dist = dfs(u)
v_dist = dfs(v)
ans = 0
for i in range(N + 1):
	if u_dist[i] < v_dist[i]:
		ans = max(ans, v_dist[i] - 1)
print(ans)


import sys
input = sys.stdin.readline
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
F = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	F[a].append(b)
	F[b].append(a)
a_dist = [-1] * N
t_dist = [-1] * N
a_dist[v] = 0
t_dist[u] = 0

def dfs(p):
	dist = [-1] * N
	stack = [p]
	dist[p] = 0
	while stack:
		q = stack.pop()
		for nq in F[q]:
			if dist[nq] == -1:
				dist[nq] = dist[q] + 1
				stack.append(nq)
	return dist
a_dist = dfs(v)
t_dist = dfs(u)
ans = 0
for (t, a) in zip(t_dist, a_dist):
	if t < a:
		temp = a - 1
		if ans < temp:
			ans = temp
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
(N, u, v) = map(int, input().split())
tree = [[] for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	tree[a - 1].append(b - 1)
	tree[b - 1].append(a - 1)

def calc_dist(dlist, n, dist, nnode):
	for c in tree[n]:
		if c == nnode:
			continue
		dlist[c] = dist
		calc_dist(dlist, c, dist + 1, n)
u_dist_list = [0] * N
calc_dist(u_dist_list, u - 1, 1, -1)
v_dist_list = [0] * N
calc_dist(v_dist_list, v - 1, 1, -1)
ans = 0
for i in range(N):
	if v_dist_list[i] - u_dist_list[i] > 0 and v_dist_list[i] > ans:
		ans = v_dist_list[i] - 1
print(ans)


from collections import deque
(N, u, v) = map(int, input().split())
edge = [[] for i in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	edge[a - 1].append(b - 1)
	edge[b - 1].append(a - 1)

def bfs(s):
	dist = [-1] * N
	que = deque([s - 1])
	dist[s - 1] = 0
	while que:
		v = que.popleft()
		d = dist[v]
		for w in edge[v]:
			if dist[w] > -1:
				continue
			dist[w] = d + 1
			que.append(w)
	return dist
(ud, vd) = (bfs(u), bfs(v))
ans = 0
for (ud_i, vd_i) in zip(ud, vd):
	ans = max(ans, vd_i - 1) if ud_i < vd_i else ans
print(ans)


import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
(n, u, v) = map(int, input().split())
node = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	node[a - 1].append(b - 1)
	node[b - 1].append(a - 1)

def dfs(i):
	visited[i] = 1
	for x in node[i]:
		if visited[x] == 0:
			dis[x] = dis[i] + 1
			dfs(x)
inf = 10 ** 9
dis = [inf] * n
dis[u - 1] = 0
visited = [0] * n
dfs(u - 1)
dis2 = []
from copy import copy
dis_dash = copy(dis)
dis2.append(dis_dash)
dis[v - 1] = 0
visited = [0] * n
dfs(v - 1)
dis2.append(dis)
cnt = 0
for i in range(n):
	if dis2[0][i] < dis2[1][i]:
		cnt = max(cnt, dis2[1][i])
print(cnt - 1)


import sys
sys.setrecursionlimit(10 ** 6)

def get_distance(v):
	distance = [0] * n
	visited = [0] * n
	visited[v] = 1
	dfs(v, 0, distance, visited)
	return distance

def dfs(v, d, distance, visited):
	distance[v] = d
	for u in adj[v]:
		if not visited[u]:
			visited[u] = 1
			dfs(u, d + 1, distance, visited)
(n, u, v) = map(int, input().split())
adj = [[] for _ in range(n)]
for _ in range(n - 1):
	(a, b) = (int(i) - 1 for i in input().split())
	adj[a].append(b)
	adj[b].append(a)
u_distance = get_distance(u - 1)
v_distance = get_distance(v - 1)
ans = 0
for (u, v) in zip(u_distance, v_distance):
	if u < v and v - 1 > ans:
		ans = v - 1
print(ans)


import sys

def input():
	return sys.stdin.readline().rstrip()
from collections import deque

def main():
	(n, u, v) = map(int, input().split())
	if u == v:
		print(0)
		return
	graph = [[] for _ in range(n)]
	depth_u = [-1] * n
	depth_v = [-1] * n
	u -= 1
	v -= 1
	for i in range(n - 1):
		(a, b) = map(int, input().split())
		graph[a - 1].append(b - 1)
		graph[b - 1].append(a - 1)
	queue = deque([u])
	visited = [False] * n
	visited[u] = True
	depth_u[u] = 0
	while queue:
		node = queue.popleft()
		for xnode in graph[node]:
			if not visited[xnode]:
				visited[xnode] = True
				depth_u[xnode] = depth_u[node] + 1
				queue.append(xnode)
	queue = deque([v])
	visited = [False] * n
	visited[v] = True
	depth_v[v] = 0
	while queue:
		node = queue.popleft()
		for xnode in graph[node]:
			if not visited[xnode]:
				visited[xnode] = True
				depth_v[xnode] = depth_v[node] + 1
				queue.append(xnode)
	ans = 0
	max_d_v = 0
	for i in range(n):
		if depth_u[i] < depth_v[i]:
			max_d_v = max(max_d_v, depth_v[i])
	print(max_d_v - 1)

def __starting_point():
	main()
__starting_point()


from collections import deque
INF = -1
(n, t, a) = map(int, input().split())
t -= 1
a -= 1
g = [[] for _ in range(n)]
for _ in range(n - 1):
	(u, v) = map(lambda x: int(x) - 1, input().split())
	g[u].append(v)
	g[v].append(u)

def bfs(start):
	dist = [INF] * n
	dist[start] = 0
	q = deque([start])
	while q:
		cur = q.pop()
		for to in g[cur]:
			if dist[to] == INF:
				dist[to] = 1 + dist[cur]
				q.appendleft(to)
	return dist
distT = bfs(t)
distA = bfs(a)
ret = 0
for v in range(n):
	if distT[v] < distA[v]:
		ret = max(ret, distA[v] - 1)
print(ret)


import heapq
import sys
sys.setrecursionlimit(10 ** 6)
(N, u, v) = map(int, input().split())
es = [[] for _ in range(N + 1)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	es[a].append(b)
	es[b].append(a)
INF = float('inf')
from_u = [INF] * (N + 1)
from_u[u] = -1
from_v = [INF] * (N + 1)
from_v[v] = -1

def solve(prev, curr, distances):
	distances[curr] = distances[prev] + 1
	for next in es[curr]:
		if distances[next] == INF:
			solve(curr, next, distances)
solve(u, u, from_u)
solve(v, v, from_v)
ans = 0
for i in range(1, N + 1):
	if from_u[i] < from_v[i]:
		ans = max(ans, from_v[i] - 1)
print(ans)


from collections import deque
(N, u, v, *AB) = map(int, open(0).read().split())
E = [[] for _ in range(N + 1)]
for (a, b) in zip(*[iter(AB)] * 2):
	E[a].append(b)
	E[b].append(a)

def solve(s):
	D = [-1] * (N + 1)
	D[s] = 0
	Q = deque([s])
	while Q:
		a = Q.popleft()
		for b in E[a]:
			if D[b] != -1:
				continue
			D[b] = D[a] + 1
			Q.append(b)
	return D[1:]
T = solve(u)
A = solve(v)
print(-1 + max((a for (t, a) in zip(T, A) if t < a)))


def resolve():
	(N, u, v) = list(map(int, input().split()))
	(u, v) = (u - 1, v - 1)
	adjs = [[] for _ in range(N)]
	for _ in range(N - 1):
		(a, b) = list([int(x) - 1 for x in input().split()])
		adjs[a].append(b)
		adjs[b].append(a)
	dist_from_u = [float('inf') for _ in range(N)]
	dist_from_u[u] = 0
	import collections
	q = collections.deque([u])
	while q:
		node = q.pop()
		for adj in adjs[node]:
			if dist_from_u[adj] > dist_from_u[node] + 1:
				dist_from_u[adj] = dist_from_u[node] + 1
				q.appendleft(adj)
	dist_from_v = [float('inf') for _ in range(N)]
	dist_from_v[v] = 0
	import collections
	q = collections.deque([v])
	while q:
		node = q.pop()
		for adj in adjs[node]:
			if dist_from_v[adj] > dist_from_v[node] + 1:
				dist_from_v[adj] = dist_from_v[node] + 1
				q.appendleft(adj)
	length = 0
	for i in range(N):
		if dist_from_u[i] < dist_from_v[i] and length < dist_from_v[i]:
			length = dist_from_v[i]
	print(length - 1)

def __starting_point():
	resolve()
__starting_point()


import sys
sys.setrecursionlimit(10 ** 5)

def search(n, total, dic):
	if dic[n] >= 0:
		return
	else:
		dic[n] = total
	for i in br[n]:
		search(i, total + 1, dic)
(n, u, v) = map(int, input().split())
br = {}
disu = {}
disv = {}
for i in range(n):
	br[i + 1] = []
	disu[i + 1] = -1
	disv[i + 1] = -1
for i in range(n - 1):
	(a, b) = map(int, input().split())
	br[a].append(b)
	br[b].append(a)
search(u, 0, disu)
search(v, 0, disv)
far = 0
for i in range(1, n + 1):
	if disv[i] < disu[i]:
		continue
	else:
		far = max(far, disv[i])
print(far - 1)


import sys
sys.setrecursionlimit(1000000)
(N, u, v) = map(int, input().split())
G = {k: [] for k in range(N + 1)}
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	G[a].append(b)
	G[b].append(a)

def dfs(ci, d, p, dist):
	dist[ci] = d
	for ni in G[ci]:
		if ni == p:
			continue
		dfs(ni, d + 1, ci, dist)

def calcDist(start):
	dist = [-1] * (N + 1)
	dfs(start, 0, -1, dist)
	return dist
distU = calcDist(u)
distV = calcDist(v)
ans = 0
for i in range(1, N + 1):
	if distU[i] < distV[i]:
		ans = max(ans, distV[i] - 1)
print(ans)


(n, u, v) = map(int, input().split())
tree = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	tree[a - 1].append(b - 1)
	tree[b - 1].append(a - 1)
va = [-1] * n
q = [v - 1]
va[v - 1] = 0
while q:
	x = q.pop()
	for j in tree[x]:
		if va[j] == -1:
			q.append(j)
			va[j] = va[x] + 1
q = [u - 1]
vt = [-1] * n
vt[u - 1] = 0
ans = 0
while q:
	x = q.pop()
	ans = max(ans, va[x])
	for j in tree[x]:
		if vt[j] == -1:
			if vt[x] + 1 < va[j]:
				q.append(j)
				vt[j] = vt[x] + 1
			elif vt[x] + 1 == va[j]:
				vt[j] = vt[x] + 1
print(ans - 1)


ma = lambda : list(map(int, input().split()))
(n, u, v) = ma()
(u, v) = (u - 1, v - 1)
tree = [[] for i in range(n)]
import collections
for i in range(n - 1):
	(a, b) = ma()
	tree[a - 1].append(b - 1)
	tree[b - 1].append(a - 1)
que = collections.deque([(v, 0)])
vis = [False] * n
dist_v = [0] * n
while que:
	(now, c) = que.popleft()
	vis[now] = True
	dist_v[now] = c
	for node in tree[now]:
		if not vis[node]:
			que.append((node, c + 1))
que = collections.deque([(u, 0)])
vis = [False] * n
dist_u = [0] * n
while que:
	(now, c) = que.popleft()
	vis[now] = True
	dist_u[now] = c
	for node in tree[now]:
		if not vis[node]:
			que.append((node, c + 1))
ans = 0
for i in range(n):
	if dist_u[i] < dist_v[i]:
		ans = max(ans, dist_v[i])
print(ans - 1)


import copy
import sys
sys.setrecursionlimit(10 ** 9)

def dfs(G, v, p, d, depth):
	depth[v] = d
	for nv in G[v]:
		if nv == p:
			continue
		dfs(G, nv, v, d + 1, depth)
	return (G, depth)
(n, T, A) = map(int, input().split())
(T, A) = (T - 1, A - 1)
g = [[] for _ in range(n)]
for i in range(n - 1):
	(x, y) = map(int, input().split())
	(x, y) = (x - 1, y - 1)
	g[x].append(y)
	g[y].append(x)
depth = [0] * n
GT = copy.deepcopy(g)
GT[T].append(-1)
(GT, dt) = dfs(GT, T, -1, 0, depth)
depth = [0] * n
GA = copy.deepcopy(g)
GA[A].append(-1)
(GA, da) = dfs(GA, A, -1, 0, depth)
ans = 0
for (t, a) in zip(dt, da):
	if t <= a:
		ans = max(ans, a)
print(ans - 1)


import sys
sys.setrecursionlimit(2 * 10 ** 5)
(n, u, v) = map(int, input().split())
edge = [tuple(map(int, input().split())) for _ in range(n - 1)]
u -= 1
v -= 1
connect = [set() for _ in range(n)]
for (a, b) in edge:
	connect[a - 1].add(b - 1)
	connect[b - 1].add(a - 1)
du = [0] * n
dv = [0] * n

def dfs(v, dis, ng, d):
	d[v] = dis
	ng.add(v)
	for w in connect[v]:
		if w not in ng:
			dfs(w, dis + 1, ng, d)
dfs(u, 0, set(), du)
dfs(v, 0, set(), dv)
ans = 0
for i in range(n):
	if du[i] < dv[i]:
		ans = max(ans, dv[i] - 1)
print(ans)


from collections import deque
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
L = [list() for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	L[a].append(b)
	L[b].append(a)
q = deque([(v, 0)])
T = [-1] * N
A = [-1] * N
while q:
	(p, x) = q.pop()
	A[p] = x
	for to in L[p]:
		if A[to] == -1:
			q.append((to, x + 1))
q = deque([(u, 0)])
while q:
	(p, x) = q.pop()
	T[p] = x
	for to in L[p]:
		if T[to] == -1 and A[to] > x + 1:
			q.append((to, x + 1))
Tcan = [i for (i, x) in enumerate(T) if x != -1]
(cnt, gi) = max([(A[i], i) for i in Tcan] + [(0, 0)])
if cnt <= 1:
	print(0)
	exit()
print(A[gi] - 1)


(n, u, v, *L) = map(int, open(0).read().split())
dist_f_v = [-1] * n
con = [[] for _ in range(n)]
for (s, t) in zip(*[iter(L)] * 2):
	con[s - 1].append(t - 1)
	con[t - 1].append(s - 1)
dist_f_v[v - 1] = 0
q = [v - 1]
while q:
	cur = q.pop()
	for nxt in con[cur]:
		if dist_f_v[nxt] < 0:
			dist_f_v[nxt] = dist_f_v[cur] + 1
			q.append(nxt)
dist_f_u = [-1] * n
dist_f_u[u - 1] = 0
q = [u - 1]
m = 0
while q:
	cur = q.pop()
	m = max(m, dist_f_v[cur])
	for nxt in con[cur]:
		if dist_f_u[nxt] > -1:
			continue
		if dist_f_u[cur] + 1 < dist_f_v[nxt]:
			dist_f_u[nxt] = dist_f_u[cur] + 1
			q.append(nxt)
		elif dist_f_u[cur] + 1 == dist_f_v[nxt]:
			dist_f_u[nxt] = dist_f_u[cur] + 1
print(m - 1)


from collections import deque
(N, u, v) = map(int, input().split())
X = [list(map(int, input().split())) for _ in range(N - 1)]
graph = [[] for _ in range(N + 1)]
for (a, b) in X:
	graph[a].append(b)
	graph[b].append(a)

def bfs(s):
	INF = 10 ** 9 + 7
	d = [INF] * (N + 1)
	d[s] = 0
	q = deque([s])
	while q:
		u = q.popleft()
		for v in graph[u]:
			if d[v] > d[u] + 1:
				d[v] = d[u] + 1
				q.append(v)
	return d
to = bfs(u)
ot = bfs(v)
ans = 0
for (i, (a, b)) in enumerate(zip(to, ot)):
	if a < b:
		ans = max(ans, b)
print(ans - 1)


(n, u, v) = map(int, input().split())
u -= 1
v -= 1
ki = [[] for _ in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	ki[a - 1].append(b - 1)
	ki[b - 1].append(a - 1)
ha = []
for i in range(n):
	if len(ki[i]) == 1:
		ha.append(i)
ta = [0] * n
ao = [0] * n
from collections import deque
d = deque()
d.append(u)
visited = [False] * n
visited[u] = True
while d:
	g = d.popleft()
	for i in ki[g]:
		if visited[i]:
			continue
		d.append(i)
		visited[i] = True
		ta[i] = ta[g] + 1
d = deque()
d.append(v)
visited = [False] * n
visited[v] = True
while d:
	g = d.popleft()
	for i in ki[g]:
		if visited[i]:
			continue
		d.append(i)
		visited[i] = True
		ao[i] = ao[g] + 1
if u in ha and ki[u][0] == v:
	print(0)
	return
m = 0
for i in ha:
	if ta[i] < ao[i]:
		m = max(m, ao[i])
print(m - 1)


import collections
(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)
q = collections.deque()
q.append((v, 0))
checked = [0] * (n + 1)
dist1 = [0] * (n + 1)
while len(q) != 0:
	(tv, td) = q.popleft()
	checked[tv] = 1
	dist1[tv] = td
	for tu in g[tv]:
		if checked[tu] == 1:
			continue
		q.append((tu, td + 1))
q = collections.deque()
q.append((u, 0))
checked = [0] * (n + 1)
dist2 = [0] * (n + 1)
while len(q) != 0:
	(tv, td) = q.popleft()
	checked[tv] = 1
	dist2[tv] = td
	for tu in g[tv]:
		if checked[tu] == 1:
			continue
		q.append((tu, td + 1))
ans = 0
for tv in range(1, n + 1):
	if dist1[tv] > dist2[tv]:
		ans = max(ans, dist1[tv] - 1)
print(ans)


from collections import defaultdict
import sys
sys.setrecursionlimit(10 ** 6)

def depth(adj, node, parent=None):
	ret = 0
	for child in adj[node]:
		if child == parent:
			continue
		d = depth(adj, child, node) + 1
		ret = max(ret, d)
	return ret

def main():
	(N, U, V) = list(map(int, input().split(' ')))
	U -= 1
	V -= 1
	adj = defaultdict(list)
	for _ in range(N - 1):
		(A, B) = list(map(lambda x: int(x) - 1, input().split(' ')))
		adj[A].append(B)
		adj[B].append(A)
	path = [-1] * N

	def find_path_on_tree(to_node, node, parent=None, d=0):
		if node == to_node:
			path[d] = node
			return True
		for child in adj[node]:
			if child == parent:
				continue
			if not find_path_on_tree(to_node, child, node, d + 1):
				continue
			path[d] = node
			return True
		return False
	find_path_on_tree(U, V)
	path = [node for node in path if node != -1]
	dist = len(path) - 1
	partial_depth = depth(adj, path[dist // 2 + 1], path[dist // 2])
	print(partial_depth + dist // 2)

def __starting_point():
	main()
__starting_point()


import sys
sys.setrecursionlimit(10 ** 6)
(n, T, A) = map(int, input().split())
T -= 1
A -= 1
g = [list() for i in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	g[a].append(b)
	g[b].append(a)
dist_r = [0] * n
dist_t = [0] * n

def dfs(v, p, dist):
	dist_r[v] = dist
	ret = 0
	for nv in g[v]:
		if nv == p:
			continue
		ret = max(ret, dfs(nv, v, dist + 1))
	dist_t[v] = ret
	return ret + 1
dfs(A, -1, 0)
init_dist = dist_r[T]
v = T
d1 = 0
d2 = init_dist
while init_dist - dist_r[v] < dist_r[v]:
	if d1 < dist_t[v]:
		d1 = dist_t[v]
		d2 = dist_r[v]
	for nv in g[v]:
		if dist_r[v] > dist_r[nv]:
			v = nv
			break
print(d1 + d2 - 1)


(N, u, v) = map(int, input().split())
AB = [list(map(int, input().split())) for _ in range(N - 1)]
graph = [[] for _ in range(N)]
for (i, (Ai, Bi)) in enumerate(AB):
	graph[Ai - 1].append(Bi - 1)
	graph[Bi - 1].append(Ai - 1)
import heapq
MY_INF = 10 ** 18
q = [(0, u - 1)]
d_u = [MY_INF] * N
d_u[u - 1] = 0
while len(q) > 0:
	(dist, src) = heapq.heappop(q)
	for dst in graph[src]:
		dist_tmp = dist + 1
		if d_u[dst] > dist_tmp:
			d_u[dst] = dist_tmp
			heapq.heappush(q, (dist_tmp, dst))
q = [(0, v - 1)]
d_v = [MY_INF] * N
d_v[v - 1] = 0
while len(q) > 0:
	(dist, src) = heapq.heappop(q)
	for dst in graph[src]:
		dist_tmp = dist + 1
		if d_v[dst] > dist_tmp:
			d_v[dst] = dist_tmp
			heapq.heappush(q, (dist_tmp, dst))
ans = 0
for i in range(N):
	if d_u[i] < d_v[i] and ans < d_v[i] - 1:
		ans = d_v[i] - 1
print(ans)


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

def dfs(v):
	dist = [-1] * (n + 1)
	stack = [v]
	dist[v] = 0
	while stack:
		v = stack.pop()
		dw = dist[v] + 1
		for w in graph[v]:
			if dist[w] >= 0:
				continue
			dist[w] = dw
			stack.append(w)
	return dist
takahashi = dfs(u)
aoki = dfs(v)
ans = 0
for i in range(1, n + 1):
	if takahashi[i] < aoki[i]:
		ans = max(ans, aoki[i] - 1)
print(ans)


from collections import deque
(n, u, v) = map(int, input().split())
Edge = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	Edge[a - 1].append(b - 1)
	Edge[b - 1].append(a - 1)

def bfs(s):
	dist = [-1] * n
	que = deque([s - 1])
	dist[s - 1] = 0
	while que:
		v = que.popleft()
		d = dist[v]
		for w in Edge[v]:
			if dist[w] > -1:
				continue
			dist[w] = d + 1
			que.append(w)
	return dist
(U, V) = (bfs(u), bfs(v))
ans = 0
for i in range(n):
	if U[i] < V[i]:
		ans = max(ans, V[i] - 1)
print(ans)


from collections import deque
import sys
sys.setrecursionlimit(10 ** 7)
(N, u, v) = map(int, input().split())
D = [[] for i in range(N)]
for i in range(N - 1):
	(a, b) = map(int, input().split())
	a -= 1
	b -= 1
	D[a].append(b)
	D[b].append(a)
Taka = [0] * (N + 1)
Aoki = [0] * (N + 1)

def f(n, p, A):
	for i in D[n]:
		if i != p:
			A[i] = A[n] + 1
			f(i, n, A)
f(u - 1, -1, Taka)
f(v - 1, -1, Aoki)
ans = 0
for i in range(N):
	if Taka[i] < Aoki[i]:
		ans = max(ans, Aoki[i] - 1)
print(ans)


from collections import defaultdict as d
from collections import deque

def bfs(adj, start, n):
	visited = [0] * (n + 1)
	tab = [0] * n
	q = deque([start])
	visited[start] = 1
	while q:
		s = q.popleft()
		for i in adj[s]:
			if visited[i] == 0:
				q.append(i)
				visited[i] = 1
				tab[i - 1] = tab[s - 1] + 1
	return tab
(n, u, v) = map(int, input().split())
adj = d(list)
for i in range(n - 1):
	(a, b) = map(int, input().split())
	adj[a].append(b)
	adj[b].append(a)
takahashi = bfs(adj, u, n)
aoki = bfs(adj, v, n)
res = -1
for i in range(n):
	if takahashi[i] <= aoki[i]:
		res = max(res, aoki[i] - 1)
print(res)


from collections import deque
(N, u, v) = map(int, input().split())
root = [[] for i in range(N)]
for _ in range(N - 1):
	(a, b) = (int(x) for x in input().split())
	root[b - 1].append(a - 1)
	root[a - 1].append(b - 1)

def search(p):
	stack = deque([p])
	check = [-1] * N
	check[p] = 0
	while len(stack) > 0:
		v = stack.popleft()
		for i in root[v]:
			if check[i] == -1:
				check[i] = check[v] + 1
				stack.append(i)
	return check
taka = search(u - 1)
aoki = search(v - 1)
ans = 0
for i in range(N):
	if taka[i] < aoki[i]:
		ans = max(ans, aoki[i] - 1)
print(ans)


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

def bfs(x):
	X = [-1] * (n + 1)
	q = collections.deque([(x, 0)])
	X[x] = 0
	while q:
		(node, dist) = q.popleft()
		for dst in V[node]:
			if X[dst] == -1:
				X[dst] = dist + 1
				q.append((dst, dist + 1))
	return X
T = bfs(u)
A = bfs(v)
ans = 0
for i in range(1, len(T)):
	if T[i] < A[i]:
		ans = max(ans, A[i] - 1)
print(ans)


from collections import deque

def bfs(s):
	stack = deque([s])
	check = [-1] * n
	check[s] = 0
	while len(stack) > 0:
		v = stack.popleft()
		for i in root[v]:
			if check[i] == -1:
				check[i] = check[v] + 1
				stack.append(i)
	return check
(n, u, v) = map(int, input().split())
root = [[] for i in range(n)]
for _ in range(n - 1):
	(a, b) = (int(x) for x in input().split())
	root[b - 1].append(a - 1)
	root[a - 1].append(b - 1)
takahashi_root = bfs(u - 1)
aoki_root = bfs(v - 1)
ans = 0
for i in range(n):
	if takahashi_root[i] < aoki_root[i]:
		ans = max(ans, aoki_root[i] - 1)
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
(n, v, u) = map(int, input().split())
u -= 1
v -= 1
g = [list() for _ in range(n)]
for _ in range(n - 1):
	(a, b) = map(int, input().split())
	g[a - 1].append(b - 1)
	g[b - 1].append(a - 1)
dep = [0] * n
mdep = [0] * n
par = [-1] * n
if u == v:
	print(0)
	exit(0)

def dfs(v, p):
	for x in g[v]:
		if x == p:
			continue
		par[x] = v
		dep[x] = dep[v] + 1
		dfs(x, v)
		if mdep[v] < mdep[x] + 1:
			mdep[v] = mdep[x] + 1
	return
dfs(u, -1)
t = (dep[v] - 1) // 2
curv = v
for _ in range(t):
	curv = par[curv]
curu = par[curv]
if not dep[v] % 2:
	curu = par[curu]
print(t + mdep[curv] + 1 - dep[v] % 2)


def bfs(p):
	stack = [p]
	check = [-1] * n
	check[p] = 0
	while len(stack):
		v = stack.pop()
		for i in root[v]:
			if check[i] == -1:
				check[i] = check[v] + 1
				stack.append(i)
	return check
(n, u, v) = map(int, input().split())
u -= 1
v -= 1
root = [[] for i in range(n)]
for _ in range(n - 1):
	(a, b) = (int(x) for x in input().split())
	root[b - 1].append(a - 1)
	root[a - 1].append(b - 1)
move_count_u = bfs(u)
move_count_v = bfs(v)
ans = 0
for i in range(n):
	if move_count_u[i] < move_count_v[i]:
		ans = max(move_count_v[i] - 1, ans)
print(ans)


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)
if len(G[u]) == 1 and G[u][0] == v:
	print(0)
	exit()
distu = [-1 for i in range(N)]
q = deque([])
q.append(u)
while len(q) > 0:
	r = q.pop()
	for p in G[r]:
		if distu[p] != -1:
			continue
		distu[p] = distu[r] + 1
		q.append(p)
distv = [-1 for i in range(N)]
q = deque([])
q.append(v)
while len(q) > 0:
	r = q.pop()
	for p in G[r]:
		if distv[p] != -1:
			continue
		distv[p] = distv[r] + 1
		q.append(p)
dist = [distv[i] if distu[i] <= distv[i] else 0 for i in range(N)]
print(max(dist))


(N, U, V) = map(int, input().split())
(U, V) = (U - 1, V - 1)
adj = [list() for _ in range(N)]
for _ in range(N - 1):
	(a, b) = map(int, input().split())
	adj[a - 1].append(b - 1)
	adj[b - 1].append(a - 1)
dist_u = [None] * N
dist_u[U] = 0
stack = [U]
while stack:
	x = stack.pop()
	d = dist_u[x] + 1
	for y in adj[x]:
		if dist_u[y] is None:
			dist_u[y] = d
			stack.append(y)
dist_v = [None] * N
dist_v[V] = 0
stack = [V]
while stack:
	x = stack.pop()
	d = dist_v[x] + 1
	for y in adj[x]:
		if dist_v[y] is None:
			dist_v[y] = d
			stack.append(y)
n = max((b for (a, b) in zip(dist_u, dist_v) if a < b))
m = min((b for (a, b) in zip(dist_u, dist_v) if a <= b), default=n)
print(max(n, m) - 1)


from collections import defaultdict, deque
(N, u, v) = map(int, input().split())
u -= 1
v -= 1
dic = defaultdict(list)
for i in range(N - 1):
	(a, b) = map(int, input().split())
	dic[a - 1] += [b - 1]
	dic[b - 1] += [a - 1]
dist1 = [float('inf')] * N
dist2 = [float('inf')] * N
q1 = deque([u])
q2 = deque([v])
dist1[u] = 0
dist2[v] = 0
while q1:
	e = q1.popleft()
	for p in dic[e]:
		if dist1[p] > dist1[e] + 1:
			dist1[p] = dist1[e] + 1
			q1 += [p]
while q2:
	e = q2.popleft()
	for p in dic[e]:
		if dist2[p] > dist2[e] + 1:
			dist2[p] = dist2[e] + 1
			q2 += [p]
ans = -1
j = 0
for i in range(N):
	if ans < dist2[i] - 1 and dist1[i] < dist2[i]:
		ans = dist2[i] - 1
if u == v:
	ans = 0
print(ans)


import sys
(n, u, v) = map(int, input().split())
sys.setrecursionlimit(10 ** 9)
tree = [[] for i in range(n)]
for i in range(n - 1):
	(a, b) = map(int, input().split())
	tree[a - 1].append(b - 1)
	tree[b - 1].append(a - 1)
a = [None] * n
t = [None] * n
t[u - 1] = 0
a[v - 1] = 0

def dfsA(x, c):
	for i in tree[x]:
		if a[i] == None:
			a[i] = c
			dfsA(i, c + 1)
dfsA(v - 1, 1)
k = a[u - 1]

def dfsT(x, c):
	global k
	for i in tree[x]:
		if t[i] == None and c < a[i]:
			t[i] = c
			if k < a[i]:
				k = a[i]
			dfsT(i, c + 1)
dfsT(u - 1, 1)
print(k - 1)


(N, u, v) = list(map(int, input().split()))
g = [[] for _ in range(N + 1)]

def dist(x):
	dist = [-1] * (N + 1)
	dist[x] = 0
	stack = [x]
	while len(stack) > 0:
		current = stack.pop()
		for s in g[current]:
			if dist[s] > -1:
				continue
			dist[s] = dist[current] + 1
			stack.append(s)
	return dist
for _ in range(N - 1):
	(a, b) = list(map(int, input().split()))
	g[a].append(b)
	g[b].append(a)
du = dist(u)
dv = dist(v)
ans = 0
for i in range(1, N + 1):
	if du[i] <= dv[i]:
		ans = max(ans, dv[i] - 1)
print(ans)