Problem p03335 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
// #include <cstdlib>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
typedef double db;
const int oo = 2139063143;
const int N = 2010;
const int P = 1000000007;
const db eps = 1e-7;
#define pritnf printf
// char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
// #define getchar()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T> inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T> inline void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void pr(T x) { print(x), putchar('\n'); }
template <typename T1, typename T2> void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2> void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}
int n, m;
struct LY {
  int x, y, nx;
  bool op;
} a[N];
int k = 0;
#define pb push_back
#define pii pair<int, int>
#define se second
#define fi first
#define mp make_pair
vector<pii> G[N];
set<int> s[N];
int deg[N];
int st[N], z = 0;
void del(int i) { s[a[i].x].erase(i), s[a[i].y].erase(i); }
void add(int i) { s[a[i].x].insert(i), s[a[i].y].insert(i); }
void solve() {
  queue<int> q;
  for (int i = 1; i <= n; i++)
    if (deg[i] == 1)
      q.push(i);
  while (q.size()) {
    int u = q.front();
    q.pop();
    z = 0;
    bool fg = 0;
    for (set<int>::iterator it = s[u].begin(); it != s[u].end(); it++) {
      int i = *it;
      if (a[i].x == a[i].y) {
        a[i].op = 1, fg ^= 1;
        if (fg)
          swap(a[i].x, a[i].y);
        continue;
      }
      st[++z] = i;
    }
    while (z >= 2) {
      int i = st[z], j = st[z - 1];
      del(i), del(j);
      if (a[i].x == u)
        swap(a[i].x, a[i].y);
      if (a[j].x == u)
        swap(a[j].x, a[j].y);
      if (a[i].x == a[j].x && a[i].y == a[j].y) {
        swap(a[j].x, a[j].y), a[i].op = a[j].op = 1;
        z -= 2;
        continue;
      }
      a[++k].x = a[i].x, a[k].y = a[j].x, add(k);
      a[i].nx = a[j].nx = k;
      z -= 2;
    }
    if (z) {
      for (pii x : G[u])
        if (deg[x.fi] - 1 >= 1) {
          int v = x.fi, i = st[z--];
          del(i);
          a[++k] = a[i];
          a[i].nx = k;
          ((a[i].x == u) ? a[k].x : a[k].y) = v;
          add(k);
        }
    }
    for (pii x : G[u])
      if (--deg[x.fi] == 1)
        q.push(x.fi);
  }
}
int ans[N];
bool fg = 0;
void dfs(int u, int fa, int aim) {
  if (u == aim) {
    while (z)
      ++ans[st[z--]];
    fg = 1;
  }
  if (fg)
    return;
  for (pii x : G[u])
    if (x.fi != fa)
      st[++z] = x.se, dfs(x.fi, u, aim), --z;
}
int main() {
  // freopen (".in","r",stdin);
  // freopen (".out","w",stdout);
  sc(n), sc(m);
  for (int i = 1; i < n; i++) {
    int x, y;
    sc(x), sc(y);
    G[x].pb(mp(y, i)), G[y].pb(mp(x, i));
    ++deg[x], ++deg[y];
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    sc(x), sc(y);
    a[++k].x = x, a[k].y = y;
    add(i), fg = 0, z = 0, dfs(x, 0, y);
  }
  solve();
  for (int i = k; i >= 1; i--)
    if (!a[i].op) {
      int j = a[i].nx;
      if ((a[i].x == a[j].x) + (a[i].y == a[j].y) == 0)
        swap(a[i].x, a[i].y);
    }
  for (int i = 1; i < n; i++)
    ans[0] += min(ans[i], 2);
  pr(ans[0]);
  for (int i = 1; i <= m; i++)
    printf("%d %d\n", a[i].x, a[i].y);

  return 0;
}


