Language: C++

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
#define equals(a, b) (fabs((a) - (b)) < EPS)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;
const ll mod2 = 998244353;
const int INF = 1e9;
const long double EPS = 1e-10;
const int MAX = 100005;

vector<int> G[MAX];
int dt[MAX];
pair<int, int> da[MAX];
vector<int> d1[MAX];
bool visited[MAX];

void dfs(int u) {
  for (int v: G[u]) {
    if (dt[v] != -1) continue;
    dt[v] = dt[u] + 1;
    dfs(v);
  }
}

void dfs2(int u) {
  for (int v: G[u]) {
    if (da[v].first != -1) continue;
    da[v].first = da[u].first + 1;
    dfs2(v);
  }
}

int main() {
  int n, u, v;
  cin >> n >> u >> v;
  u--, v--;
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  rep(i, n) dt[i] = -1;
  dt[u] = 0;
  dfs(u);

  rep(i, n) da[i] = {-1, i};
  da[v] = 0;
  dfs2(v);
  sort(da.begin(), da.end());

  rep(i, n) {
    d1[dt[i]].push_back(i);
  }
  rep(i, n) {
    d2[da[i]].push_back(i);
  }

  int idxa = 0;

  rep(i, n) {
    bool flag = false;
    for (int x1: d1[i]) {
      if (!visited[x1]) {
        flag = true;
      }
    }
    if (!flag) {
      cout << i - 1 << endl;
      return 0;
    }
    
    while (idxa < n && da[idxa].first <= i) {
      visited[da[idxa].second] = true;
      idxa++;
    }

    flag = false;
    for (int x1: d1[i]) {
      if (!visited[x1]) {
        d1[i + 2].push_back(x1);
        flag = true;
      }
    }
    if (!flag) {
      cout << i << endl;
      return 0;
    }
  }







  
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:54:11: error: no match for 'operator=' (operand types are 'std::pair<int, int>' and 'int')
   54 |   da[v] = 0;
      |           ^
In file included from /usr/include/c++/14/bits/stl_algobase.h:64,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_pair.h:946:9: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, const _U1&>, std::is_assignable<_T2&, const _U2&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(const std::pair<_U1, _U2>&) [with _U2 = _U1; _T1 = int; _T2 = int]'
  946 |         operator=(const pair<_U1, _U2>& __p)
      |         ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:946:9: note:   template argument deduction/substitution failed:
a.cc:54:11: note:   mismatched types 'const std::pair<_T1, _T2>' and 'int'
   54 |   da[v] = 0;
      |           ^
/usr/include/c++/14/bits/stl_pair.h:957:9: note: candidate: 'template<class _U1, class _U2> typename std::enable_if<std::__and_<std::is_assignable<_T1&, _U1&&>, std::is_assignable<_T2&, _U2&&> >::value, std::pair<_T1, _T2>&>::type std::pair<_T1, _T2>::operator=(std::pair<_U1, _U2>&&) [with _U2 = _U1; _T1 = int; _T2 = int]'
  957 |         operator=(pair<_U1, _U2>&& __p)
      |         ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:957:9: note:   template argument deduction/substitution failed:
a.cc:54:11: note:   mismatched types 'std::pair<_T1, _T2>' and 'int'
   54 |   da[v] = 0;
      |           ^
/usr/include/c++/14/bits/stl_pair.h:921:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::__conditional_t<((bool)std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value), const std::pair<_T1, _T2>&, const std::__nonesuch&>) [with _T1 = int; _T2 = int; std::__conditional_t<((bool)std::__and_<std::is_copy_assignable<_T1>, std::is_copy_assignable<_T2> >::value), const std::pair<_T1, _T2>&, const std::__nonesuch&> = const std::pair<int, int>&]'
  921 |       operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:923:65: note:   no known conversion for argument 1 from 'int' to 'std::__conditional_t<true, const std::pair<int, int>&, const std::__nonesuch&>' {aka 'const std::pair<int, int>&'}
  921 |       operator=(__conditional_t<__and_<is_copy_assignable<_T1>,
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
  922 |                                        is_copy_assignable<_T2>>::value,
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  923 |                                 const pair&, const __nonesuch&> __p)
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~
/usr/include/c++/14/bits/stl_pair.h:931:7: note: candidate: 'std::pair<_T1, _T2>& std::pair<_T1, _T2>::operator=(std::__conditional_t<((bool)std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value), std::pair<_T1, _T2>&&, std::__nonesuch&&>) [with _T1 = int; _T2 = int; std::__conditional_t<((bool)std::__and_<std::is_move_assignable<_Tp>, std::is_move_assignable<_T2> >::value), std::pair<_T1, _T2>&&, std::__nonesuch&&> = std::pair<int, int>&&]'
  931 |       operator=(__conditional_t<__and_<is_move_assignable<_T1>,
      |       ^~~~~~~~
/usr/include/c++/14/bits/stl_pair.h:933:55: note:   no known conversion for argument 1 from 'int' to 'std::__conditional_t<true, std::pair<int, int>&&, std::__nonesuch&&>' {aka 'std::pair<int, int>&&'}
  931 |       operator=(__conditional_t<__and_<is_move_assignable<_T1>,
      |                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  932 |                                        is_move_assignable<_T2>>::value,
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  933 |                                 pair&&, __nonesuch&&> __p)
      |                                 ~~~~~~~~~~~~~~~~~~~~~~^~~
a.cc:56:11: error: request for member 'begin' in 'da', which is of non-class type 'std::pair<int, int> [100005]'
   56 |   sort(da.begin(), da.end());
      |           ^~~~~
a.cc:56:23: error: request for member 'end' in 'da', which is of non-class type 'std::pair<int, int> [100005]'
   56 |   sort(da.begin(), da.end());
      |                       ^~~
a.cc:62:5: error: 'd2' was not declared in this scope; did you mean 'dt'?
   62 |     d2[da[i]].push_back(i);
      |     ^~
      |     dt