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# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections.Generic;
using System.Linq;

namespace ABC148F
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Nuv = Cin.IArr();
            int[] A = new int[Nuv[0]];
            int[] B = new int[Nuv[0]];
            for (int i = 0; i < Nuv[0] - 1; i++)
            {
                var AB = Cin.IArr();
                A[i] = AB[0];
                B[i] = AB[1];
            }

            var solver = new Solver(Nuv[0], Nuv[1], Nuv[2], A, B);

            Console.WriteLine(solver.Solve());
        }
    }

    class Solver
    {
        // 頂点数
        private int N;

        // 高橋初期位置
        private int U;

        // 青木初期位置
        private int V;

        // 木 <頂点、 隣接する頂点>
        private Dictionary<int, HashSet<int>> Tree;

        public Solver(int n, int u, int v, int[] a, int[] b)
        {
            N = n;
            U = u;
            V = v;
            Tree = new Dictionary<int, HashSet<int>>();
            for (int i = 0; i < N - 1; i++)
            {
                if (Tree.ContainsKey(a[i]))
                {
                    Tree[a[i]].Add(b[i]);
                }
                else
                {
                    Tree.Add(a[i], new HashSet<int> {b[i]});
                }

                if (Tree.ContainsKey(b[i]))
                {
                    Tree[b[i]].Add(a[i]);
                }
                else
                {
                    Tree.Add(b[i], new HashSet<int> {a[i]});
                }
            }
        }

        public IConvertible Solve()
        {
            // 高橋の初期値からの距離のマップ<頂点, 距離>
            Dictionary<int, int> udis = new Dictionary<int, int>();
            // 青木からの初期値からの距離のマップ<頂点, 距離>
            Dictionary<int, int> vdis = new Dictionary<int, int>();

            Queue<Q> queue = new Queue<Q>();
            queue.Enqueue(new Q(U, 0));
            HashSet<int> tmemo = new HashSet<int> {U};
            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                udis.Add(current.Node, current.Distance);

                foreach (int node in Tree[current.Node])
                {
                    if (tmemo.Contains(node))
                    {
                        continue;
                    }

                    tmemo.Add(node);
                    queue.Enqueue(new Q(node, current.Distance + 1));
                }
            }

            queue.Enqueue(new Q(V, 0));
            HashSet<int> umemo = new HashSet<int> {V};
            while (queue.Count > 0)
            {
                var current = queue.Dequeue();
                vdis.Add(current.Node, current.Distance);

                foreach (int node in Tree[current.Node])
                {
                    if (umemo.Contains(node))
                    {
                        continue;
                    }

                    umemo.Add(node);
                    queue.Enqueue(new Q(node, current.Distance + 1));
                }
            }

            for (int i = 1; i <= N; i++)
            {
                // 高橋は青木と同じ距離の点(を経由する点)に行くことはできない
                if (vdis[i] <= udis[i])
                {
                    udis[i] = -1;
                }
            }

            int goal = U;
            for (int i = 1; i <= N; i++)
            {
                if (udis[goal] < udis[i])
                {
                    goal = i;
                }
            }

            var dis = vdis[goal] - udis[goal];
            return udis[goal] + dis - 1;
        }
    }

    class Q
    {
        public int Node;
        public int Distance;

        public Q(int node, int distance)
        {
            Node = node;
            Distance = distance;
        }
    }

    internal static class Cin
    {
        public static string String()
        {
            return Console.ReadLine();
        }

        public static int Int()
        {
            return int.Parse(String());
        }

        public static int[] IArr()
        {
            return Split().Select(int.Parse).ToArray();
        }

        public static long Long()
        {
            return long.Parse(String());
        }

        public static long[] LArr()
        {
            return Split().Select(long.Parse).ToArray();
        }

        public static double Double()
        {
            return double.Parse(String());
        }

        public static double[] DArr()
        {
            return Split().Select(double.Parse).ToArray();
        }

        private static IEnumerable<String> Split()
        {
            return String().Split(' ');
        }
    }
}