Problem p03058 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define pi std::pair<int, int>
#define mkp std::make_pair
#define ll long long
#define pb push_back
#define fi first
#define se second

//{{{IO
namespace IO {
template <class I> void read(I &x) {
  x = 0;
  char ch = getchar();
  bool f = false;
  for (; !isdigit(ch); ch = getchar())
    f |= (ch == '-');
  for (; isdigit(ch); ch = getchar())
    x = (x << 3) + (x << 1) + ch - '0';
  return;
}

int read_int() {
  int x;
  return read(x), x;
}
ll read_ll() {
  ll x;
  return read(x), x;
}
template <class I> void chmax(I &x, I y) { x = x > y ? x : y; }
template <class I> void chmin(I &x, I y) { x = x < y ? x : y; }
#define ri read_int()
#define rl read_ll()
} // namespace IO
using IO::chmax;
using IO::chmin;
using IO::read_int;
using IO::read_ll;
//}}}

//{{{Mod
namespace Mod {
//	const int mod = 1e9 + 7;
//	const int mod = 1004535809;
const int mod = 998244353;

inline void MOD(int &x) { x -= x >= mod ? mod : 0; }
inline void add(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; }
inline int Add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline void mul(int &x, int y) { x = 1ll * x * y % mod; }
inline int Mul(int x, int y) { return 1ll * x * y % mod; }
} // namespace Mod
//}}}

#define N 2003

int n, m, top;
int st[N], fa[N][12], dep[N], lca[N][N];

std::queue<pi> Q;
std::vector<int> e[N];
std::bitset<N> p[N], s[N], g[N], all;

void dfs(int u, int f) {
  st[++top] = u;
  fa[u][0] = f;
  dep[u] = dep[f] + 1;
  for (int i = 1; i <= 11; ++i)
    fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int i = 1; i <= top; ++i)
    s[st[i]].set(u), p[u].set(st[i]);
  for (auto v : e[u])
    if (v != f)
      dfs(v, u);
  --top;
  return;
}

int get_lca(int x, int y) {
  if (dep[x] < dep[y])
    std::swap(x, y);
  for (int i = 11; ~i; --i)
    if (dep[fa[x][i]] >= dep[y])
      x = fa[x][i];
  if (x == y)
    return x;
  for (int i = 11; ~i; --i)
    if (fa[x][i] != fa[y][i])
      x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}

