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;
#define int long long
using P = pair<int, int>;
constexpr array<int, 4> dx = {1, 0, -1, 0};
constexpr array<int, 4> dy = {0, 1, 0, -1};
constexpr int INF = 1010101010;
constexpr int MOD = 1000000000 + 7;
// constexpr int MOD = 998244353;
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
constexpr int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
constexpr int fact(int n) { return n > 1 ? n * fact(n - 1) % MOD : 1; }
int modinv(int a)
{
    int b = MOD, u = 1, v = 0;
    while (b)
    {
        int t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= MOD;
    if (u < 0)
        u += MOD;
    return u;
}
constexpr int modpow(int a, int n)
{
    int rtn = 1;
    while (n > 0)
    {
        if (n & 1)
            rtn = rtn * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return rtn;
}

int tkhs, aoki;

// 升目マップを探索する
class Graph
{
private:
    int _n;
    // エッジ（ノードとノードをつなぐ辺）
    struct Edge
    {
        int to;       // 終点のノード番号
        int cost = 1; // エッジのコスト
    };
    vector<vector<Edge>> _edges; // _edges[i] ノードiから伸びるエッジの集合
    vector<int> _dist;           // ノードの距離を記録

public:
    // コンストラクタ ノード数nを渡す
    Graph(int n);
    ~Graph();
    void uniteNodes(int one, int another, int cost);
    // スタック使用深さ優先探索する 木ならtrue
    int depthFirstSearch(const int start);
    int maxDist(); // 木（閉路を持たない）の数を返す
};

Graph::Graph(int n) : _n(n)
{
    _edges.resize(_n);
    _dist.resize(_n, INF);
}

Graph::~Graph()
{
}

void Graph::uniteNodes(int one, int another, int cost = 1)
{
    _edges[one].push_back({another, cost});
    _edges[another].push_back({one, cost});
}

int Graph::depthFirstSearch(const int start)
{
    // 今後探索するノードを記録するスタック
    stack<int> destination;

    int dist_tkhs_aoki;

    // 最初にスタートを探索した
    destination.push(start);
    _dist[start] = 0;

    // 全てのノードを探索
    while (!destination.empty())
    {
        // スタックから調べる升を取り出す
        const int p = destination.top();
        destination.pop();

        for (auto &&edge : _edges[p])
        {
            // np: 今探索している隣接ノードのノード番号
            const int np = edge.to;
            // npをすでに探索していない
            if (_dist[np] == INF)
            {
                if (np == tkhs)
                {
                    stack<int> stemp;
                    destination = stemp;
                    dist_tkhs_aoki = _dist[p] + 1;
                }
                // npを今後調べるノードとしてスタックに積む
                destination.push(np);
                // npを探索したと記録する
                _dist[np] = _dist[p] + 1;
            }
        }
    }

    const int dist_max = maxDist();
    return dist_max - 1;
}

int Graph::maxDist()
{
    int max_dist = -1;
    for (int i = 0; i < _n; i++)
        if (_dist[i] != INF)
            chmax(max_dist, _dist[i]);

    return max_dist;
}

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(0);
    cout << setprecision(10);

    int n;
    cin >> n;
    cin >> tkhs >> aoki;
    tkhs--;
    aoki--;
    Graph g(n);
    for (int i = 0; i < n; i++)
    {
        int u, v;
        cin >> u >> v;
        g.uniteNodes(u - 1, v - 1);
    }

    cout << g.depthFirstSearch(aoki) << endl;
}