Problem p03990 (C++)

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

const int RLEN = 1 << 18 | 1;
inline char nc() {
  static char ibuf[RLEN], *ib, *ob;
  (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ib == ob) ? -1 : *ib++;
}
inline int rd() {
  char ch = nc();
  int i = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = nc();
  }
  while (isdigit(ch)) {
    i = (i << 1) + (i << 3) + ch - '0';
    ch = nc();
  }
  return i * f;
}

const int N = 1e5 + 50;
int n, col[N];
struct Tree {
  int stx, fa[N], dep[N];
  vector<int> edge[N];
  inline void dfs(int x, int f, int d = 0) {
    fa[x] = f;
    dep[x] = d;
    for (auto v : edge[x])
      if (v != f)
        dfs(v, x, d + 1);
  }
  inline void init() {
    for (int i = 1; i < n; i++) {
      int x = rd(), y = rd();
      edge[x].push_back(y);
      edge[y].push_back(x);
    }
    dfs(stx, 0);
  }
  inline bool valid(int x, int y, int rs = 0) {
    while (dep[x] > dep[y])
      if ((x = fa[x], ++rs) >= 3)
        return false;
    while (dep[x] < dep[y])
      if ((y = fa[y], ++rs) >= 3)
        return false;
    while (x != y)
      if ((y = fa[y], x = fa[x], rs += 2) >= 3)
        return false;
    return true;
  }
} A, B;
inline void dfs(int x) {
  if (A.dep[x] >= B.dep[x])
    return;
  col[x] = 1;
  for (auto v : A.edge[x])
    if (v != A.fa[x])
      dfs(v);
}
int main() {
  n = rd();
  A.stx = rd();
  B.stx = rd();
  if (A.stx == B.stx) {
    puts("0");
    return 0;
  }
  A.init();
  B.init();
  dfs(A.stx);
  for (int i = 1; i <= n; i++)
    for (auto v : A.edge[i])
      if (col[i] && !B.valid(i, v)) {
        puts("-1");
        return 0;
      }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (col[i])
      ans = max(ans, B.dep[i]);
  cout << (ans << 1) << '\n';
}

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

const int RLEN = 1 << 18 | 1;
inline char nc() {
  static char ibuf[RLEN], *ib, *ob;
  (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));
  return (ib == ob) ? -1 : *ib++;
}
inline int rd() {
  char ch = nc();
  int i = 0, f = 1;
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = nc();
  }
  while (isdigit(ch)) {
    i = (i << 1) + (i << 3) + ch - '0';
    ch = nc();
  }
  return i * f;
}

const int N = 2e5 + 50;
int n, col[N];
struct Tree {
  int stx, fa[N], dep[N];
  vector<int> edge[N];
  inline void dfs(int x, int f, int d = 0) {
    fa[x] = f;
    dep[x] = d;
    for (auto v : edge[x])
      if (v != f)
        dfs(v, x, d + 1);
  }
  inline void init() {
    for (int i = 1; i < n; i++) {
      int x = rd(), y = rd();
      edge[x].push_back(y);
      edge[y].push_back(x);
    }
    dfs(stx, 0);
  }
  inline bool valid(int x, int y, int rs = 0) {
    while (dep[x] > dep[y])
      if ((x = fa[x], ++rs) >= 3)
        return false;
    while (dep[x] < dep[y])
      if ((y = fa[y], ++rs) >= 3)
        return false;
    while (x != y)
      if ((y = fa[y], x = fa[x], rs += 2) >= 3)
        return false;
    return true;
  }
} A, B;
inline void dfs(int x) {
  if (A.dep[x] >= B.dep[x])
    return;
  col[x] = 1;
  for (auto v : A.edge[x])
    if (v != A.fa[x])
      dfs(v);
}
int main() {
  n = rd();
  A.stx = rd();
  B.stx = rd();
  if (A.stx == B.stx) {
    puts("0");
    return 0;
  }
  A.init();
  B.init();
  dfs(A.stx);
  for (int i = 1; i <= n; i++)
    for (auto v : A.edge[i])
      if (col[i] && !B.valid(i, v)) {
        puts("-1");
        return 0;
      }
  int ans = 0;
  for (int i = 1; i <= n; i++)
    if (col[i])
      ans = max(ans, B.dep[i]);
  cout << (ans << 1) << '\n';
}