Problem p02834 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <bitset>

using namespace std;

#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i, st, ed) for (int i = st; i < ed; i++)
#define repn(i, st, ed) for (int i = st; i <= ed; i++)
#define repb(i, ed, st) for (int i = ed; i >= st; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;

const long double PI = 3.141592653589793238463;
const int N = 1010;
const ll INF = 1LL << 60;

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

// Main Code

vector<int> g[N];
bool visA[N];
bool visT[N];
ll distA[N];
ll distT[N];

// Normal DFS
void dfsA(int curr, int par, int dist) {
  distA[curr] = dist;
  visA[curr] = 1;
  for (auto x : g[curr]) {
    if (visA[x] == 0) {
      dfsA(x, curr, dist + 1);
    } else {
      if (distA[x] > (distA[curr] + 1)) {
        dfsA(x, curr, dist + 1);
      }
    }
  }
}

void dfsT(int curr, int par, int dist) {
  distT[curr] = dist;
  visT[curr] = 1;
  for (auto x : g[curr]) {
    if (visT[x] == 0) {
      dfsT(x, curr, dist + 1);
    } else {
      if (distT[x] > (distT[curr] + 1)) {
        dfsT(x, curr, dist + 1);
      }
    }
  }
}

int main() {
  int n, u, v;
  cin >> n >> u >> v;
  rep(i, 1, n) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  dfsA(v, -1, 0);
  dfsT(u, -1, 0);
  ll ans = 0;
  repn(i, 1, n) {
    if (distA[i] > distT[i]) {
      chmax(ans, distA[i] - 1);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <bitset>

using namespace std;

#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i, st, ed) for (int i = st; i < ed; i++)
#define repn(i, st, ed) for (int i = st; i <= ed; i++)
#define repb(i, ed, st) for (int i = ed; i >= st; i--)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;

const long double PI = 3.141592653589793238463;
const int N = 1e5 + 10;
const ll INF = 1LL << 60;

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}
template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

// Main Code

vector<int> g[N];
bool visA[N];
bool visT[N];
ll distA[N];
ll distT[N];

// Normal DFS
void dfsA(int curr, int par, int dist) {
  distA[curr] = dist;
  visA[curr] = 1;
  for (auto x : g[curr]) {
    if (visA[x] == 0) {
      dfsA(x, curr, dist + 1);
    } else {
      if (distA[x] > (distA[curr] + 1)) {
        dfsA(x, curr, dist + 1);
      }
    }
  }
}

void dfsT(int curr, int par, int dist) {
  distT[curr] = dist;
  visT[curr] = 1;
  for (auto x : g[curr]) {
    if (visT[x] == 0) {
      dfsT(x, curr, dist + 1);
    } else {
      if (distT[x] > (distT[curr] + 1)) {
        dfsT(x, curr, dist + 1);
      }
    }
  }
}

int main() {
  int n, u, v;
  cin >> n >> u >> v;
  rep(i, 1, n) {
    int x, y;
    cin >> x >> y;
    g[x].pb(y);
    g[y].pb(x);
  }
  dfsA(v, -1, 0);
  dfsT(u, -1, 0);
  ll ans = 0;
  repn(i, 1, n) {
    if (distA[i] > distT[i]) {
      chmax(ans, distA[i] - 1);
    }
  }
  cout << ans << endl;
}