Fixed submission:
#include <algorithm>
// #include <cstdlib>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <string>
using namespace std;
typedef long long LL;
typedef double db;
const int oo = 2139063143;
const int N = 10010;
const int P = 1000000007;
const db eps = 1e-7;
#define pritnf printf
// char buf[1<<22],*p1=buf,*p2=buf,obuf[1<<22],*O=obuf;
// #define getchar()
// (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
template <typename T> inline void sc(T &x) {
  x = 0;
  static int p;
  p = 1;
  static char c;
  c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      p = -1;
    c = getchar();
  }
  while (isdigit(c)) {
    x = (x << 1) + (x << 3) + (c - 48);
    c = getchar();
  }
  x *= p;
}
template <typename T> inline void print(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    print(x / 10);
  putchar(x % 10 + '0');
}
template <typename T> inline void pr(T x) { print(x), putchar('\n'); }
template <typename T1, typename T2> void chkmax(T1 &A, T2 B) {
  A = A >= B ? A : B;
}
template <typename T1, typename T2> void chkmin(T1 &A, T2 B) {
  A = A <= B ? A : B;
}
int n, m;
struct LY {
  int x, y, nx;
  bool op;
} a[N];
int k = 0;
#define pb push_back
#define pii pair<int, int>
#define se second
#define fi first
#define mp make_pair
vector<pii> G[N];
set<int> s[N];
int deg[N];
int st[N], z = 0;
void del(int i) { s[a[i].x].erase(i), s[a[i].y].erase(i); }
void add(int i) { s[a[i].x].insert(i), s[a[i].y].insert(i); }
void solve() {
  queue<int> q;
  for (int i = 1; i <= n; i++)
    if (deg[i] == 1)
      q.push(i);
  while (q.size()) {
    int u = q.front();
    q.pop();
    z = 0;
    bool fg = 0;
    for (set<int>::iterator it = s[u].begin(); it != s[u].end(); it++) {
      int i = *it;
      if (a[i].x == a[i].y) {
        a[i].op = 1, fg ^= 1;
        if (fg)
          swap(a[i].x, a[i].y);
        continue;
      }
      st[++z] = i;
    }
    while (z >= 2) {
      int i = st[z], j = st[z - 1];
      del(i), del(j);
      if (a[i].x == u)
        swap(a[i].x, a[i].y);
      if (a[j].x == u)
        swap(a[j].x, a[j].y);
      if (a[i].x == a[j].x && a[i].y == a[j].y) {
        swap(a[j].x, a[j].y), a[i].op = a[j].op = 1;
        z -= 2;
        continue;
      }
      a[++k].x = a[i].x, a[k].y = a[j].x, add(k);
      a[i].nx = a[j].nx = k;
      z -= 2;
    }
    if (z) {
      for (pii x : G[u])
        if (deg[x.fi] - 1 >= 1) {
          int v = x.fi, i = st[z--];
          del(i);
          a[++k] = a[i];
          a[i].nx = k;
          ((a[i].x == u) ? a[k].x : a[k].y) = v;
          add(k);
        }
    }
    for (pii x : G[u])
      if (--deg[x.fi] == 1)
        q.push(x.fi);
  }
}
int ans[N];
bool fg = 0;
void dfs(int u, int fa, int aim) {
  if (u == aim) {
    while (z)
      ++ans[st[z--]];
    fg = 1;
  }
  if (fg)
    return;
  for (pii x : G[u])
    if (x.fi != fa)
      st[++z] = x.se, dfs(x.fi, u, aim), --z;
}
int main() {
  // freopen (".in","r",stdin);
  // freopen (".out","w",stdout);
  sc(n), sc(m);
  for (int i = 1; i < n; i++) {
    int x, y;
    sc(x), sc(y);
    G[x].pb(mp(y, i)), G[y].pb(mp(x, i));
    ++deg[x], ++deg[y];
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    sc(x), sc(y);
    a[++k].x = x, a[k].y = y;
    add(i), fg = 0, z = 0, dfs(x, 0, y);
  }
  solve();
  for (int i = k; i >= 1; i--)
    if (!a[i].op) {
      int j = a[i].nx;
      if ((a[i].x == a[j].x) + (a[i].y == a[j].y) == 0)
        swap(a[i].x, a[i].y);
    }
  for (int i = 1; i < n; i++)
    ans[0] += min(ans[i], 2);
  pr(ans[0]);
  for (int i = 1; i <= m; i++)
    printf("%d %d\n", a[i].x, a[i].y);

  return 0;
}