Problem p03990 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define N 200005
using namespace std;
int n, u, v, fa[N], fst[N], to[N], nxt[N], l, dep[N], dis[N], Ans, f[N], q[N];
struct T {
  int x, y;
} a[N], b[N];
vector<int> e[N];
void link(int x, int y) {
  to[++l] = y;
  nxt[l] = fst[x];
  fst[x] = l;
  to[++l] = x;
  nxt[l] = fst[y];
  fst[y] = l;
}
void dfs(int x) {
  for (int i = fst[x]; i; i = nxt[i])
    if (to[i] != fa[x]) {
      fa[to[i]] = x;
      dep[to[i]] = dep[x] + 1;
      dfs(to[i]);
    }
}
int Gdis(int x, int y) {
  if (x == y)
    return 0;
  if (fa[x] == y || fa[y] == x)
    return 1;
  if (fa[x] == fa[y] || fa[fa[x]] == y || fa[fa[y]] == x)
    return 2;
  return 3;
}
int main() {
  scanf("%d%d%d", &n, &u, &v);
  // if (u==v){puts("0");,return 0;}
  for (int i = 1; i < n; i++)
    scanf("%d%d", &a[i].x, &a[i].y);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &b[i].x, &b[i].y);
    link(b[i].x, b[i].y);
  }
  dfs(v);
  for (int i = 1; i < n; i++) {
    int tmp = Gdis(a[i].x, a[i].y);
    if (tmp == 1) {
      e[a[i].x].push_back(a[i].y);
      e[a[i].y].push_back(a[i].x);
    } else if (tmp == 2) {
      if (fa[a[i].x] != v || fa[a[i].y] != v) {
        e[a[i].x].push_back(a[i].y);
        e[a[i].y].push_back(a[i].x);
      }
    } else
      f[a[i].x] = f[a[i].y] = 1;
  }
  memset(dis, 0x3f, sizeof dis);
  int l = 0, r = 1;
  q[1] = u;
  dis[u] = 0;
  while (l < r) {
    int t = q[++l];
    // cout<<t<<' '<<dis[t]<<endl;
    if (f[t] == 1) {
      puts("-1");
      return 0;
    } else
      Ans = max(Ans, dep[t] * 2);
    for (int i = 0; i < e[t].size(); i++)
      if (dis[e[t][i]] > dis[t] + 1 && dis[t] + 1 < dep[e[t][i]]) {
        dis[e[t][i]] = dis[t] + 1;
        q[++r] = e[t][i];
      }
  }
  printf("%d\n", Ans);
}

Fixed submission:
#include <bits/stdc++.h>
#define N 400005
using namespace std;
int n, u, v, fa[N], fst[N], to[N], nxt[N], l, dep[N], dis[N], Ans, f[N], q[N];
struct T {
  int x, y;
} a[N], b[N];
vector<int> e[N];
void link(int x, int y) {
  to[++l] = y;
  nxt[l] = fst[x];
  fst[x] = l;
  to[++l] = x;
  nxt[l] = fst[y];
  fst[y] = l;
}
void dfs(int x) {
  for (int i = fst[x]; i; i = nxt[i])
    if (to[i] != fa[x]) {
      fa[to[i]] = x;
      dep[to[i]] = dep[x] + 1;
      dfs(to[i]);
    }
}
int Gdis(int x, int y) {
  if (x == y)
    return 0;
  if (fa[x] == y || fa[y] == x)
    return 1;
  if (fa[x] == fa[y] || fa[fa[x]] == y || fa[fa[y]] == x)
    return 2;
  return 3;
}
int main() {
  scanf("%d%d%d", &n, &u, &v);
  // if (u==v){puts("0");,return 0;}
  for (int i = 1; i < n; i++)
    scanf("%d%d", &a[i].x, &a[i].y);
  for (int i = 1; i < n; i++) {
    scanf("%d%d", &b[i].x, &b[i].y);
    link(b[i].x, b[i].y);
  }
  dfs(v);
  for (int i = 1; i < n; i++) {
    int tmp = Gdis(a[i].x, a[i].y);
    if (tmp == 1) {
      e[a[i].x].push_back(a[i].y);
      e[a[i].y].push_back(a[i].x);
    } else if (tmp == 2) {
      if (fa[a[i].x] != v || fa[a[i].y] != v) {
        e[a[i].x].push_back(a[i].y);
        e[a[i].y].push_back(a[i].x);
      }
    } else
      f[a[i].x] = f[a[i].y] = 1;
  }
  memset(dis, 0x3f, sizeof dis);
  int l = 0, r = 1;
  q[1] = u;
  dis[u] = 0;
  while (l < r) {
    int t = q[++l];
    // cout<<t<<' '<<dis[t]<<endl;
    if (f[t] == 1) {
      puts("-1");
      return 0;
    } else
      Ans = max(Ans, dep[t] * 2);
    for (int i = 0; i < e[t].size(); i++)
      if (dis[e[t][i]] > dis[t] + 1 && dis[t] + 1 < dep[e[t][i]]) {
        dis[e[t][i]] = dis[t] + 1;
        q[++r] = e[t][i];
      }
  }
  printf("%d\n", Ans);
}