Problem p02834 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

vector<vector<int>> gr;

void dfs(int v, int p, int depth, vector<vector<int>> &levels) {
  levels[depth].pb(v);
  for (int u : gr[v])
    if (u != p)
      dfs(u, v, depth + 1, levels);
}

vector<vector<int>> a, b;

bool check(int m) {
  set<int> x, y;
  for (int i = m; i >= 0; i -= 2) {
    for (int j : a[i])
      x.insert(j);
    for (int j : b[i])
      y.insert(j);
  }
  for (int i : x)
    if (y.find(i) == y.end())
      return false;
  return true;
}

bool check1(int m) {
  set<int> x, y;
  for (int i = m; i >= 0; i -= 2) {
    for (int j : a[i])
      x.insert(j);
    for (int j : b[i - 1])
      y.insert(j);
  }
  for (int i : x)
    if (y.find(i) == y.end())
      return false;
  return true;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n, x, y;
  cin >> n >> x >> y, x--, y--;
  gr.resize(n);
  forn(i, 1, n) {
    int v, u;
    cin >> v >> u, v--, u--;
    gr[v].pb(u), gr[u].pb(v);
  }
  a.resize(n + 2), b.resize(n + 2);
  dfs(x, -1, 0, a), dfs(y, -1, 0, b);
  /*forn (i, 0, n) {
      cout << i << ":";
      for (int j : a[i])
          cout << j << " ";
      cout << "\n";
  }
  forn (i, 0, n) {
      cout << i << ":";
      for (int j : b[i])
          cout << j << " ";
      cout << "\n";
  }*/
  /*forn (i, 0, n - 1)  {
      cout << i << ": " << check(i) << "\n";
  }
  cout << "\n";
  forn (i, 1, n)
      cout << i << ": " << check1(i) << "\n";*/
  int l = 0, r = n - 1;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (check(m))
      r = m;
    else
      l = m;
  }
  int ans;
  if (check(r))
    ans = r;
  else
    ans = 0;
  // cout << ans << " ";
  l = 1, r = n - 1;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (check1(m))
      r = m;
    else
      l = m;
  }
  if (check1(r))
    ans = max(ans, r - 1);
  cout << ans;
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>

using namespace std;

// vector string deque break continue
#define forn(i, s, f) for (int i = (int)s; i < (int)f; i++)
#define ll long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fs first
#define sc second
#define pf push_front
#define pb push_back
#define pop_f pop_front
#define pop_b pop_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) (int)(x).size()

vector<vector<int>> gr;

void dfs(int v, int p, int depth, vector<vector<int>> &levels) {
  levels[depth].pb(v);
  for (int u : gr[v])
    if (u != p)
      dfs(u, v, depth + 1, levels);
}

vector<vector<int>> a, b;

bool check(int m) {
  set<int> x, y;
  for (int i = m; i >= 0; i -= 2) {
    for (int j : a[i])
      x.insert(j);
    for (int j : b[i])
      y.insert(j);
  }
  for (int i : x)
    if (y.find(i) == y.end())
      return false;
  return true;
}

bool check1(int m) {
  set<int> x, y;
  for (int i = m; i >= 0; i -= 2) {
    for (int j : a[i])
      x.insert(j);
    if (i - 1 >= 0)
      for (int j : b[i - 1])
        y.insert(j);
  }
  for (int i : x)
    if (y.find(i) == y.end())
      return false;
  return true;
}

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n, x, y;
  cin >> n >> x >> y, x--, y--;
  gr.resize(n);
  forn(i, 1, n) {
    int v, u;
    cin >> v >> u, v--, u--;
    gr[v].pb(u), gr[u].pb(v);
  }
  a.resize(n + 2), b.resize(n + 2);
  dfs(x, -1, 0, a), dfs(y, -1, 0, b);
  /*forn (i, 0, n) {
      cout << i << ":";
      for (int j : a[i])
          cout << j << " ";
      cout << "\n";
  }
  forn (i, 0, n) {
      cout << i << ":";
      for (int j : b[i])
          cout << j << " ";
      cout << "\n";
  }*/
  /*forn (i, 0, n - 1)  {
      cout << i << ": " << check(i) << "\n";
  }
  cout << "\n";
  forn (i, 1, n)
      cout << i << ": " << check1(i) << "\n";*/
  int l = 0, r = n - 1;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (check(m))
      r = m;
    else
      l = m;
  }
  int ans;
  if (check(r))
    ans = r;
  else
    ans = 0;
  // cout << ans << " ";
  l = 1, r = n - 1;
  while (r - l > 1) {
    int m = (l + r) / 2;
    if (check1(m))
      r = m;
    else
      l = m;
  }
  if (check1(r))
    ans = max(ans, r - 1);
  cout << ans;
  return 0;
}