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: Accepted)

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

// using System.Numerics;
using System.Runtime.CompilerServices;
using ll=System.Int64;

public class Contest
{
	static void Swap1<T>(ref T a, ref T b) { T t = a; a = b; b = t; }
	static void Swap2<T>(ref T a, ref T b) where T : IComparable { if (a.CompareTo(b)==1) Swap1(ref a, ref b); }  // b の方が小さければ交換
	static bool Chmin<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)== 1) { a = b; return true; } return false; }  // b の方が小さければ a を更新
	static bool Chmax<T>(ref T a, T b) where T : IComparable { if (a.CompareTo(b)==-1) { a = b; return true; } return false; }  // b の方が大きければ a を更新
	static T[][] InitMat<T>(long len1, long len2, T val) where T : struct { T[][] m = new T[len1][]; for (int i = 0; i < m.Length; i++) m[i] = Enumerable.Repeat(val, (int)len2).ToArray(); return m; }
	static T[][][] InitMat<T>(long len1, long len2, long len3, T val) where T : struct { T[][][] m = new T[len1][][]; for (int i = 0; i < m.Length; i++) m[i] = InitMat(len2,len3,val); return m; }
	public const long MOD1 = 1000000007; // 10^9+7
	public const double EPS1 = 1e-8;

	public static void Main() {
		Solve_F();
 	}

	public static void Solve_A() {
		long  n, m;
		MyIO.ReadMulti(out n);
		MyIO.ReadMulti(out m);
		long ans = 0;

		if(m*n==2) ans = 3;
		if(m*n==3) ans = 2;
		if(m*n==6) ans = 1;




		MyIO.WriteMulti(ans);
	}
	public static void Solve_B() {
		long  n;
		MyIO.ReadMulti(out n);
		string s,t;
		MyIO.ReadMulti(out s,out t);

		StringBuilder ans = new StringBuilder();
		for (int i = 0; i < n; i++)
		{
			ans.Append(s[i].ToString()+t[i].ToString());
		}


		MyIO.WriteMulti(ans.ToString());
	}
	public static void Solve_C() {
		long  n, m;
		MyIO.ReadMulti(out n, out m);



		long ans = Lcm(n,m);


		MyIO.WriteMulti(ans);
	}
	public static void Solve_D() {
		long  n;
		MyIO.ReadMulti(out n);
		ll[] a;
		MyIO.ReadArray(out a);

		long ans = 0;

		ll idx = 1;
		for (int i = 0; i < n; i++)
		{
			if(a[i]==idx){
				idx++;
			}else{
				ans++;
			}
		}
		if(ans==n) ans = -1;


		MyIO.WriteMulti(ans);
	}
	public static void Solve_E() {
		long  n;
		MyIO.ReadMulti(out n);
		ll n5c = 0;
		ll v = n/2;
		while(0<v) {
			v /= 5;
			n5c += v;
		}


		long ans = (n%2==1)?0:n5c;

		MyIO.WriteMulti(ans);
	}
	public static void Solve_F() {
		long  n, u, v;
		MyIO.ReadMulti(out n, out u, out v);
		VT<ll,ll>[] abl;
		MyIO.ReadLines(n-1,out abl);
		List<ll>[] g = MakeGraph(abl, -1);



		if(g[u-1].Count==1&&g[u-1][0]==v-1) {
			MyIO.WriteMulti(0);
			return;
		}
		bool[] visit1 = new bool[n];
		bool[] visit2 = new bool[n];
		Dictionary<ll,ll> d1 = new Dictionary<ll, ll>();
		if (g[u-1].Count==1) d1[u-1] = 0;
		Dictionary<ll,ll> d2 = new Dictionary<ll, ll>();
		if (g[v-1].Count==1) d2[v-1] = 0;
		Func<ll,ll,ll> dfs1 = null;
		dfs1 = (i,d)=>{
			visit1[i] = true;
			ll cnt = 0;
			foreach (var e in g[i])
			{
				if(visit1[e]) continue;
				dfs1(e,d+1);
				cnt++;
			}
			if(cnt==0) d1[i] = d;
			return 0;
		};
		Func<ll,ll,ll> dfs2 = null;
		dfs2 = (i,d)=>{
			visit2[i] = true;
			ll cnt = 0;
			foreach (var e in g[i])
			{
				if(visit2[e]) continue;
				dfs2(e,d+1);
				cnt++;
			}
			if(cnt==0) d2[i] = d;
			return 0;
		};
		dfs1(u-1, 0);
		dfs2(v-1, 0);

		ll ans = 1;
		foreach (var kv in d2)
		{
			ll x = kv.Value-d1[kv.Key];
			if(0<x) Chmax(ref ans, kv.Value-1);
		}

		MyIO.WriteMulti(ans);
	}



	public static long Gcd(long a, long b) {
	    var v = new[] { a, b };
	    while (v[1] != 0) v = new[] { v[1], v[0] % v[1] };
	    return v[0];
	}

	public static long Lcm(long a, long b) => a / Gcd(a, b) * b;

    public static List<long>[] MakeGraph(VT<long,long>[] inEdges, long shift=0){
        long lim = -1;
		foreach (var e in inEdges)
        {
            if(lim<e.Item1) lim = e.Item1;
            if(lim<e.Item2) lim = e.Item2;
        }
        List<long>[] ret = new List<long>[Math.Max(lim+shift+1,0)];
        if (ret.Length==0) return ret;
        for (int i = 0; i < ret.Length; i++) ret[i] = new List<long>();
		foreach (var e in inEdges)
        {
            ret[e.Item1+shift].Add(e.Item2+shift);
            ret[e.Item2+shift].Add(e.Item1+shift);
        }
        return ret;
    }










}




















