Language: C++

#include <bits/stdc++.h>
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

using namespace std;

const long long INF = 1LL << 60;

//各桁の和を出力
long long Sum(long long n) {
  long long m = 0;
  while (n) {
    m += n % 10;
    n /= 10;
  }
  return m;
}

const int MAX = 510000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void Comuse() {
  fac[0] = fac[1] = 1;
  finv[0] = finv[1] = 1;
  inv[1] = 1;
  for (int i = 2; i < MAX; i++) {
    fac[i] = fac[i - 1] * i % MOD;
    inv[i] = MOD - inv[MOD % i] * (MOD / i) % MOD;
    finv[i] = finv[i - 1] * inv[i] % MOD;
  }
}

#define comuse Comuse()
// nCk(comuseを使え)
long long combi(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
// pCk(comuseを使え)
long long perm(int n, int k) {
  if (n < k) return 0;
  if (n < 0 || k < 0) return 0;
  return fac[n] * (finv[k] % MOD) % MOD;
}

//最小公倍数(aとbの)
long long lcm(long long a, long long b) {
  long long n;
  n = a / __gcd(a, b) * b;
  return n;
}

// auto A = div(n)
vector<long long> div(long long n) {
  vector<long long> ret;
  for (long long i = 1; i * i <= n; i++) {
    if (n % i == 0) {
      ret.push_back(i);
      if (i * i != n) {
        ret.push_back(n / i);
      }
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

//(aのn乗をmodで割ったあまり)
long long modpow(long long a, long long n, long long mod) {
  long long ans = 1;
  while (n > 0) {
    if (n & 1) {
      ans = ans * a % mod;
    }

    a = a * a % mod;

    n >>= 1;
  }

  return ans;
}

void solve();

const int MAX_N = 131072;
// segment tree
int NN;
int seg[MAX_N * 2 - 1];
void seguse() {
  for (int i = 0; i < 2 * NN - 1; i++) {
    seg[i] = INT_MAX;
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(20) << fixed;
  solve();
}

///////////////////////////////////////////////////////////

int N, takahashi, aoki;
vector<bool> used(1000000);
vector<bool> used2(1000000);
vector<int> route;
bool correctPath = false;
int halfSize;
int Max = 0;
int to;
int counter = 0;

void searchPath(int t, &vector<vector<int>> ki){
  if(t == takahashi || counter >= 10000){
    correctPath = true;
    return;
  }
  for(int i = 0; i < ki.at(t).size(); i++){
    counter++;
    to = ki.at(t).at(i);
    if(used.at(to)){
      continue;
    }
    used.at(to) = true;
    searchPath(to, ki);
    if(correctPath){
      route.push_back(to);
      return;
    }
  }
  return;
}

void longestPath(int t, &vector<vector<int>> ki, int steps){
  //cout << "t:" << t << " steps:" << steps <<endl;
  if(steps > Max){
    Max = steps;
  }
  for(int i = 0; i < ki.at(t).size(); i++){
    int to = ki.at(t).at(i);
    if(used2.at(to)){
      //cout << "continue" <<endl;
      continue;
    }
    used2.at(to) = true;
    longestPath(to, ki, steps + 1);
  }
  return;
}

void solve() {
  cin >> N >> takahashi >> aoki;
  takahashi--;
  aoki--;
  vector<vector<int>> ki(N);
  for(int i = 0; i < N-1; i++){
    int a, b;
    cin >> a >> b;
    ki.at(a-1).push_back(b-1);
    ki.at(b-1).push_back(a-1);
  }
  searchPath(aoki, ki);
  route.erase(route.begin());
  //for(int i = 0; i < route.size(); i++){
  //  cout << route.at(i) <<endl;
  //}
  halfSize = route.size() / 2;
  if(route.size() == 0){
    used2.at(aoki) = true;
    used2.at(takahashi) = true;
    longestPath(takahashi, ki, 0);
  }
  else if(route.size() == 1){
    used2.at(route.at(halfSize)) = true;
    used2.at(takahashi) = true;
    longestPath(takahashi, ki, 0);
  } else{
    used2.at(route.at(halfSize)) = true;
    used2.at(route.at(halfSize-1)) = true;
    longestPath(route.at(halfSize-1), ki, 0);
  }
  int Path = halfSize + Max;
  if(route.size() % 2 == 0){
    //cout << Max <<endl;
    cout << Path <<endl;
  }
  else{
    cout << Path + 1 <<endl;
  }
}



Compiler output:
a.cc:126:24: error: expected identifier before '&' token
  126 | void searchPath(int t, &vector<vector<int>> ki){
      |                        ^
a.cc:126:25: error: expected ',' or '...'
  126 | void searchPath(int t, &vector<vector<int>> ki){
      |                         ^~~~~~~~~~~~~~~~~~~
a.cc: In function 'void searchPath(int, int&)':
a.cc:131:22: error: 'ki' was not declared in this scope; did you mean 'i'?
  131 |   for(int i = 0; i < ki.at(t).size(); i++){
      |                      ^~
      |                      i
a.cc: At global scope:
a.cc:147:25: error: expected identifier before '&' token
  147 | void longestPath(int t, &vector<vector<int>> ki, int steps){
      |                         ^
a.cc:147:26: error: expected ',' or '...'
  147 | void longestPath(int t, &vector<vector<int>> ki, int steps){
      |                          ^~~~~~~~~~~~~~~~~~~
a.cc: In function 'void longestPath(int, int&)':
a.cc:149:6: error: 'steps' was not declared in this scope
  149 |   if(steps > Max){
      |      ^~~~~
a.cc:152:22: error: 'ki' was not declared in this scope; did you mean 'i'?
  152 |   for(int i = 0; i < ki.at(t).size(); i++){
      |                      ^~
      |                      i
a.cc:159:25: error: 'steps' was not declared in this scope
  159 |     longestPath(to, ki, steps + 1);
      |                         ^~~~~
a.cc: In function 'void solve()':
a.cc:175:20: error: invalid initialization of reference of type 'int&' from expression of type 'std::vector<std::vector<int> >'
  175 |   searchPath(aoki, ki);
      |                    ^~
a.cc:126:24: note: in passing argument 2 of 'void searchPath(int, int&)'
  126 | void searchPath(int t, &vector<vector<int>> ki){
      |                        ^~~~~~~~~~~~~~~~~~~~
a.cc:184:28: error: invalid initialization of reference of type 'int&' from expression of type 'std::vector<std::vector<int> >'
  184 |     longestPath(takahashi, ki, 0);
      |                            ^~
a.cc:147:25: note: in passing argument 2 of 'void longestPath(int, int&)'
  147 | void longestPath(int t, &vector<vector<int>> ki, int steps){
      |                         ^~~~~~~~~~~~~~~~~~~~
a.cc:189:28: error: invalid initialization of reference of type 'int&' from expression of type 'std::vector<std::vector<int> >'
  189 |     longestPath(takahashi, ki, 0);
      |                            ^~
a.cc:147:25: note: in passing argument 2 of 'void longestPath(int, int&)'
  147 | void longestPath(int t, &vector<vector<int>> ki, int steps){
      |                         ^~~~~~~~~~~~~~~~~~~~
a.cc:193:39: error: invalid initialization of reference of type 'int&' from expression of type 'std::vector<std::vector<int> >'
  193 |     longestPath(route.at(halfSize-1), ki, 0);
      |                                       ^~
a.cc:147:25: note: in passing argument 2 of 'void longestPath(int, int&)'
  147 | void longestPath(int t, &vector<vector<int>> ki, int steps){
      |                         ^~~~~~~~~~~~~~~~~~~~