Problem p03335 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

namespace zyt {
template <typename T> inline bool read(T &x) {
  char c;
  bool f = false;
  x = 0;
  do
    c = getchar();
  while (c != EOF && c != '-' && !isdigit(c));
  if (c == EOF)
    return false;
  if (c == '-')
    f = true, c = getchar();
  do
    x = x * 10 + c - '0', c = getchar();
  while (isdigit(c));
  if (f)
    x = -x;
  return true;
}
template <typename T> inline void write(T x) {
  static char buf[20];
  char *pos = buf;
  if (x < 0)
    putchar('-'), x = -x;
  do
    *pos++ = x % 10 + '0';
  while (x /= 10);
  while (pos > buf)
    putchar(*--pos);
}
typedef pair<int, int> pii;
const int N = 2e3 + 10;
vector<int> v[N], g[N];
int dep[N], n, m, fa[N], deg[N], p[N << 1], f[N << 1];
pii arr[N << 1], bck[N << 1];
bool tag[N << 1];
queue<int> q;
int find(const int u) { return !p[u] ? u : p[u] = find(p[u]); }
void dfs(const int u, const int f) {
  dep[u] = dep[f] + 1;
  fa[u] = f;
  for (auto v : g[u]) {
    if (v == f)
      continue;
    ++deg[u];
    dfs(v, u);
  }
}
namespace LCA {
const int B = 15;
int fa[B][N];
void dfs(const int u, const int f) {
  fa[0][u] = f;
  for (int i = 1; i < B; i++)
    fa[i][u] = fa[i - 1][fa[i - 1][u]];
  for (auto v : g[u]) {
    if (v == f)
      continue;
    dfs(v, u);
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b])
    swap(a, b);
  for (int i = B - 1; i >= 0; i--)
    if (dep[fa[i][a]] >= dep[b])
      a = fa[i][a];
  if (a == b)
    return a;
  for (int i = B - 1; i >= 0; i--)
    if (fa[i][a] != fa[i][b])
      a = fa[i][a], b = fa[i][b];
  return fa[0][a];
}
int dis(const int a, const int b) {
  return dep[a] + dep[b] - dep[lca(a, b)] * 2;
}
} // namespace LCA
bool check(const int u, const int a, const int b) {
  using LCA::dis;
  return dis(a, u) + dis(b, u) == dis(a, b);
}
int work() {
  read(n), read(m);
  for (int i = 1; i < n; i++) {
    int u, v;
    read(u), read(v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  LCA::dfs(1, 0);
  for (int i = 1; i <= n; i++)
    if (!deg[i])
      q.push(i);
  for (int i = 1; i <= m; i++) {
    read(arr[i].first), read(arr[i].second);
    bck[i] = arr[i];
    int a = arr[i].first, b = arr[i].second;
    if (dep[a] < dep[b])
      swap(a, b);
    while (dep[a] > dep[b])
      v[a].push_back(i), a = fa[a];
    while (a != b) {
      v[a].push_back(i), v[b].push_back(i);
      a = fa[a], b = fa[b];
    }
  }
  int cnt = m, ans = 0;
  for (int i = 2; i <= n; i++)
    ans += min(2, int(v[i].size()));
  while (q.size() && q.front() != 1) {
    static bool tg[N << 1];
    int u = q.front();
    q.pop();
    if (!fa[u])
      break;
    static int num[N];
    vector<int> tmp;
    for (auto i : v[u])
      ++num[find(i)];
    for (auto i : v[u])
      if (!--num[find(i)] && arr[find(i)].first != arr[find(i)].second &&
          check(u, arr[find(i)].first, arr[find(i)].second)) {
        tmp.push_back(find(i));
        if (arr[find(i)].first != u)
          swap(arr[find(i)].first, arr[find(i)].second), tg[find(i)] = 1;
      }
    if (tmp.size() == 1)
      arr[tmp[0]].first = fa[u];
    else if (tmp.size() > 1) {
      p[tmp[0]] = p[tmp[1]] = f[tmp[0]] = f[tmp[1]] = ++cnt;
      if (!tg[tmp[0]])
        tag[tmp[0]] = 1;
      if (tg[tmp[1]])
        tag[tmp[1]] = 1;
      arr[cnt] = pii(arr[tmp[0]].second, arr[tmp[1]].second);
      for (int i = 2; i < tmp.size(); i++)
        arr[tmp[i]].first = fa[u];
    }
    for (auto i : tmp)
      if (tg[i])
        swap(arr[i].first, arr[i].second), tg[i] = false;
    if (!--deg[fa[u]])
      q.push(fa[u]);
  }
  for (int i = cnt; i > 0; i--)
    tag[i] ^= tag[f[i]];
  write(ans), putchar('\n');
  for (int i = 1; i <= m; i++)
    if (tag[i])
      printf("%d %d\n", bck[i].second, bck[i].first);
    else
      printf("%d %d\n", bck[i].first, bck[i].second);
  return 0;
}
} // namespace zyt
int main() { return zyt::work(); }

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

namespace zyt {
template <typename T> inline bool read(T &x) {
  char c;
  bool f = false;
  x = 0;
  do
    c = getchar();
  while (c != EOF && c != '-' && !isdigit(c));
  if (c == EOF)
    return false;
  if (c == '-')
    f = true, c = getchar();
  do
    x = x * 10 + c - '0', c = getchar();
  while (isdigit(c));
  if (f)
    x = -x;
  return true;
}
template <typename T> inline void write(T x) {
  static char buf[20];
  char *pos = buf;
  if (x < 0)
    putchar('-'), x = -x;
  do
    *pos++ = x % 10 + '0';
  while (x /= 10);
  while (pos > buf)
    putchar(*--pos);
}
typedef pair<int, int> pii;
const int N = 2e3 + 10;
vector<int> v[N], g[N];
int dep[N], n, m, fa[N], deg[N], p[N << 1], f[N << 1];
pii arr[N << 1], bck[N << 1];
bool tag[N << 1];
queue<int> q;
int find(const int u) { return !p[u] ? u : p[u] = find(p[u]); }
void dfs(const int u, const int f) {
  dep[u] = dep[f] + 1;
  fa[u] = f;
  for (auto v : g[u]) {
    if (v == f)
      continue;
    ++deg[u];
    dfs(v, u);
  }
}
namespace LCA {
const int B = 15;
int fa[B][N];
void dfs(const int u, const int f) {
  fa[0][u] = f;
  for (int i = 1; i < B; i++)
    fa[i][u] = fa[i - 1][fa[i - 1][u]];
  for (auto v : g[u]) {
    if (v == f)
      continue;
    dfs(v, u);
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b])
    swap(a, b);
  for (int i = B - 1; i >= 0; i--)
    if (dep[fa[i][a]] >= dep[b])
      a = fa[i][a];
  if (a == b)
    return a;
  for (int i = B - 1; i >= 0; i--)
    if (fa[i][a] != fa[i][b])
      a = fa[i][a], b = fa[i][b];
  return fa[0][a];
}
int dis(const int a, const int b) {
  return dep[a] + dep[b] - dep[lca(a, b)] * 2;
}
} // namespace LCA
bool check(const int u, const int a, const int b) {
  using LCA::dis;
  return dis(a, u) + dis(b, u) == dis(a, b);
}
int work() {
  read(n), read(m);
  for (int i = 1; i < n; i++) {
    int u, v;
    read(u), read(v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(1, 0);
  LCA::dfs(1, 0);
  for (int i = 1; i <= n; i++)
    if (!deg[i])
      q.push(i);
  for (int i = 1; i <= m; i++) {
    read(arr[i].first), read(arr[i].second);
    bck[i] = arr[i];
    int a = arr[i].first, b = arr[i].second;
    if (dep[a] < dep[b])
      swap(a, b);
    while (dep[a] > dep[b])
      v[a].push_back(i), a = fa[a];
    while (a != b) {
      v[a].push_back(i), v[b].push_back(i);
      a = fa[a], b = fa[b];
    }
  }
  int cnt = m, ans = 0;
  for (int i = 2; i <= n; i++)
    ans += min(2, int(v[i].size()));
  while (q.size() && q.front() != 1) {
    static bool tg[N << 1];
    int u = q.front();
    q.pop();
    if (!fa[u])
      break;
    static int num[N << 1];
    vector<int> tmp;
    for (auto i : v[u])
      ++num[find(i)];
    for (auto i : v[u])
      if (!--num[find(i)] && arr[find(i)].first != arr[find(i)].second &&
          check(u, arr[find(i)].first, arr[find(i)].second)) {
        tmp.push_back(find(i));
        if (arr[find(i)].first != u)
          swap(arr[find(i)].first, arr[find(i)].second), tg[find(i)] = 1;
      }
    if (tmp.size() == 1)
      arr[tmp[0]].first = fa[u];
    else if (tmp.size() > 1) {
      p[tmp[0]] = p[tmp[1]] = f[tmp[0]] = f[tmp[1]] = ++cnt;
      if (!tg[tmp[0]])
        tag[tmp[0]] = 1;
      if (tg[tmp[1]])
        tag[tmp[1]] = 1;
      arr[cnt] = pii(arr[tmp[0]].second, arr[tmp[1]].second);
      for (int i = 2; i < tmp.size(); i++)
        arr[tmp[i]].first = fa[u];
    }
    for (auto i : tmp)
      if (tg[i])
        swap(arr[i].first, arr[i].second), tg[i] = false;
    if (!--deg[fa[u]])
      q.push(fa[u]);
  }
  for (int i = cnt; i > 0; i--)
    tag[i] ^= tag[f[i]];
  write(ans), putchar('\n');
  for (int i = 1; i <= m; i++)
    if (tag[i])
      printf("%d %d\n", bck[i].second, bck[i].first);
    else
      printf("%d %d\n", bck[i].first, bck[i].second);
  return 0;
}
} // namespace zyt
int main() { return zyt::work(); }