#region MyIO

public struct VT<T, U>
{
	public T Item1; public U Item2;
    public VT(T a, U b) { Item1 = a; Item2 = b; }
}
public struct VT<T, U, V>
{
	public T Item1; public U Item2; public V Item3;
    public VT(T a, U b, V c) { Item1 = a; Item2 = b; Item3 = c; }
}
public struct VT<T, U, V, W>
{
	public T Item1; public U Item2; public V Item3; public W Item4;
    public VT(T a, U b, V c, W d) { Item1 = a; Item2 = b; Item3 = c; Item4 = d; }
}

public class Printer : System.IO.StreamWriter
{
	public override IFormatProvider FormatProvider { get { return System.Globalization.CultureInfo.InvariantCulture; } }
	public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
	public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
}
public static class MyIO
{
	private static char[] splitter = new char[] {' '};
	private static bool eq<T, U>() => typeof(T).Equals(typeof(U));
	private static T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
	private static T cv<T>(string s) =>
		eq<T, int>()    ? ct<T, int>(int.Parse(s))
	  : eq<T, long>()   ? ct<T, long>(long.Parse(s))
	  : eq<T, double>() ? ct<T, double>(double.Parse(s, System.Globalization.CultureInfo.InvariantCulture))
	  : eq<T, char>()   ? ct<T, char>(s[0])
						: ct<T, string>(s);
			
	public static string[] ReadMulti<T>(out T a) {
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries); 
		a = cv<T>(ar[0]);
		return ar;
	}
	public static string[] ReadMulti<T, U>(out T a, out U b) {
		var ar = ReadMulti<T>(out a); 
        b = cv<U>(ar[1]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V>(out T a, out U b, out V c) {
		var ar = ReadMulti<T, U>(out a, out b); 
        c = cv<V>(ar[2]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W>(out T a, out U b, out V c, out W d) {
		var ar = ReadMulti<T, U, V>(out a, out b, out c); 
        d = cv<W>(ar[3]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
		var ar = ReadMulti<T, U, V, W>(out a, out b, out c, out d); 
        e = cv<X>(ar[4]);
		return ar;
	}
	public static string[] ReadMulti<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f) {
		var ar = ReadMulti<T, U, V, W, X>(out a, out b, out c, out d, out e); 
        f = cv<Y>(ar[5]);
		return ar;
	}
	public static string[] ReadArray<T>(out T[] a) {		
		var ar = Console.ReadLine().Split(splitter, StringSplitOptions.RemoveEmptyEntries);
		a = ar.Select(cv<T>).ToArray();
		return ar;
	}		
	public static string[][] ReadLines<T>(long n, out T[] a) {
		a = new T[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadMulti(out a[i]);
		return arr;
	}
	public static string[][] ReadLines<T, U>(long n, out VT<T, U>[] a) {
		a = new VT<T, U>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p;
			arr[i] = ReadMulti(out o, out p);
			a[i] = new VT<T, U>(o, p);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V>(long n, out VT<T, U, V>[] a) {
		a = new VT<T, U, V>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q;
			arr[i] = ReadMulti(out o, out p, out q);
			a[i] = new VT<T, U, V>(o, p, q);
		}
		return arr;
	}
	public static string[][] ReadLines<T, U, V, W>(long n, out VT<T, U, V, W>[] a) {
		a = new VT<T, U, V, W>[n];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) {
			T o; U p; V q; W r;
			arr[i] = ReadMulti(out o, out p, out q, out r);
			a[i] = new VT<T, U, V, W>(o, p, q, r);
		}
		return arr;
	}
	public static string[][] ReadMatrix<T>(long n, out T[][] a) {
		a = new T[n][];
		string[][] arr = new string[n][];
		for(int i=0; i<n; i++) arr[i] = ReadArray(out a[i]);
		return arr;
	}

	private static void wwp(Action act){
		var sw = new Printer(Console.OpenStandardOutput()){AutoFlush = false};
		Console.SetOut(sw);
		act();
		Console.Out.Flush();
		sw.AutoFlush = true;
		Console.SetOut(sw);
	}
	private static string wfm(Type t) =>t.Equals(typeof(double)) ? "{0:F10}" : "{0}";
	public static void WriteMulti(params object[] a) {
		wwp(()=>{
			for(int i=0;i<a.Length;i++) {
				string fs = wfm(a[i].GetType()) + ((i<a.Length-1) ? " " : Environment.NewLine);
				Console.Write(fs, a[i]);
			}
		});
	}
	public static void WriteArray<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T));
			string fs1 = fs + " ";
			string fs2 = fs + Environment.NewLine;
			for(int i=0;i<a.Length;i++) Console.Write( ((i<a.Length-1) ? fs1 : fs2), a[i]);
		});
	}
	public static void WriteLines<T>(T[] a) {
		wwp(()=>{
			string fs = wfm(typeof(T)) + Environment.NewLine;
			foreach(T x in a) Console.Write(fs, x);
		});
	}


}

#endregion