Problem p03990 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define N 210000
#define pb push_back
using namespace std;
struct node {
  int y, nex;
} a[2 * N];
int e[N][2], len, fir[N], n, sa, sb, dep[N], dfn[N], las[N], id, f[N], p[N],
    tail, d[N], ans;
bool b[N], v[N];
vector<int> A[N];
void ins(int x, int y) {
  a[++len].y = y;
  a[len].nex = fir[x];
  fir[x] = len;
}
void dfs(int x, int fa) {
  f[x] = fa;
  dfn[x] = ++id;
  dep[x] = dep[fa] + 1;
  for (int k = fir[x]; k; k = a[k].nex) {
    int y = a[k].y;
    if (y == fa)
      continue;
    dfs(y, x);
  }
  las[x] = id;
}
bool check(int x, int y) {
  if (dep[x] > dep[y])
    swap(x, y);
  if (dfn[x] <= dfn[y] && las[x] >= dfn[y]) {
    if (dep[y] - dep[x] > 2)
      return 1;
    return 0;
  }
  if (f[x] == f[y])
    return 0;
  return 1;
}
void bfs() {
  v[sa] = 1;
  p[1] = sa;
  tail = 1;
  for (int i = 1; i <= tail; i++) {
    int x = p[i], siz = A[x].size();
    for (int k = 0; k < siz; k++) {
      int y = A[x][k];
      d[y] = d[x] + 1;
      if (d[y] < dep[y]) {
        v[y] = 1;
        p[++tail] = y;
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &sa, &sb);
  for (int i = 1; i < n; i++)
    scanf("%d%d", &e[i][0], &e[i][1]);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  dfs(sb, 0);
  for (int i = 1; i <= n; i++)
    dep[i]--;
  for (int i = 1; i < n; i++) {
    int x = e[i][0], y = e[i][1];
    if (check(x, y))
      b[x] = b[y] = 1;
    else
      A[e[i][0]].pb(e[i][1]), A[e[i][1]].pb(e[i][0]);
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    if (v[i] && b[i]) {
      printf("-1\n");
      return 0;
    }
    if (v[i])
      ans = max(ans, dep[i] * 2);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#define N 210000
#define pb push_back
using namespace std;
struct node {
  int y, nex;
} a[2 * N];
int e[N][2], len, fir[N], n, sa, sb, dep[N], dfn[N], las[N], id, f[N], p[N],
    tail, d[N], ans;
bool b[N], v[N];
vector<int> A[N];
void ins(int x, int y) {
  a[++len].y = y;
  a[len].nex = fir[x];
  fir[x] = len;
}
void dfs(int x, int fa) {
  f[x] = fa;
  dfn[x] = ++id;
  dep[x] = dep[fa] + 1;
  for (int k = fir[x]; k; k = a[k].nex) {
    int y = a[k].y;
    if (y == fa)
      continue;
    dfs(y, x);
  }
  las[x] = id;
}
bool check(int x, int y) {
  if (dep[x] > dep[y])
    swap(x, y);
  if (dfn[x] <= dfn[y] && las[x] >= dfn[y]) {
    if (dep[y] - dep[x] > 2)
      return 1;
    return 0;
  }
  if (f[x] == f[y])
    return 0;
  return 1;
}
void bfs() {
  v[sa] = 1;
  p[1] = sa;
  tail = 1;
  for (int i = 1; i <= tail; i++) {
    int x = p[i], siz = A[x].size();
    for (int k = 0; k < siz; k++) {
      int y = A[x][k];
      d[y] = d[x] + 1;
      if (d[y] < dep[y] && v[y] == 0) {
        v[y] = 1;
        p[++tail] = y;
      }
    }
  }
}
int main() {
  scanf("%d%d%d", &n, &sa, &sb);
  for (int i = 1; i < n; i++)
    scanf("%d%d", &e[i][0], &e[i][1]);
  for (int i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    ins(x, y);
    ins(y, x);
  }
  dfs(sb, 0);
  for (int i = 1; i <= n; i++)
    dep[i]--;
  for (int i = 1; i < n; i++) {
    int x = e[i][0], y = e[i][1];
    if (check(x, y))
      b[x] = b[y] = 1;
    else
      A[e[i][0]].pb(e[i][1]), A[e[i][1]].pb(e[i][0]);
  }
  bfs();
  for (int i = 1; i <= n; i++) {
    if (v[i] && b[i]) {
      printf("-1\n");
      return 0;
    }
    if (v[i])
      ans = max(ans, dep[i] * 2);
  }
  printf("%d\n", ans);
  return 0;
}