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 <bits/stdc++.h>

using namespace std;

typedef long long int ll;
typedef pair<ll, ll> pll;

#define FOR(i, n, m) for(ll (i)=(m);(i)<(n);++(i))
#define REP(i, n) FOR(i,n,0)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll) 1e15;

struct RootedTree {
    RootedTree(int n, int root = 0) :
            mRoot(root) {
        mVertexTbl.resize(n);
    }

    void addEdge(int a, int b, long long int cost = 1) {
        mVertexTbl[a].child.push_back(pair<int, long long int>(b, cost));
        mVertexTbl[b].child.push_back(pair<int, long long int>(a, cost));
    }

    void build() {
        queue<tuple<int, int, long long int>> q;
        q.push(make_tuple(mRoot, -1, 0));
        while (!q.empty()) {
            auto t = q.front();
            q.pop();
            int index = get<0>(t);
            int parent = get<1>(t);
            long long int depth = get<2>(t);
            mVertexTbl[index].parent = parent;
            mVertexTbl[index].depth = depth;
            for (auto it = mVertexTbl[index].child.begin(); it != mVertexTbl[index].child.end();) {
                int nxt = (*it).first;
                if (nxt == parent) {
                    mVertexTbl[index].child.erase(it);
                    continue;
                }
                q.push(make_tuple(nxt, index, depth + (*it).second));
                ++it;
            }
        }
    }

    struct Vertex {
        vector<pair<int, long long int>> child;
        long long int depth = 0;
        int parent = -1;
    };

    int mRoot;
    vector<Vertex> mVertexTbl;
};

ll CA[100005];
ll CT[100005];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll N, T, A;
    cin >> N >> T >> A;
    T--;
    A--;
    RootedTree tree(N, A);
    REP(i, N - 1) {
        ll a, b;
        cin >> a >> b;
        a--;
        b--;
        tree.addEdge(a, b);
    }
    tree.build();
    REP(i, N) {
        CA[i] = CT[i] = INF;
    }

    {
        queue<pll> q;
        q.push(pll(A, 0));
        CA[A] = 0;
        while (!q.empty()) {
            pll t = q.front();
            q.pop();
            REP(i, tree.mVertexTbl[t.first].child.size()) {
                ll n = tree.mVertexTbl[t.first].child[i].first;
                if (CA[n] <= t.second + 1)
                    continue;
                CA[n] = t.second + 1;
                q.push(pll(n, CA[n]));
            }
        }
    }
    {
        queue<pll> q;
        q.push(pll(T, 0));
        CT[T] = 0;
        while (!q.empty()) {
            pll t = q.front();
            q.pop();
            ll cost = t.second + 1;
            ll n = tree.mVertexTbl[t.first].parent;
            if (CT[n] <= cost)
                continue;
            if (CA[n] <= cost)
                continue;
            CT[n] = cost;
            q.push(pll(n, cost));
        }
    }

    ll ta = T;
    REP(i, N) {
        if (CT[i] == INF)
            continue;
        if (CT[ta] < CT[i])
            ta = i;
    }
    ll ans = CT[ta];
    {
        queue<pll> q;
        q.push(pll(ta, CT[ta]));
        while (!q.empty()) {
            pll t = q.front();
            q.pop();
            ll cost = t.second + 1;
            ans = std::max(t.second, ans);
            REP(i, tree.mVertexTbl[t.first].child.size()) {
                ll n = tree.mVertexTbl[t.first].child[i].first;
                if (CA[n] >= cost)
                    ans = std::max(ans, std::min(CA[n], cost));
                if (CA[n] > cost)
                    q.push(pll(n, cost));
            }
            ll p = tree.mVertexTbl[t.first].parent;
            if (CA[p] >= cost)
                ans = std::max(ans, std::min(CA[p], cost));
            if (CA[p] > cost)
                q.push(pll(p, cost));
        }
    }
    cout << ans << endl;
    return 0;
}