Problem p03335 (C++)

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

using namespace std;

const int N = 2005;

int n, m;
int res;
int sum[N];
int fr[N], to[N];
int h[N], p[11][N];
int color[N * 2];
vector<int> G[N], G2[N];
set<pair<int, int>> S[N];
vector<pair<int, int>> vdel[N];

int lca(int u, int v) {
  if (h[u] > h[v])
    swap(u, v);
  for (int i = 10; i >= 0; --i)
    if (h[p[i][v]] >= h[u])
      v = p[i][v];
  for (int i = 10; i >= 0; --i)
    if (p[i][v] != p[i][u])
      v = p[i][v], u = p[i][u];
  return (u == v) ? u : p[0][u];
}

void dfs(int u) {
  for (int i = 1; i <= 10; ++i) {
    p[i][u] = p[i - 1][p[i - 1][u]];
  }
  for (auto v : G[u])
    if (p[0][u] != v) {
      p[0][v] = u, h[v] = h[u] + 1, dfs(v);
    }
}

void solve(int u) {
  int pmx = 0;
  for (auto v : G[u])
    if (p[0][u] != v) {
      solve(v);
      if (S[v].size() == 1)
        res++;
      if (S[v].size() > 1) {
        res += 2;
        if (!sum[v]) {
          auto p0 = S[v].begin();
          auto p1 = ++S[v].begin();
          G2[p0->second].push_back(p1->second);
          G2[p1->second].push_back(p0->second);
          int x = p0->first;
          int y = p1->first;
          if (h[x] > h[y])
            swap(x, y);
          sum[v]++, sum[y]--;
        }
      }
      sum[u] += sum[v];
      if (!pmx || S[pmx].size() < S[v].size())
        pmx = v;
    }
  if (pmx)
    swap(S[u], S[pmx]);
  for (auto v : G[u])
    if (p[0][u] != v) {
      for (auto i : S[v])
        S[u].insert(i);
    }
  for (auto i : vdel[u])
    S[u].erase(i);
}

void dfscolor(int u) {
  for (auto v : G2[u]) {
    if (color[v]) {
      assert(color[v] == 3 ^ color[u]);
      continue;
    }
    color[v] = 3 ^ color[u], dfscolor(v);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v), G[v].push_back(u);
  }
  p[0][1] = 1, dfs(1);
  for (int i = 0; i < m; ++i) {
    cin >> fr[i] >> to[i];
    if (h[fr[i]] > h[to[i]])
      swap(fr[i], to[i]);
    int tmp = lca(fr[i], to[i]);
    if (tmp == fr[i]) {
      S[to[i]].insert({tmp, i * 2});
      vdel[tmp].push_back({tmp, i * 2});
    } else {
      S[to[i]].insert({tmp, i * 2});
      S[fr[i]].insert({tmp, i * 2 + 1});
      G2[i * 2].push_back(i * 2 + 1);
      G2[i * 2 + 1].push_back(i * 2);
      vdel[tmp].push_back({tmp, i * 2});
      vdel[tmp].push_back({tmp, i * 2 + 1});
    }
  }
  solve(1);
  for (int i = 0; i < m * 2; ++i) {
    if (!color[i])
      color[i] = 1, dfscolor(i);
  }
  cout << res << '\n';
  for (int i = 0; i < m; ++i) {
    if (color[i * 2] == 1)
      cout << fr[i] << ' ' << to[i] << '\n';
    else
      cout << to[i] << ' ' << fr[i] << '\n';
  }
}

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

using namespace std;

const int N = 2005;

int n, m;
int res;
int sum[N];
int fr[N], to[N];
int h[N], p[11][N];
int color[N * 2];
vector<int> G[N], G2[N * 2];
set<pair<int, int>> S[N];
vector<pair<int, int>> vdel[N];

int lca(int u, int v) {
  if (h[u] > h[v])
    swap(u, v);
  for (int i = 10; i >= 0; --i)
    if (h[p[i][v]] >= h[u])
      v = p[i][v];
  for (int i = 10; i >= 0; --i)
    if (p[i][v] != p[i][u])
      v = p[i][v], u = p[i][u];
  return (u == v) ? u : p[0][u];
}

void dfs(int u) {
  for (int i = 1; i <= 10; ++i) {
    p[i][u] = p[i - 1][p[i - 1][u]];
  }
  for (auto v : G[u])
    if (p[0][u] != v) {
      p[0][v] = u, h[v] = h[u] + 1, dfs(v);
    }
}

void solve(int u) {
  int pmx = 0;
  for (auto v : G[u])
    if (p[0][u] != v) {
      solve(v);
      if (S[v].size() == 1)
        res++;
      if (S[v].size() > 1) {
        res += 2;
        if (!sum[v]) {
          auto p0 = S[v].begin();
          auto p1 = ++S[v].begin();
          G2[p0->second].push_back(p1->second);
          G2[p1->second].push_back(p0->second);
          int x = p0->first;
          int y = p1->first;
          if (h[x] > h[y])
            swap(x, y);
          sum[v]++, sum[y]--;
        }
      }
      sum[u] += sum[v];
      if (!pmx || S[pmx].size() < S[v].size())
        pmx = v;
    }
  if (pmx)
    swap(S[u], S[pmx]);
  for (auto v : G[u])
    if (p[0][u] != v) {
      for (auto i : S[v])
        S[u].insert(i);
    }
  for (auto i : vdel[u])
    S[u].erase(i);
}

void dfscolor(int u) {
  for (auto v : G2[u]) {
    if (color[v]) {
      assert(color[v] == 3 ^ color[u]);
      continue;
    }
    color[v] = 3 ^ color[u], dfscolor(v);
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 1; i < n; ++i) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v), G[v].push_back(u);
  }
  p[0][1] = 1, dfs(1);
  for (int i = 0; i < m; ++i) {
    cin >> fr[i] >> to[i];
    if (h[fr[i]] > h[to[i]])
      swap(fr[i], to[i]);
    int tmp = lca(fr[i], to[i]);
    if (tmp == fr[i]) {
      S[to[i]].insert({tmp, i * 2});
      vdel[tmp].push_back({tmp, i * 2});
    } else {
      S[to[i]].insert({tmp, i * 2});
      S[fr[i]].insert({tmp, i * 2 + 1});
      G2[i * 2].push_back(i * 2 + 1);
      G2[i * 2 + 1].push_back(i * 2);
      vdel[tmp].push_back({tmp, i * 2});
      vdel[tmp].push_back({tmp, i * 2 + 1});
    }
  }
  solve(1);
  for (int i = 0; i < m * 2; ++i) {
    if (!color[i])
      color[i] = 1, dfscolor(i);
  }
  cout << res << '\n';
  for (int i = 0; i < m; ++i) {
    if (color[i * 2] == 1)
      cout << fr[i] << ' ' << to[i] << '\n';
    else
      cout << to[i] << ' ' << fr[i] << '\n';
  }
}