// #include <ext/pb_ds/assoc_container.hpp> 
                                             //#include <ext/pb_ds/tree_policy.hpp> 
                                                #include <iostream>
                                                #include <iomanip>
                                                #include <sstream>
                                                #include <cstring>
                                                #include <vector>
                                                #include <deque>
                                                #include <queue>
                                                #include <set>
                                                #include <map>
                                                #include <valarray>
                                                #include <iterator>
                                                #include <functional>
                                                #include <limits>
                                                #include <algorithm>
                                                #include <numeric>
                                                #include <cmath>
                                                #include <cassert>
                                                #include <unordered_map>
                                                #include <unordered_set>
                                                #include <stack>
                                                using namespace std;
                                                #define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
                                                 #define trace(x) cerr << #x << ": " << x << " " << endl;
                                             
                                                typedef int64_t ll;
                                             
                                                #define endl '\n'
                                                #define int ll
                                                ll mod=1e9+7;
                                                ll mod1=1e9+6;
                                                
                                                ll power(ll a,ll b)
                                                {
                                                    if(b==0) return 1;
                                                    else if(b%2==0)
                                                        return power((((a%mod)*(a%mod))%mod),b/2)%mod;
                                                    else return ((a%mod)*(power((((a%mod)*(a%mod))%mod),b/2)%mod))%mod;
                                                }
                                               
                                             //  using namespace __gnu_pbds;
                                           //  #define ordered_set tree<pair<int,int>, null_type,less<pair<int,int>>, rb_tree_tag,tree_order_statistics_node_update>
                                                //find_by_order(k)  returns iterator to kth element starting from 0;
                                                //order_of_key(k) returns count of elements strictly smaller than k;
                                                //erase,insert same as normal set
                                              
                                            int n,x,y;
                                            vector<int> v[200001];
                                            int dis[200001];
                                            int dis1[200001];
                                            void dfs(int u,int par)
                                            {
                                                dis[u]=dis[par]+1;
                                                for(int i=0;i<v[u].size();++i)
                                                {
                                                    if(v[u][i]!=par)
                                                    {
                                                        dfs(v[u][i],u);
                                                    }
                                                }
                                            }
                                            void dfs1(int u,int par)
                                            {
                                                dis1[u]=dis1[par]+1;
                                                for(int i=0;i<v[u].size();++i)
                                                {
                                                    if(v[u][i]!=par)
                                                    {
                                                        dfs1(v[u][i],u);
                                                    }
                                                }
                                            }
                                            int32_t main()
                                            {
                                                IOS
                                            
                                                
                                                    
                                                cin>>n>>x>>y;
                                                for(int i=1;i<=n-1;++i)
                                                {
                                                    int a,b;
                                                    cin>>a>>b;
                                                    v[a].push_back(b);
                                                    v[b].push_back(a);
                                                }
                                                memset(dis,0,sizeof(dis));
                                                dfs(y,0);
                                                int maxi=0;
                                                for(int i=1;i<=n;++i)
                                                {
                                                    maxi=max(maxi,dis[i]);
                                                }
                                                //cout<<maxi<<endl;
                                              
                                                
                                                memset(dis1,0,sizeof(dis1));
                                                dfs1(x,0);
                                                int mini=-1;
                                                for(int i=1;i<=n;++i)
                                                {
                                                    if(int(v[i].size()!=1))
                                                        continue;
                                                    if(i==y)
                                                        continue;
                                                    if(dis1[i]<dis[i])
                                                    {   
                                                        //cout<<dis[i]<<" "<<i<<endl;
                                                        mini=max(mini,dis[i]-2);
                                                    }
                                                }
                                                //cout<<mini<<" "<<maxi<<endl;
                                                
                                                    if(mini==-1)
                                                    {   
                                                        cout<<dis[x]-1;
                                                        return 0;
                                                    }

                                                    cout<<mini<<endl;
                                                
                                                


                                             }    

Measured memory_bytes: 15232