Problem p03058 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ri register int
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
// getchar
#define gc()                                                                   \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                 \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
// print the remaining part
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
// putchar
inline void putc(char x) {
  *oS++ = x;
  if (oS == oT)
    flush();
}
// input a signed integer
template <class I> inline void gi(I &x) {
  for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
    if (c == '-')
      f = -1;
  for (x = 0; c <= '9' && c >= '0'; c = gc())
    x = (x << 1) + (x << 3) + (c & 15);
  x *= f;
}
// print a signed integer
template <class I> inline void print(I x) {
  if (!x)
    putc('0');
  if (x < 0)
    putc('-'), x = -x;
  while (x)
    qu[++qr] = x % 10 + '0', x /= 10;
  while (qr)
    putc(qu[qr--]);
}
// no need to call flush at the end manually!
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::gi;
using io ::print;
using io ::putc;
const int N = 2e3 + 5;
#define bit bitset<N>
struct node {
  int tot, head[N], nxt[N << 1], adj[N << 1], top[N], sz[N], f[N], dep[N],
      son[N];
  bit p[N], q[N];
  inline void addedge(ri x, ri y) {
    nxt[++tot] = head[x];
    adj[head[x] = tot] = y;
  }
  inline void dfs(ri x) {
    sz[x] = 1;
    p[x][x] = q[x][x] = 1;
    for (ri y, i = head[x]; i; i = nxt[i])
      if ((y = adj[i]) != f[x])
        p[y] = p[x], f[y] = x, dep[y] = dep[x] + 1, dfs(y), q[x] |= q[y],
        sz[x] += sz[y], son[x] = sz[y] > sz[son[x]] ? y : son[x];
  }
  inline void dfs2(ri x, ri tp) {
    top[x] = tp;
    if (son[x])
      dfs2(son[x], tp);
    for (ri y, i = head[x]; i; i = nxt[i])
      if ((y = adj[i]) != f[x] && y != son[x])
        dfs2(y, y);
  }
  inline int lca(ri x, ri y) {
    while (top[x] ^ top[y])
      if (dep[top[x]] > dep[top[y]])
        x = f[top[x]];
      else
        y = f[top[y]];
    return dep[x] < dep[y] ? x : y;
  }
  inline int sec(ri x, ri y) { // lca(x,y) = x
    while (top[x] ^ top[y]) {
      if (f[top[y]] == x)
        return top[y];
      y = f[top[y]];
    }
    return son[x];
  }
  inline bit S(ri a, ri b) {
    ri x = lca(a, b);
    bit ans = p[a] ^ p[b];
    ans[x] = 1;
    if (x != b)
      ans |= q[b];
    else
      ans |= ~q[sec(b, a)];
    if (x != a)
      ans |= q[a];
    else
      ans |= ~q[sec(a, b)];
    return ans;
  }
} t;
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
pii qu[N * N];
bit nG[N];
int main() {
  ri n, m, i, x, y, l, r;
  l = 1;
  r = 0;
  gi(n);
  gi(m);
  for (i = 1; i ^ n; ++i)
    gi(x), gi(y), t.addedge(x, y), t.addedge(y, x);
  t.dfs(1);
  t.dfs2(1, 1);
  while (m--)
    gi(x), gi(y), nG[x][y] = nG[y][x] = 1, qu[++r] = mp(x, y);
  while (l <= r) {
    x = qu[l].fi;
    y = qu[l].se;
    ++l;
    bit now = (nG[x] ^ nG[y]) & t.S(x, y);
    now[x] = now[y] = 0;
    for (i = 1; i <= n; ++i)
      if (now[i])
        if (!nG[x][i])
          nG[x][i] = nG[i][x] = 1, qu[++r] = mp(x, i);
        else
          nG[y][i] = nG[i][y] = 1, qu[++r] = mp(y, i);
  }
  printf("%d\n", r);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ri register int
namespace io {
const int SIZE = (1 << 21) + 1;
char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c,
                                       qu[55];
int f, qr;
// getchar
#define gc()                                                                   \
  (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin),                 \
               (iS == iT ? EOF : *iS++))                                       \
            : *iS++)
