Problem p03990 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int Maxn = 200010;
int sta, stb;
vector<int> veca[Maxn], vecb[Maxn];
int n, e[Maxn][2];
int disa[Maxn], disb[Maxn], size[Maxn], dfn[Maxn], id, fa[Maxn];
void dfs(int x) {
  int sz = vecb[x].size();
  size[x] = 1;
  dfn[x] = ++id;
  for (int i = 0; i < sz; i++) {
    int y = vecb[x][i];
    if (y == fa[x])
      continue;
    fa[y] = x;
    disb[y] = disb[x] + 1;
    dfs(y);
    size[x] += size[y];
  }
}
bool v[Maxn], bo[Maxn];
bool check(int x, int y) {
  if (disb[x] < disb[y])
    swap(x, y);
  if (disb[x] - disb[y] > 2)
    return true;
  if (dfn[y] <= dfn[x] && dfn[x] < dfn[y] + size[y])
    return false;
  if (fa[x] == fa[y])
    return false;
  return true;
}
void bfs() {
  queue<int> q;
  q.push(sta);
  bo[sta] = true;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int sz = veca[x].size();
    for (int i = 0; i < sz; i++) {
      int y = veca[x][i];
      disa[y] = disa[x] + 1;
      if (disa[y] < disb[y])
        bo[y] = true, q.push(y);
    }
  }
}
int _max(int x, int y) { return x > y ? x : y; }
int main() {
  int i, j, k;
  scanf("%d%d%d", &n, &sta, &stb);
  for (i = 1; i < n; i++)
    scanf("%d%d", &e[i][0], &e[i][1]);
  for (i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    vecb[x].push_back(y);
    vecb[y].push_back(x);
  }
  dfs(stb);
  for (i = 1; i < n; i++) {
    if (check(e[i][0], e[i][1]))
      v[e[i][0]] = v[e[i][1]] = true;
    else
      veca[e[i][0]].push_back(e[i][1]), veca[e[i][1]].push_back(e[i][0]);
  }
  bfs();
  int ans = 0;
  for (i = 1; i <= n; i++) {
    if (v[i] && bo[i]) {
      printf("-1\n");
      return 0;
    }
    if (bo[i])
      ans = _max(ans, disb[i] * 2);
  }
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int Maxn = 200010;
int sta, stb;
vector<int> veca[Maxn], vecb[Maxn];
int n, e[Maxn][2];
int disa[Maxn], disb[Maxn], size[Maxn], dfn[Maxn], id, fa[Maxn];
void dfs(int x) {
  int sz = vecb[x].size();
  size[x] = 1;
  dfn[x] = ++id;
  for (int i = 0; i < sz; i++) {
    int y = vecb[x][i];
    if (y == fa[x])
      continue;
    fa[y] = x;
    disb[y] = disb[x] + 1;
    dfs(y);
    size[x] += size[y];
  }
}
bool v[Maxn], bo[Maxn];
bool check(int x, int y) {
  if (disb[x] < disb[y])
    swap(x, y);
  if (disb[x] - disb[y] > 2)
    return true;
  if (dfn[y] <= dfn[x] && dfn[x] < dfn[y] + size[y])
    return false;
  if (fa[x] == fa[y])
    return false;
  return true;
}
void bfs() {
  queue<int> q;
  q.push(sta);
  bo[sta] = true;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    int sz = veca[x].size();
    for (int i = 0; i < sz; i++) {
      int y = veca[x][i];
      disa[y] = disa[x] + 1;
      if (disa[y] < disb[y] && !bo[y])
        bo[y] = true, q.push(y);
    }
  }
}
int _max(int x, int y) { return x > y ? x : y; }
int main() {
  int i, j, k;
  scanf("%d%d%d", &n, &sta, &stb);
  for (i = 1; i < n; i++)
    scanf("%d%d", &e[i][0], &e[i][1]);
  for (i = 1; i < n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    vecb[x].push_back(y);
    vecb[y].push_back(x);
  }
  dfs(stb);
  for (i = 1; i < n; i++) {
    if (check(e[i][0], e[i][1]))
      v[e[i][0]] = v[e[i][1]] = true;
    else
      veca[e[i][0]].push_back(e[i][1]), veca[e[i][1]].push_back(e[i][0]);
  }
  bfs();
  int ans = 0;
  for (i = 1; i <= n; i++) {
    if (v[i] && bo[i]) {
      printf("-1\n");
      return 0;
    }
    if (bo[i])
      ans = _max(ans, disb[i] * 2);
  }
  printf("%d\n", ans);
  return 0;
}