Problem p02834 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define PER(i, n) for (ll i = n; i >= 1; i--)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using vc = vector<char>;
using vcc = vector<vc>;
using vP = vector<P>;
using vPP = vector<vP>;
using vPl = vector<Pl>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void dfs(int v, vi &res, const vii tree) {
  for (int nv : tree[v]) {
    if (res[nv] != inf)
      continue;
    res[nv] = res[v] + 1;
    dfs(nv, res, tree);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, u, v;
  cin >> n >> u >> v;
  u--;
  v--;
  vii tree(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  vi t(n, inf), a(n, inf);
  t[u] = 0, a[v] = 0;
  dfs(u, t, tree);
  dfs(v, a, tree);
  int ans = 0;
  rep(i, n) {
    if (t[i] + 1 > a[i])
      continue;
    int d = a[i];
    if (sz(tree[i]) == 1)
      d--;
    chmax(ans, d);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP(i, n) for (ll i = 1; i < n; i++)
#define PER(i, n) for (ll i = n; i >= 1; i--)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int, int>;
using Pl = pair<ll, ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using vc = vector<char>;
using vcc = vector<vc>;
using vP = vector<P>;
using vPP = vector<vP>;
using vPl = vector<Pl>;
const int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dy[] = {1, 0, -1, 0, 1, -1, -1, 1};
const int inf = (1 << 30) - 1;
const ll infll = (1LL << 62) - 1;
ll ceil(const ll &a, const ll &b) { return ((a) + (b)-1) / b; }
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

void dfs(int v, vi &res, const vii &tree) {
  for (int nv : tree[v]) {
    if (res[nv] != inf)
      continue;
    res[nv] = res[v] + 1;
    dfs(nv, res, tree);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, u, v;
  cin >> n >> u >> v;
  u--;
  v--;
  vii tree(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    tree[a].push_back(b);
    tree[b].push_back(a);
  }
  vi t(n, inf), a(n, inf);
  t[u] = 0, a[v] = 0;
  dfs(u, t, tree);
  dfs(v, a, tree);
  int ans = 0;
  rep(i, n) {
    if (t[i] + 1 > a[i])
      continue;
    int d = a[i];
    if (sz(tree[i]) == 1)
      d--;
    chmax(ans, d);
  }
  cout << ans << endl;
}