Language: C++

#include<bits/stdc++.h>
using namespace std;
#define rep(i,j,n) for(int i=(int)(j);i<(int)(n);i++)
#define REP(i,j,n) for(int i=(int)(j);i<=(int)(n);i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(),(a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int,int>
#define priq priority_queue<int>
#define disup(A,key) distance(A.begin(),upper_bound(ALL(A),(int)(key)))
#define dislow(A,key) distance(A.begin(),lower_bound(ALL(A),(int)(key)))
#define tii tuple<int,int,int>
signed main(){
  int N; cin>>N;
  int T,A; cin>>T>>A;
  T--; A--;
  vii X(N);
  vi upA,downA;
  rep(i,0,N-1){
    int a,b; cin>>a>>b;
    if(a>b)
      swap(a,b);
    if(a!=A&&b!=A){
      X[a-1].push_back(b-1);
      X[b-1].push_back(a-1);
    }
    else if(a==A)
      downA.push_back(b-1);
    else if(b==A)
      upA.push_back(a-1);
  }
  int TA=-1;
  int count1=1,count2=1;
  stuck<int> Q;
  vector<bool> F(N,1);
  rep(i,0,upA.size()){
    Q.push(upA[i]);
    F[upA[i]]=0;
    if(upA[i]==T)
      TA=1;
  }
  F[A]=0;
  Q.push(-1);
  int memo=A;
  while(1){
    count1++;
    int K=Q.front();
    Q.pop();
    if(K==-1)
      count1=1;
    else{
      rep(i,0,X[K].size()){
        if(F[X[K][i]]){
          Q.push(X[K][i]);
          F[X[K][i]]=0;
        }
      }
      if(A==K){
        TA=count1;
        break;
      }
    }
    if(Q.size()==0)
      break;
  }
  if(TA==-1){
  count1=1;
  stuck<int> QA;
  vector<bool> FA(N,1);
  rep(i,0,downA.size()){
    Q.push(downA[i]);
    F[downA[i]]=0;
    if(downA[i]==T)
      TA=1;
  }
  FA[A]=0;
  QA.push(-1);
  int memo=A;
  while(1){
    count1++;
    int K=QA.front();
    QA.pop();
    if(K==-1)
      count1=1;
    else{
      rep(i,0,X[K].size()){
        if(FA[X[K][i]]){
          QA.push(X[K][i]);
          FA[X[K][i]]=0;
        }
      }
      if(A==K){
        TA=count1;
        break;
      }
    }
    if(Q.size()==0)
      break;
  }
    queue<int> Z;
  vector<bool> P(N,1);
  vector<bool> PP(N,1);
  queue<int> QQ;
        QQ.push(T);
    PP[T]=0;
        QQ.push(-1);
        int count=0;
        while(1){
          int k=QQ.front();
          QQ.pop();
          if(k==-1){
            count++;
            if(count==(TA+1)/2)
              break;
          }
          else{
            P[k]=0;
            rep(i,0,X[k].size()){
              if(PP[X[k][i]]){
                QQ.push(X[K][i]);
                PP[X[k][i]]=0;
              }
            }
          }
        }
  rep(i,0,upA.size()){
    Z.push(upA[i]);
    P[upA[i]]=0;
    if(upA[i]==T)
      TA=1;
  }
  P[A]=0;
  Z.push(-1);
  memo=A;
  while(1){
    int K=Z.front();
    Z.pop();
    if(K==memo)
      break;
    if(K==-1){
      Z.push(-1);
      count2++;
    }
    else{
      rep(i,0,X[K].size()){
        if(P[X[K][i]]){
          Z.push(X[K][i]);
          P[X[K][i]]=0;
        }
      }
    }
    memo=K;
  }
  }
  else{
  queue<int> Z;
  vector<bool> P(N,1);
  vector<bool> PP(N,1);
  queue<int> QQ;
        QQ.push(T);
    PP[T]=0;
        QQ.push(-1);
        int count=0;
        while(1){
          int k=QQ.front();
          QQ.pop();
          if(k==-1){
            count++;
            if(count==(TA+1)/2)
              break;
          }
          else{
            P[k]=0;
            rep(i,0,X[k].size()){
              if(PP[X[k][i]]){
                QQ.push(X[K][i]);
                PP[X[k][i]]=0;
              }
            }
          }
        }
  rep(i,0,downA.size()){
    Z.push(downA[i]);
    P[downA[i]]=0;
    if(downA[i]==T)
      TA=1;
  }
  P[A]=0;
  Z.push(-1);
  memo=A;
  while(1){
    int K=Z.front();
    Z.pop();
    if(K==memo)
      break;
    if(K==-1){
      Z.push(-1);
      count2++;
    }
    else{
      rep(i,0,X[K].size()){
        if(P[X[K][i]]){
          Z.push(X[K][i]);
          P[X[K][i]]=0;
        }
      }
    }
    memo=K;
  }
  }
  ifg(TA%2==1)
    count2--;
  cout<<count2<<endl;
  }



Compiler output:
a.cc: In function 'int main()':
a.cc:36:3: error: 'stuck' was not declared in this scope; did you mean 'struct'?
   36 |   stuck<int> Q;
      |   ^~~~~
      |   struct
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:36:9: note: in expansion of macro 'int'
   36 |   stuck<int> Q;
      |         ^~~
a.cc:39:5: error: 'Q' was not declared in this scope
   39 |     Q.push(upA[i]);
      |     ^
a.cc:45:3: error: 'Q' was not declared in this scope
   45 |   Q.push(-1);
      |   ^
a.cc:6:13: error: expected primary-expression before 'long'
    6 | #define int long long
      |             ^~~~
a.cc:70:9: note: in expansion of macro 'int'
   70 |   stuck<int> QA;
      |         ^~~
a.cc:79:3: error: 'QA' was not declared in this scope; did you mean 'FA'?
   79 |   QA.push(-1);
      |   ^~
      |   FA
a.cc:122:27: error: 'K' was not declared in this scope
  122 |                 QQ.push(X[K][i]);
      |                           ^
a.cc:178:27: error: 'K' was not declared in this scope
  178 |                 QQ.push(X[K][i]);
      |                           ^
a.cc:213:3: error: 'ifg' was not declared in this scope
  213 |   ifg(TA%2==1)
      |   ^~~