// print the remaining part
inline void flush() {
  fwrite(obuf, 1, oS - obuf, stdout);
  oS = obuf;
}
// putchar
inline void putc(char x) {
  *oS++ = x;
  if (oS == oT)
    flush();
}
// input a signed integer
template <class I> inline void gi(I &x) {
  for (f = 1, c = gc(); c < '0' || c > '9'; c = gc())
    if (c == '-')
      f = -1;
  for (x = 0; c <= '9' && c >= '0'; c = gc())
    x = (x << 1) + (x << 3) + (c & 15);
  x *= f;
}
// print a signed integer
template <class I> inline void print(I x) {
  if (!x)
    putc('0');
  if (x < 0)
    putc('-'), x = -x;
  while (x)
    qu[++qr] = x % 10 + '0', x /= 10;
  while (qr)
    putc(qu[qr--]);
}
// no need to call flush at the end manually!
struct Flusher_ {
  ~Flusher_() { flush(); }
} io_flusher_;
} // namespace io
using io ::gi;
using io ::print;
using io ::putc;
const int N = 2e3 + 5;
#define bit bitset<N>
struct node {
  int tot, head[N], nxt[N << 1], adj[N << 1], top[N], sz[N], f[N], dep[N],
      son[N];
  bit p[N], q[N];
  inline void addedge(ri x, ri y) {
    nxt[++tot] = head[x];
    adj[head[x] = tot] = y;
  }
  inline void dfs(ri x) {
    sz[x] = 1;
    p[x][x] = q[x][x] = 1;
    for (ri y, i = head[x]; i; i = nxt[i])
      if ((y = adj[i]) != f[x])
        p[y] = p[x], f[y] = x, dep[y] = dep[x] + 1, dfs(y), q[x] |= q[y],
        sz[x] += sz[y], son[x] = sz[y] > sz[son[x]] ? y : son[x];
  }
  inline void dfs2(ri x, ri tp) {
    top[x] = tp;
    if (son[x])
      dfs2(son[x], tp);
    for (ri y, i = head[x]; i; i = nxt[i])
      if ((y = adj[i]) != f[x] && y != son[x])
        dfs2(y, y);
  }
  inline int lca(ri x, ri y) {
    while (top[x] ^ top[y])
      if (dep[top[x]] > dep[top[y]])
        x = f[top[x]];
      else
        y = f[top[y]];
    return dep[x] < dep[y] ? x : y;
  }
  inline int sec(ri x, ri y) { // lca(x,y) = x
    while (top[x] ^ top[y]) {
      if (f[top[y]] == x)
        return top[y];
      y = f[top[y]];
    }
    return son[x];
  }
  inline bit S(ri a, ri b) {
    ri x = lca(a, b);
    bit ans = p[a] ^ p[b];
    ans[x] = 1;
    if (x != b)
      ans |= q[b];
    else
      ans |= ~q[sec(b, a)];
    if (x != a)
      ans |= q[a];
    else
      ans |= ~q[sec(a, b)];
    return ans;
  }
} t;
#define pii pair<int, int>
#define fi first
#define se second
#define mp make_pair
pii qu[N * N];
bit nG[N];
int main() {
  ri n, m, i, x, y, l, r;
  l = 1;
  r = 0;
  gi(n);
  gi(m);
  for (i = 1; i ^ n; ++i)
    gi(x), gi(y), t.addedge(x, y), t.addedge(y, x);
  t.dfs(1);
  t.dfs2(1, 1);
  while (m--)
    gi(x), gi(y), nG[x][y] = nG[y][x] = 1, qu[++r] = mp(x, y);
  while (l <= r) {
    x = qu[l].fi;
    y = qu[l].se;
    ++l;
    bit now = (nG[x] ^ nG[y]) & t.S(x, y);
    now[x] = now[y] = 0;
    for (i = now._Find_first(); i != now.size(); i = now._Find_next(i))
      if (!nG[x][i])
        nG[x][i] = nG[i][x] = 1, qu[++r] = mp(x, i);
      else
        nG[y][i] = nG[i][y] = 1, qu[++r] = mp(y, i);
  }
  printf("%d\n", r);
  return 0;
}