int jump(int u, int k) {
  for (int i = 11; ~i; --i)
    if (k & (1 << i))
      u = fa[u][i];
  return u;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("puzzle.in", "r", stdin);
  freopen("puzzle.out", "w", stdout);
  freopen("puzzle.log", "w", stderr);
#endif
  n = ri, m = ri;
  for (int i = 2; i <= n; ++i) {
    int u = ri, v = ri;
    e[u].pb(v), e[v].pb(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i)
    all.set(i);
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      lca[i][j] = lca[j][i] = get_lca(i, j);
  for (int i = 1; i <= m; ++i) {
    int u = ri, v = ri;
    g[u].set(v);
    g[v].set(u);
    Q.push(mkp(u, v));
  }
  while (!Q.empty()) {
    pi tmp = Q.front();
    Q.pop();
    int u = tmp.fi, v = tmp.se;
    if (dep[u] > dep[v])
      std::swap(u, v);
    std::bitset<N> x = g[u] ^ g[v], y = (p[u] ^ p[v]) | s[v];
    if (lca[u][v] == u)
      y |= all ^ s[jump(v, dep[v] - dep[u] - 1)];
    else
      y |= s[u];
    y.set(lca[u][v]);
    x &= y;
    for (int p = x._Find_first(); p <= n; p = x._Find_next(p)) {
      if (p == u || p == v)
        continue;
      if (g[u].test(p)) {
        g[v].set(p);
        g[p].set(v);
        ++m;
        Q.push(mkp(v, p));
      } else {
        g[u].set(p);
        g[p].set(u);
        ++m;
        Q.push(mkp(u, p));
      }
    }
  }
  printf("%d\n", m);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define pi std::pair<int, int>
#define mkp std::make_pair
#define ll long long
#define pb push_back
#define fi first
#define se second

//{{{IO
namespace IO {
template <class I> void read(I &x) {
  x = 0;
  char ch = getchar();
  bool f = false;
  for (; !isdigit(ch); ch = getchar())
    f |= (ch == '-');
  for (; isdigit(ch); ch = getchar())
    x = (x << 3) + (x << 1) + ch - '0';
  return;
}

int read_int() {
  int x;
  return read(x), x;
}
ll read_ll() {
  ll x;
  return read(x), x;
}
template <class I> void chmax(I &x, I y) { x = x > y ? x : y; }
template <class I> void chmin(I &x, I y) { x = x < y ? x : y; }
#define ri read_int()
#define rl read_ll()
} // namespace IO
using IO::chmax;
using IO::chmin;
using IO::read_int;
using IO::read_ll;
//}}}

//{{{Mod
namespace Mod {
//	const int mod = 1e9 + 7;
//	const int mod = 1004535809;
const int mod = 998244353;

inline void MOD(int &x) { x -= x >= mod ? mod : 0; }
inline void add(int &x, int y) { x = x + y >= mod ? x + y - mod : x + y; }
inline int Add(int x, int y) { return x + y >= mod ? x + y - mod : x + y; }
inline void mul(int &x, int y) { x = 1ll * x * y % mod; }
inline int Mul(int x, int y) { return 1ll * x * y % mod; }
} // namespace Mod
//}}}

#define N 2003

int n, m, top;
int st[N], fa[N][12], dep[N], lca[N][N];

std::queue<pi> Q;
std::vector<int> e[N];
std::bitset<N> p[N], s[N], g[N], all;

void dfs(int u, int f) {
  st[++top] = u;
  fa[u][0] = f;
  dep[u] = dep[f] + 1;
  for (int i = 1; i <= 11; ++i)
    fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int i = 1; i <= top; ++i)
    s[st[i]].set(u), p[u].set(st[i]);
  for (auto v : e[u])
    if (v != f)
      dfs(v, u);
  --top;
  return;
}

int get_lca(int x, int y) {
  if (dep[x] < dep[y])
    std::swap(x, y);
  for (int i = 11; ~i; --i)
    if (dep[fa[x][i]] >= dep[y])
      x = fa[x][i];
  if (x == y)
    return x;
  for (int i = 11; ~i; --i)
    if (fa[x][i] != fa[y][i])
      x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}

int jump(int u, int k) {
  for (int i = 11; ~i; --i)
    if (k & (1 << i))
      u = fa[u][i];
  return u;
}

int main() {
  n = ri, m = ri;
  for (int i = 2; i <= n; ++i) {
    int u = ri, v = ri;
    e[u].pb(v), e[v].pb(u);
  }
  dfs(1, 0);
  for (int i = 1; i <= n; ++i)
    all.set(i);
  for (int i = 1; i <= n; ++i)
    for (int j = i + 1; j <= n; ++j)
      lca[i][j] = lca[j][i] = get_lca(i, j);
  for (int i = 1; i <= m; ++i) {
    int u = ri, v = ri;
    g[u].set(v);
    g[v].set(u);
    Q.push(mkp(u, v));
  }
  while (!Q.empty()) {
    pi tmp = Q.front();
    Q.pop();
    int u = tmp.fi, v = tmp.se;
    if (dep[u] > dep[v])
      std::swap(u, v);
    std::bitset<N> x = g[u] ^ g[v], y = (p[u] ^ p[v]) | s[v];
    if (lca[u][v] == u)
      y |= all ^ s[jump(v, dep[v] - dep[u] - 1)];
    else
      y |= s[u];
    y.set(lca[u][v]);
    x &= y;
    for (int p = x._Find_first(); p <= n; p = x._Find_next(p)) {
      if (p == u || p == v)
        continue;
      if (g[u].test(p)) {
        g[v].set(p);
        g[p].set(v);
        ++m;
        Q.push(mkp(v, p));
      } else {
        g[u].set(p);
        g[p].set(u);
        ++m;
        Q.push(mkp(u, p));
      }
    }
  }
  printf("%d\n", m);
  return 0;
}