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: Accepted)

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <sstream>
#include <set>
#include <map>
#include <vector>
#include <list>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <bitset>     //UWAGA - w czasie kompilacji musi byc znany rozmiar wektora - nie mozna go zmienic
#include <cassert>
#include <iomanip>        //do setprecision
#include <ctime>
#include <complex>
using namespace std;

#define FOR(i,b,e) for(int i=(b);i<(e);++i)
#define FORQ(i,b,e) for(int i=(b);i<=(e);++i)
#define FORD(i,b,e) for(int i=(b)-1;i>=(e);--i)
#define REP(x, n) for(int x = 0; x < (n); ++x)

#define ST first
#define ND second
#define PB push_back
#define PF push_front
#define MP make_pair
#define LL long long
#define ULL unsigned LL
#define LD long double
#define pii pair<int,int>
#define pll pair<LL,LL>
#define vi vector<int>
#define vii vector<vi>

const double pi = 3.14159265358979323846264;
const int mod=1000000007;

int main(){
    int n,u,v,du;
    cin>>n>>u>>v;
    vi a(n),b(n),used(n+1,0),depth(n+1,mod),used2(n+1,mod),used3(n+1,mod);
    vii g(n+1,vi());
    FOR(i,1,n){
        cin>>a[i-1]>>b[i-1];
        g[a[i-1]].PB(b[i-1]);
        g[b[i-1]].PB(a[i-1]);
    }
    queue<int> q;
    q.push(u);
    used[u]=1;
    while(q.size()){
        auto nw=q.front();
        q.pop();
        for(auto x:g[nw]){
            if(used[x]||x==v)continue;
            q.push(x);
            used[x]=1;
        }
    }
    q.push(v);
    depth[v]=0;
    while(q.size()){
        auto nw=q.front();
        q.pop();
        for(auto x:g[nw]){
            if(used[x]==0||depth[x]!=mod)continue;
            q.push(x);
            depth[x]=depth[nw]+1;
            if(x==u)du=depth[x];
        }
    }
    int th=du/2,ans=du-th-1,st=-1;
    q.push(u);
    used2[u]=0;
    while(q.size()){
        auto nw=q.front();
        q.pop();
        for(auto x:g[nw]){
            if(used[x]==0||depth[x]<=th||used2[x]!=mod)continue;
            q.push(x);
            used2[x]=used2[nw]+1;
            if(depth[x]==th+1&&used2[x]+depth[x]==du)st=x;
        }
    }
    if(st==-1){
        cout<<th<<endl;
        return 0;
    }
    q.push(st);
    used3[st]=0;
    int mx=0;
    while(q.size()){
        auto nw=q.front();
        q.pop();
        for(auto x:g[nw]){
            if(used[x]==0||depth[x]<=th||used3[x]!=mod)continue;
            q.push(x);
            used3[x]=used3[nw]+1;
            mx=max(mx,used3[x]);
        }
    }
    ans=ans+mx;
    if(du%2==0)ans++;


    cout<<ans<<endl;




}