Problem p03335 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i, x, y) for (int i = int(x); i < int(y); ++i)
#define For(i, x, y) for (int i = int(x); i <= int(y); ++i)
#define FOr(i, x, y) for (int i = int(x); i >= int(y); --i)
#define pi acos(-1)
#define mk make_pair
#define pa pair<ll, ll>
#define lf else if
#define IL inline
#define max(x, y) ((x) < (y) ? (y) : (x))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define sqr(x) ((x) * (x))
#define Mul(x, y) ((x) = 1LL * (x) * (y) % mod)
#define Add(x, y) ((x) = ((x) + (y)) % mod)
#define Max(x, y) ((x) = ((x) < (y) ? (y) : (x)))
#define Min(x, y) ((x) = ((x) > (y) ? (y) : (x)))
#define E(x) return writeln(x), 0
#define LL (long long)
#define p(x) printf("~%lld~\n", LL(x))
#define pp(x, y) printf("~~%lld %lld~~\n", LL(x), LL(y))
#define ppp(x, y, z) printf("~~~%lld %lld %lld~~~\n", LL(x), LL(y), LL(z))
#define pppp(a, b, c, d)                                                       \
  printf("~~~%lld %lld %lld %lld\n", LL(a), LL(b), LL(c), LL(d))
#define f_in(x) freopen(x ".in", "r", stdin)
#define f_out(x) freopen(x ".out", "w", stdout)
#define open(x) f_in(x), f_out(x)
#define fi first
#define se second
#define GuYue                                                                  \
  puts("\nGuYueNa____________________________________________________________" \
       "____________________")
