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 <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp> 
using namespace std;
//using namespace __gnu_pbds;
#define rep(i,a,b) for(i=(a);i<=(b);i++)
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int,int>
#define pb push_back
#define F first
#define S second
#define mod 1000000007
#define maxn 100005
#define inf 1e18
#define boost ios::sync_with_stdio(false);cin.tie(0)
#define fr freopen("source.txt","r",stdin),freopen("output.txt","w",stdout)
#define SET(a,b) memset(a,b,sizeof(a))
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define all(x) x.begin(), x.end()
//template<typename T>
//using ordered_set = tree<T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

vector<ll>adj[maxn];
ll a[maxn],lvl[maxn],p[maxn][20];
ll ans=0,x,y;
void dfs(ll u,ll par)
{
    p[u][0]=par;
    lvl[u]=lvl[par]+1;
    for(auto v : adj[u])
    {
        if(v==par) continue;
        dfs(v,u);
    }
}
ll lca(ll u,ll v)
{
    ll i,lg;
    if(lvl[u]<lvl[v])swap(u,v);
    for(lg=0;(1<<lg)<=lvl[u];lg++);
    lg--;
    for(i=lg;i>=0;i--)
    {
        if((lvl[u]-(1<<i))>=lvl[v])
        u=p[u][i];
    }
    if(u==v) return u;
    for(i=lg;i>=0;i--)
    {
        if(p[u][i]!=-1&&p[u][i]!=p[v][i]){
            u=p[u][i];v=p[v][i];
        }
    }
    return p[u][0];
}
ll getAncestor(ll u,ll dis)
{
    dis=lvl[u]-dis;
    ll i,lg;
    for(lg=0;(1<<lg)<=lvl[u];lg++);
    lg--;
    for(i=lg;i>=0;i--)
    {
        if(lvl[u]-(1<<i)>=dis)
        u=p[u][i];
    }
    return u;
}
ll dis(ll u,ll v)
{
    if(lvl[u]<lvl[v]) swap(u,v);
    ll w=lca(u,v);
    return lvl[u]+lvl[v]-2*lvl[w];
}
void DFS(ll v,ll par){
    vector<pll>t;
    for(auto p:adj[v]){
        if(p!=par){
            t.pb({lvl[p],p});
        }
    }
    sort(all(t),greater<pll>());
    if(t.size()==1){
        ans++;
        DFS(t[0].S,v);
    }
    else if(!t.empty()){
        if(dis(x,v)<dis(y,v)) {
            ans++;
            DFS(t[0].S,v);
        }
        else{
            for(auto p:t) {
                if(lca(x,p.S)==p.S) {
                    ans++;
                    DFS(p.S,v);
                    break;
                }
            }
        }
    }
}
int main()
{
    ll i,u,v,j,n;
    cin>>n>>x>>y;
    for(i=1;i<n;i++){
        cin>>u>>v;
        adj[u].pb(v);
        adj[v].pb(u);
    }
    for(i=0;i<20;i++)
    {
        for(j=1;j<=n;j++)
        {
            p[j][i]=-1;
        }
    }
    lvl[0]=-1;
    dfs(y,0);
    for(i=1;i<20;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(p[j][i-1]!=-1)
            p[j][i]=p[p[j][i-1]][i-1];
        }
    }
    DFS(y,0);
    cout<<max(ans-1,0LL);
}