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>
using namespace std;
typedef long long ll;
//typedef pair<ll, ll> P;
typedef pair<int, int> i_i;
ll INF = 1e12+10;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
vector<pair<ll, ll> > pairs;
#define print1(x) cerr << "x = " << x << endl;
#define print2(x, y) cerr<< "x,y = "  << x << "," << y << endl;
#define print3(x, y, z) cerr << "x,y,z = " << x << "," << y << "," << z << endl;
#define print4(x, y, z, w) cerr << "x,y,z,w = " << x << "," << y << "," << z << "," << w << endl;
const ll MAX = ll(1e10) * 300 + 10;
#define MAX_V int(2e5 + 1)
#define INF (1000000000LL) // * 100000000LL)

struct edge{int to, cost;};
typedef pair<int, int> P;

vector<edge> G[MAX_V];
int d[MAX_V], depth[MAX_V];
vector<bool> seen;
int S, T, V;

void dijkstra(int s){
    priority_queue<P, vector<P>, greater<P> > que;
    fill(d, d + V, INF);
    d[s] = 0;
    que.push(P(0, s));
    
    while (!que.empty()){
        P p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < G[v].size(); i++){
            edge e = G[v][i];
            if (d[e.to] > d[v] + e.cost){
                d[e.to] = d[v] + e.cost;
                que.push(P(d[e.to], e.to));
            }
        }
    }
    //fprintf(stderr, "%d, %d, %d\n", V, T, S);
    //for (int i = 0; i < V; i++){
    //    fprintf(stderr, "%d, %d: %d\n", s, i, d[i]);
    //}
    //exit(1);
    return;
}

int dfs(int v) {
    // �s�����������C���N�������g���Ȃ��烁��
    int dep = 0;
    seen[v] = true; // v ��K��ςɂ���
    for (int i = 0; i < G[v].size(); i++){
        edge e = G[v][i];
        if (seen[e.to]) continue; 
        int tmp = dfs(e.to);
        dep = max(tmp, dep);
    }

    // �A�肪�������C���N�������g���Ȃ��烁��
    depth[v] = dep + 1;
    return depth[v];
}

////////////////////////////////////////

int main() {
    int ans = 0;
    cin >> V >> T >> S;
    S--;
    T--;
    vector<int> A(V), B(V), C(V);
    for (int i = 0; i < V - 1; i++){
        cin >> A[i] >> B[i];
        C[i] = 1;
        G[A[i] - 1].push_back((edge){B[i] - 1, C[i]});
        G[B[i] - 1].push_back((edge){A[i] - 1, C[i]});
    }
    
    
    
    dijkstra(S);
    seen.assign(V, false); // �S���_���u���K��v�ɏ�����
    dfs(S);
    int a = T, b = S;
    while (1){
        if (a == b){
            break;
        }
        int next = -1;
        
        //fprintf(stderr, "aaa a, b = (%d, %d), %d, %d \n", a + 1, b + 1, d[a], d[b]);
        
        if (d[a] >= d[b] + 2){
            for (int i = 0; i < G[a].size(); i++){
                edge e = G[a][i];
                if (d[e.to] < d[a]){
                    next = e.to;
                    break;
                }
            }
        } else {
            int cost = -1;
            for (int i = 0; i < G[a].size(); i++){
                edge e = G[a][i];
                if (d[e.to] > d[a] && depth[e.to] > cost){
                    cost = depth[e.to];
                    next = e.to;
                    //fprintf(stderr, "###  %d -> %d\n", a + 1, e.to + 1);
                }
            }
            if (next == -1){
            for (int i = 0; i < G[a].size(); i++){
                edge e = G[a][i];
                if (d[e.to] < d[a]){
                    next = e.to;
                    break;
                }
            }
            }                
        }
        a = next;
        //fprintf(stderr, "a = %d\n", a + 1);
        if (a == b){
            break;
        }
        if (1){
        
            int cost = -1;
            for (int i = 0; i < G[b].size(); i++){
                edge e = G[b][i];
                if (d[e.to] > d[b] && depth[e.to] > cost){
                    cost = depth[e.to];
                    next = e.to;
                }
            }
        }
        ans++;
        
        b = next;
        //fprintf(stderr, "b = %d\n", b + 1);
    }
        
    cout << ans << endl;
    return 0;
}