typedef complex<double> E;
namespace SHENZHEBEI {
#ifdef LOCAL
struct _ {
  _() { freopen("cf.in", "r", stdin); }
} _;
#endif
#define NEG 1
static const int GYN = 2333333;
char SZB[GYN], *SS = SZB, *TT = SZB;
inline char gc() {
  if (SS == TT) {
    TT = (SS = SZB) + fread(SZB, 1, GYN, stdin);
    if (SS == TT)
      return '\n';
  }
  return *SS++;
}
#if NEG
inline ll read() {
  ll x = 0, g = 1;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      g = -1;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x * g;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#else
inline ll read() {
  ll x = 0;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    ;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x;
}
inline void write(ll x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#endif
inline char readchar() {
  char ch = gc();
  for (; isspace(ch); ch = gc())
    ;
  return ch;
}
inline ll readstr(char *s) {
  char ch = gc();
  int cur = 0;
  for (; isspace(ch); ch = gc())
    ;
  for (; !isspace(ch); ch = gc())
    s[cur++] = ch;
  s[cur] = '\0';
  return cur;
}
void Print(long long *a, int s, int t) { For(i, s, t) printf("%lld ", a[i]); }
void Print(int *a, int s, int t) { For(i, s, t) printf("%d ", a[i]); }
void Print(char *a, int s, int t) { For(i, s, t) putchar(a[i]); }
void writeln(ll x) {
  write(x);
  puts("");
}
} // namespace SHENZHEBEI
using namespace SHENZHEBEI;
const ll N = 400000;
vector<ll> g[N], h[N], I[N];
set<ll> gg[N];
ll col[N], vist[N], dep[N], fa[N], IdDel[N], LineDel[N], a[N], b[N], A[N], B[N],
    ans, n, m;
ll Lca(ll x, ll y) {
  for (; x != y; dep[x] < dep[y] ? y = fa[y] : x = fa[x])
    ;
  return x;
}
void Dfs(ll x) {
  rep(i, 0, g[x].size()) {
    ll to = g[x][i];
    if (fa[x] == to)
      continue;
    fa[to] = x;
    dep[to] = dep[x] + 1;
    Dfs(to);
  }
}
void Delete(ll x, ll y) {
  for (; x != y;) {
    if (dep[x] < dep[y])
      swap(x, y);
    if (!IdDel[x])
      IdDel[x] = 1, ans += 2;
    x = fa[x];
  }
}
void Link(ll x, ll y, ll w) {
  h[x].push_back(y);
  I[x].push_back(w);
}
void dfs(ll x) {
  FOr(i, g[x].size() - 1, 0) {
    ll to = g[x][i];
    if (to == fa[x])
      continue;
    dfs(to);
  }
  if (x == 1)
    return;
  for (set<ll>::iterator it = gg[x].begin(), it1; it != gg[x].end(); it = it1) {
    ll id = *it;
    it1 = ++it;
    if (LineDel[id] || a[id] == b[id])
      gg[x].erase(it);
  }

  if (!IdDel[x]) {
    if (gg[x].size() == 1) {
      ++ans;
      ll id = *gg[x].begin();
      if (a[id] == x)
        a[id] = fa[a[id]];
      lf(b[id] == x) b[id] = fa[b[id]];
      gg[fa[x]].insert(id);
      IdDel[x] = 1;
    }
    lf(gg[x].size() > 1) {
      set<ll>::iterator it = gg[x].begin();
      ll id1 = *it, id2 = *++it, tp1 = a[id1] == x, tp2 = a[id2] == x,
         to1 = tp1 ? b[id1] : a[id1], to2 = tp2 ? b[id2] : a[id2],
         Down = Lca(x, to1) ^ Lca(x, to2) ^ Lca(to1, to2); // 比较深的那个
      Delete(x, Down);
      Link(id1, id2, tp1 == tp2);
      Link(id2, id1, tp1 == tp2);
      gg[a[id1]].erase(id1);
      gg[b[id1]].erase(id1);
      LineDel[id1] = 1;
      if (tp2) {
        gg[a[id2]].erase(id2);
        a[id2] = tp1 ? b[id1] : a[id1];
        gg[a[id2]].insert(id2);
      } else {
        gg[b[id2]].erase(id2);
        b[id2] = tp1 ? b[id1] : a[id1];
        gg[b[id2]].insert(id2);
      }

      for (it = gg[x].begin(); it != gg[x].end(); ++it) {
        ll id = *it;
        if (a[id] == x)
          a[id] = fa[x];
        else
          b[id] = fa[x];
        gg[fa[x]].insert(id);
      }
    }
  } else {
    for (set<ll>::iterator it = gg[x].begin(); it != gg[x].end(); ++it) {
      ll id = *it;
      if (a[id] == x)
        a[id] = fa[x];
      else
        b[id] = fa[x];
      gg[fa[x]].insert(id);
    }
  }
}
void fafa(ll x, ll Col) {
  vist[x] = 1;
  col[x] = Col;
  rep(i, 0, h[x].size()) if (!vist[h[x][i]]) fafa(h[x][i], col[x] ^ I[x][i]);
}
int main() {
  n = read(), m = read();
  rep(i, 1, n) {
    ll x = read(), y = read();
    g[x].push_back(y);
    g[y].push_back(x);
  }
  For(i, 1, m) {
    A[i] = a[i] = read(), B[i] = b[i] = read();
    gg[a[i]].insert(i);
    gg[b[i]].insert(i);
  }
  Dfs(1);
  dfs(1);
  writeln(ans);
  For(i, 1, m) if (!vist[i]) fafa(i, 0);
  For(i, 1, m) if (col[i]) printf("%d %d\n", B[i], A[i]);
  else printf("%d %d\n", A[i], B[i]);
}
/*
1
 &1----2
 &|+
 &|+
 &|+++++
*&3----4
*&|
*&|
*&5




 &1----2
 &|+++++
 &|+
 &|+++++
*&3----4
*&|
*&|
*&5
*/

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define llu unsigned long long
#define ld double
#define llu unsigned long long
#define rep(i, x, y) for (int i = int(x); i < int(y); ++i)
#define For(i, x, y) for (int i = int(x); i <= int(y); ++i)
#define FOr(i, x, y) for (int i = int(x); i >= int(y); --i)
#define pi acos(-1)
#define mk make_pair
#define pa pair<ll, ll>
#define lf else if
#define IL inline
#define max(x, y) ((x) < (y) ? (y) : (x))
#define min(x, y) ((x) < (y) ? (x) : (y))
#define sqr(x) ((x) * (x))
#define Mul(x, y) ((x) = 1LL * (x) * (y) % mod)
#define Add(x, y) ((x) = ((x) + (y)) % mod)
#define Max(x, y) ((x) = ((x) < (y) ? (y) : (x)))
#define Min(x, y) ((x) = ((x) > (y) ? (y) : (x)))
#define E(x) return writeln(x), 0
#define LL (long long)
#define p(x) printf("~%lld~\n", LL(x))
#define pp(x, y) printf("~~%lld %lld~~\n", LL(x), LL(y))
#define ppp(x, y, z) printf("~~~%lld %lld %lld~~~\n", LL(x), LL(y), LL(z))
#define pppp(a, b, c, d)                                                       \
  printf("~~~%lld %lld %lld %lld\n", LL(a), LL(b), LL(c), LL(d))
#define f_in(x) freopen(x ".in", "r", stdin)
#define f_out(x) freopen(x ".out", "w", stdout)
#define open(x) f_in(x), f_out(x)
#define fi first
#define se second
#define GuYue                                                                  \
  puts("\nGuYueNa____________________________________________________________" \
       "____________________")
typedef complex<double> E;
namespace SHENZHEBEI {
#ifdef LOCAL
struct _ {
  _() { freopen("cf.in", "r", stdin); }
} _;
#endif
#define NEG 1
static const int GYN = 2333333;
char SZB[GYN], *SS = SZB, *TT = SZB;
inline char gc() {
  if (SS == TT) {
    TT = (SS = SZB) + fread(SZB, 1, GYN, stdin);
    if (SS == TT)
      return '\n';
  }
  return *SS++;
}
#if NEG
inline ll read() {
  ll x = 0, g = 1;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    if (ch == '-')
      g = -1;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x * g;
}
inline void write(ll x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#else
inline ll read() {
  ll x = 0;
  char ch = gc();
  for (; !isdigit(ch); ch = gc())
    ;
  for (; isdigit(ch); ch = gc())
    x = x * 10 - 48 + ch;
  return x;
}
inline void write(ll x) {
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}
#endif
inline char readchar() {
  char ch = gc();
  for (; isspace(ch); ch = gc())
    ;
  return ch;
}
inline ll readstr(char *s) {
  char ch = gc();
  int cur = 0;
  for (; isspace(ch); ch = gc())
    ;
  for (; !isspace(ch); ch = gc())
    s[cur++] = ch;
  s[cur] = '\0';
  return cur;
}
void Print(long long *a, int s, int t) { For(i, s, t) printf("%lld ", a[i]); }
void Print(int *a, int s, int t) { For(i, s, t) printf("%d ", a[i]); }
void Print(char *a, int s, int t) { For(i, s, t) putchar(a[i]); }
void writeln(ll x) {
  write(x);
  puts("");
}
} // namespace SHENZHEBEI
using namespace SHENZHEBEI;
const ll N = 400000;
vector<ll> g[N], h[N], I[N];
set<ll> gg[N];
ll col[N], vist[N], dep[N], fa[N], IdDel[N], LineDel[N], a[N], b[N], A[N], B[N],
    ans, n, m;
ll Lca(ll x, ll y) {
  for (; x != y; dep[x] < dep[y] ? y = fa[y] : x = fa[x])
    ;
  return x;
}
void Dfs(ll x) {
  rep(i, 0, g[x].size()) {
    ll to = g[x][i];
    if (fa[x] == to)
      continue;
    fa[to] = x;
    dep[to] = dep[x] + 1;
    Dfs(to);
  }
}
void Delete(ll x, ll y) {
  for (; x != y;) {
    if (dep[x] < dep[y])
      swap(x, y);
    if (!IdDel[x])
      IdDel[x] = 1, ans += 2;
    x = fa[x];
  }
}
void Link(ll x, ll y, ll w) {
  h[x].push_back(y);
  I[x].push_back(w);
}
void dfs(ll x) {
  FOr(i, g[x].size() - 1, 0) {
    ll to = g[x][i];
    if (to == fa[x])
      continue;
    dfs(to);
  }
  if (x == 1)
    return;
  for (set<ll>::iterator it = gg[x].begin(), it1; it != gg[x].end(); it = it1) {
    ll id = *it;
    it1 = it;
    ++it1;
    if (LineDel[id] || a[id] == b[id])
      gg[x].erase(it);
  }

  if (!IdDel[x]) {
    if (gg[x].size() == 1) {
      ++ans;
      ll id = *gg[x].begin();
      if (a[id] == x)
        a[id] = fa[a[id]];
      lf(b[id] == x) b[id] = fa[b[id]];
      gg[fa[x]].insert(id);
      IdDel[x] = 1;
    }
    lf(gg[x].size() > 1) {
      set<ll>::iterator it = gg[x].begin();
      ll id1 = *it, id2 = *++it, tp1 = a[id1] == x, tp2 = a[id2] == x,
         to1 = tp1 ? b[id1] : a[id1], to2 = tp2 ? b[id2] : a[id2],
         Down = Lca(x, to1) ^ Lca(x, to2) ^ Lca(to1, to2); // 比较深的那个
      Delete(x, Down);
      Link(id1, id2, tp1 == tp2);
      Link(id2, id1, tp1 == tp2);
      gg[a[id1]].erase(id1);
      gg[b[id1]].erase(id1);
      LineDel[id1] = 1;
      if (tp2) {
        gg[a[id2]].erase(id2);
        a[id2] = tp1 ? b[id1] : a[id1];
        gg[a[id2]].insert(id2);
      } else {
        gg[b[id2]].erase(id2);
        b[id2] = tp1 ? b[id1] : a[id1];
        gg[b[id2]].insert(id2);
      }

      for (it = gg[x].begin(); it != gg[x].end(); ++it) {
        ll id = *it;
        if (a[id] == x)
          a[id] = fa[x];
        else
          b[id] = fa[x];
        gg[fa[x]].insert(id);
      }
    }
  } else {
    for (set<ll>::iterator it = gg[x].begin(); it != gg[x].end(); ++it) {
      ll id = *it;
      if (a[id] == x)
        a[id] = fa[x];
      else
        b[id] = fa[x];
      gg[fa[x]].insert(id);
    }
  }
}
void fafa(ll x, ll Col) {
  vist[x] = 1;
  col[x] = Col;
  rep(i, 0, h[x].size()) if (!vist[h[x][i]]) fafa(h[x][i], col[x] ^ I[x][i]);
}
int main() {
  n = read(), m = read();
  rep(i, 1, n) {
    ll x = read(), y = read();
    g[x].push_back(y);
    g[y].push_back(x);
  }
  For(i, 1, m) {
    A[i] = a[i] = read(), B[i] = b[i] = read();
    gg[a[i]].insert(i);
    gg[b[i]].insert(i);
  }
  Dfs(1);
  dfs(1);
  writeln(ans);
  For(i, 1, m) if (!vist[i]) fafa(i, 0);
  For(i, 1, m) if (col[i]) printf("%d %d\n", B[i], A[i]);
  else printf("%d %d\n", A[i], B[i]);
}
/*
1
 &1----2
 &|+
 &|+
 &|+++++
*&3----4
*&|
*&|
*&5




 &1----2
 &|+++++
 &|+
 &|+++++
*&3----4
*&|
*&|
*&5
*/