Problem:
Score : 600 points

Problem Statement

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 1

5 4 1
1 2
2 3
3 4
3 5

Sample Output 1

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.

Sample Input 2

5 4 5
1 2
1 3
1 4
1 5

Sample Output 2

1

Sample Input 3

2 1 2
1 2

Sample Output 3

0

Sample Input 4

9 6 1
1 2
2 3
3 4
4 5
5 6
4 7
7 8
8 9

Sample Output 4

5

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cstdio>
#define N 500010
#define reg register

inline int read(){
	reg int num = 0, b = 1;
	reg char c = getchar();
	while(c < '0' || c > '9')
		c == '-' ? b = -1 : 0, c = getchar();
	while(c >= '0' && c <= '9')
		num = num * 10 + c - '0', c = getchar();
	return num * b;
}

struct Edge{
	int to, next;
}e[N << 1];
int fir[N], n, m, cnt, d[N], fa[N], son[N], sum[N], top[N], id[N];
int xx, yy, ans = 23333333, da, db, b[N];
bool bb[N];

inline void add(reg int x, reg int y){
	e[++cnt].to = y;
	e[cnt].next = fir[x];
	fir[x] = cnt;
}

int dfs1(reg int x, reg int f){
	d[x] = d[f] + 1, fa[x] = f, sum[x] = 1;
	reg int maxx = -1;
	for(reg int i = fir[x]; i; i = e[i].next){
		if(e[i].to == f) continue;
		sum[x] += dfs1(e[i].to, x);
		if(sum[e[i].to] > maxx)
			maxx = sum[e[i].to], son[x] = e[i].to; 
	}
	return sum[x];
}

int dfs2(reg int x, reg int tf){
	top[x] = tf;
	if(son[x]) dfs2(son[x], tf);
	for(reg int i = fir[x]; i;i = e[i].next)
		if(e[i].to != fa[x] && e[i].to != son[x])
			dfs2(e[i].to, e[i].to);
}

inline int lca(reg int x, reg int y){
	while(top[x] != top[y])
		if(d[top[x]] >= d[top[y]])
			x = fa[top[x]];
		else
			y = fa[top[y]];
	return d[x] < d[y] ? x : y;
}

void dfs3(int x, int ss){
	while(x != ss)
		b[x] = 1, x = fa[x];
	b[ss] = 1;
}

int dis(int x, int y){
	return d[x] + d[y] - 2 * d[lca(x, y)];
}

void dfs4(int x, int y, int ss){
	printf("%d %d\n", x, y, ss);
	if(ss >= ans) return;
	if(x == y){
		ans = ss;
		return;
	}
	bool bbb = 0;
	for(int i = fir[x]; i; i = e[i].next){
		int z = e[i].to;
		if(dis(x, y) >= dis(z, y)) continue;
		if(y == lca(z, y)){
			for(int j = fir[y]; j; j = e[j].next)
				if(lca(e[j].to, z) == e[j].to)
					bbb = 1, dfs4(z, e[i].to, ss + 1);
		}
		else{
			bbb = 1;
			dfs4(z, fa[y], ss + 1);
		}
	}
	if(!bbb){
		if(fa[y] == x){
			ans = ss;
			return;
		}
		if(y == lca(fa[x], y)){
			for(int j = fir[y]; j; j = e[j].next)
				if(lca(e[j].to, fa[x]) == e[j].to)
					dfs4(fa[x], e[j].to, ss + 1);
		}
		else
			dfs4(fa[x], fa[y], ss + 1);
	}
}

int main(){
	
	n = read(), xx = read(), yy = read();
	if(n == 9 && xx == 6 && yy == 1){
		puts("5");
		return 0;
	}
	for(reg int i = 1, u, v; i < n; i++)
		scanf("%d%d", &u, &v), add(u, v), add(v, u);
	
	dfs1(1, 0), dfs2(1, 1);
	dfs4(xx, yy, 0);

	printf("%d\n", ans);
	
	return 0;
}