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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Text;
using System.Collections.Generic;
using System.Collections;
using static System.Math;
using System.Linq;
using static System.Array;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            //var N = long.Parse(Console.ReadLine());
            Solve();

        }
        static void Solve()
        {
            var NUV = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
            var N = NUV[0];
            var U = NUV[1];
            var V = NUV[2];

            var Taka = new Dikstra(N+1);
            var Aoki = new Dikstra(N+1);

            for (int i = 1; i <= N -1; i++)
            {
                var AB = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
                var A = AB[0];
                var B = AB[1];

                Taka.Add(A, B);
                Taka.Add(B, A);
                Aoki.Add(A, B);
                Aoki.Add(B, A);
            }

            var TC = Taka.GetMinCost(U);
            var AC = Aoki.GetMinCost(V);

            long ans = -1;
            for (int i = 1; i <= N; i++)
            {
                if(TC[i] < AC[i])
                {
                    ans = Max(ans, AC[i] - 1);
                }
            }

            Console.WriteLine(ans);
        }
    }

    public class Dikstra
    {
        public int N { get; }               // 頂点の数
        private List<Edge>[] _graph;        // グラフの辺のデータ

        /// <summary>
        /// 初期化
        /// </summary>
        /// <param name="n">頂点数</param>
        public Dikstra(int n)
        {
            N = n;
            _graph = new List<Edge>[n];
            for (int i = 0; i < n; i++) _graph[i] = new List<Edge>();
        }

        /// <summary>
        /// 辺を追加
        /// </summary>
        /// <param name="a">接続元の頂点</param>
        /// <param name="b">接続先の頂点</param>
        /// <param name="cost">コスト</param>
        public void Add(int a, int b, long cost = 1)
                => _graph[a].Add(new Edge(b, cost));

        /// <summary>
        /// 最短経路のコストを取得
        /// </summary>
        /// <param name="start">開始頂点</param>
        public long[] GetMinCost(int start)
        {
            // コストをスタート頂点以外を無限大に
            var cost = new long[N];
            for (int i = 0; i < N; i++) cost[i] = 1000000000000000000;
            cost[start] = 0;

            // 未確定の頂点を格納する優先度付きキュー(コストが小さいほど優先度が高い)
            var q = new PriorityQueue<Vertex>(N * 10, Comparer<Vertex>.Create((a, b) => b.CompareTo(a)));
            q.Push(new Vertex(start, 0));

            while (q.Count > 0)
            {
                var v = q.Pop();

                // 記録されているコストと異なる(コストがより大きい)場合は無視
                if (v.cost != cost[v.index]) continue;

                // 今回確定した頂点からつながる頂点に対して更新を行う
                foreach (var e in _graph[v.index])
                {
                    if (cost[e.to] > v.cost + e.cost)
                    {
                        // 既に記録されているコストより小さければコストを更新
                        cost[e.to] = v.cost + e.cost;
                        q.Push(new Vertex(e.to, cost[e.to]));
                    }
                }
            }

            // 確定したコストを返す
            return cost;
        }

        public struct Edge
        {
            public int to;                      // 接続先の頂点
            public long cost;                   // 辺のコスト

            public Edge(int to, long cost)
            {
                this.to = to;
                this.cost = cost;
            }
        }

        public struct Vertex : IComparable<Vertex>
        {
            public int index;                   // 頂点の番号
            public long cost;                   // 記録したコスト

            public Vertex(int index, long cost)
            {
                this.index = index;
                this.cost = cost;
            }

            public int CompareTo(Vertex other)
                => cost.CompareTo(other.cost);
        }
    }
    class PriorityQueue<T> where T : IComparable<T>
    {
        private readonly T[] _array;
        private readonly IComparer _comparer;
        public int Count { get; private set; } = 0;
        public T Root => _array[0];

        public PriorityQueue(int capacity, IComparer comparer = null)
        {
            _array = new T[capacity];
            _comparer = comparer;
        }

        /// <summary>
        /// 要素を挿入する
        /// </summary>
        public void Push(T item)
        {
            _array[this.Count] = item;
            Count += 1;

            var n = Count - 1;                  // 末尾(追加した)のノードの番号
            while (n != 0)
            {
                var parent = (n - 1) / 2;       // 親ノードの番号

                if (Compare(_array[n], _array[parent]) > 0)
                {
                    Swap(n, parent);
                    n = parent;
                }
                else
                {
                    break;
                }
            }
        }

        /// <summary>
        /// 優先度の一番高いものを取り出す
        /// </summary>
        public T Pop()
        {
            Swap(0, this.Count - 1);            // 先頭要素を末尾にする
            Count -= 1;

            var parent = 0;                     // 親ノードの番号
            while (true)
            {
                var child = 2 * parent + 1;     // 子ノードの番号
                if (child > Count - 1) break;

                // 値の大きい方の子を選ぶ
                if (child < Count - 1 && Compare(_array[child], _array[child + 1]) < 0) child += 1;

                // 子の方が親より大きければ入れ替える
                if (Compare(_array[parent], _array[child]) < 0)
                {
                    Swap(parent, child);
                    parent = child;
                }
                else
                {
                    break;
                }
            }

            return _array[Count];
        }

        /// <summary>
        /// 大きいものから列挙していく
        /// withPop = falseのときは順番関係なく取り出しもしないが素早く全要素を取得できる　
        /// </summary>
        public IEnumerable<T> GetAllElements(bool withPop = true)
        {
            int count = Count;
            for (int i = 0; i < count; i++)
            {
                if (withPop) yield return Pop();
                else yield return _array[count - i - 1];
            }
        }

        private int Compare(T a, T b)
        {
            if (_comparer == null) return a.CompareTo(b);
            return _comparer.Compare(a, b);
        }

        private void Swap(int a, int b)
        {
            var tmp = _array[a];
            _array[a] = _array[b];
            _array[b] = tmp;
        }
    }
}