Problem p03058 (C++)

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

const int N = 2005;

struct edge {
  int to, nxt;
} e[N << 1];
int n, m, head[N], tot;

void link(int x, int y) {
  e[++tot] = (edge){y, head[x]}, head[x] = tot;
  e[++tot] = (edge){x, head[y]}, head[y] = tot;
}

typedef std::bitset<N> bitset;

int f[N], next[N][N], lca[N][N];

bitset path[N], sub[N];

void dfs(int x) {
  path[x].set(x), sub[x].set(x);
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != f[x]) {
      path[e[i].to] = path[x], f[e[i].to] = x, dfs(e[i].to);
      for (int j = sub[e[i].to]._Find_first(); j < N;
           j = sub[e[i].to]._Find_next(j)) {
        next[i][j] = e[i].to;
        for (int k = sub[x]._Find_first(); k < N; k = sub[x]._Find_next(k))
          lca[j][k] = lca[k][j] = x;
      }
      sub[x] |= sub[e[i].to];
    }
}

bitset near[N], U;
std::queue<std::pair<int, int>> q;

int edge_num = 0;

void _link(int x, int y) {
  near[x].set(y), near[y].set(x), q.emplace(x, y), ++edge_num;
}

inline bitset get_path(int x, int y) {
  bitset ret = path[x] ^ path[y];
  ret.set(lca[x][y]);
  return ret;
}
inline bitset subtree(int x, int y) {
  if (sub[x].test(y))
    std::swap(x, y);
  if (sub[y].test(x))
    return sub[x] | U ^ sub[next[y][x]];
  else
    return sub[x] | sub[y];
}
inline bitset get(int x, int y) { return subtree(x, y) | get_path(x, y); }

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    U.set(i), near[i].set(i);
  for (int i = 1, x, y; i < n; ++i)
    std::cin >> x >> y, link(x, y);
  dfs(1);
  for (int i = 0, x, y; i < m; ++i)
    std::cin >> x >> y, _link(x, y);
  while (q.size()) {
    auto tmp = q.front();
    q.pop();
    bitset t =
        (near[tmp.first] ^ near[tmp.second]) & get(tmp.first, tmp.second);
    for (int x = t._Find_first(); x < N; x = t._Find_next(x)) {
      int v = near[tmp.first].test(x) ? tmp.second : tmp.first;
      _link(x, v);
    }
  }
  std::cout << edge_num << '\n';
  return 0;
}

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

const int N = 2005;

struct edge {
  int to, nxt;
} e[N << 1];
int n, m, head[N], tot;

void link(int x, int y) {
  e[++tot] = (edge){y, head[x]}, head[x] = tot;
  e[++tot] = (edge){x, head[y]}, head[y] = tot;
}

typedef std::bitset<N> bitset;

int f[N], next[N][N], lca[N][N];

bitset path[N], sub[N];

void dfs(int x) {
  path[x].set(x), sub[x].set(x);
  for (int i = head[x]; i; i = e[i].nxt)
    if (e[i].to != f[x]) {
      path[e[i].to] = path[x], f[e[i].to] = x, dfs(e[i].to);
      for (int j = sub[e[i].to]._Find_first(); j < N;
           j = sub[e[i].to]._Find_next(j)) {
        next[x][j] = e[i].to;
        for (int k = sub[x]._Find_first(); k < N; k = sub[x]._Find_next(k))
          lca[j][k] = lca[k][j] = x;
      }
      sub[x] |= sub[e[i].to];
    }
}

bitset near[N], U;
std::queue<std::pair<int, int>> q;

int edge_num = 0;

void _link(int x, int y) {
  near[x].set(y), near[y].set(x), q.emplace(x, y), ++edge_num;
}

inline bitset get_path(int x, int y) {
  bitset ret = path[x] ^ path[y];
  ret.set(lca[x][y]);
  return ret;
}
inline bitset subtree(int x, int y) {
  if (sub[x].test(y))
    std::swap(x, y);
  if (sub[y].test(x))
    return sub[x] | U ^ sub[next[y][x]];
  else
    return sub[x] | sub[y];
}
inline bitset get(int x, int y) { return subtree(x, y) | get_path(x, y); }

int main() {
  std::ios::sync_with_stdio(0), std::cin.tie(0);
  std::cin >> n >> m;
  for (int i = 1; i <= n; ++i)
    U.set(i), near[i].set(i);
  for (int i = 1, x, y; i < n; ++i)
    std::cin >> x >> y, link(x, y);
  dfs(1);
  for (int i = 0, x, y; i < m; ++i)
    std::cin >> x >> y, _link(x, y);
  while (q.size()) {
    auto tmp = q.front();
    q.pop();
    bitset t =
        (near[tmp.first] ^ near[tmp.second]) & get(tmp.first, tmp.second);
    for (int x = t._Find_first(); x < N; x = t._Find_next(x)) {
      int v = near[tmp.first].test(x) ? tmp.second : tmp.first;
      _link(x, v);
    }
  }
  std::cout << edge_num << '\n';
  return 0;
}