Problem p03335 (C++)

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

using ll = int64_t;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(), x.end()
#ifdef MAROON_LOCAL
#define cerr (cerr << "-- line " << __LINE__ << " -- ")
#else
class CerrDummy {
} cerrDummy;
template <class T> CerrDummy &operator<<(CerrDummy &cd, const T &) {
  return cd;
}
using charTDummy = char;
using traitsDummy = char_traits<charTDummy>;
CerrDummy &operator<<(CerrDummy &cd,
                      basic_ostream<charTDummy, traitsDummy> &(
                          basic_ostream<charTDummy, traitsDummy> &)) {
  return cd;
}
#define cerr cerrDummy
#endif
#define REACH cerr << "reached" << endl
#define DMP(x) cerr << #x << ":" << x << endl
#define ZERO(x) memset(x, 0, sizeof(x))
#define ONE(x) memset(x, -1, sizeof(x))

using pi = pair<int, int>;
using vi = vector<int>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  REP(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

ll read() {
  ll i;
  scanf("%" SCNd64, &i);
  return i;
}

void printSpace() { printf(" "); }

void printEoln() { printf("\n"); }

void print(ll x, int suc = 1) {
  printf("%" PRId64, x);
  if (suc == 1)
    printEoln();
  if (suc == 2)
    printSpace();
}

string readString() {
  static char buf[3341000];
  scanf("%s", buf);
  return string(buf);
}

char *readCharArray() {
  static char buf[3341000];
  static int bufUsed = 0;
  char *ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}

template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}

template <class T> T Sq(const T &t) { return t * t; }

#define CAPITAL
void Yes(bool ex = true) {
#ifdef CAPITAL
  cout << "YES" << endl;
#else
  cout << "Yes" << endl;
#endif
  if (ex)
    exit(0);
}
void No(bool ex = true) {
#ifdef CAPITAL
  cout << "NO" << endl;
#else
  cout << "No" << endl;
#endif
  if (ex)
    exit(0);
}

const ll infLL = LLONG_MAX / 3;

#ifdef int
const int inf = infLL;
#else
const int inf = INT_MAX / 2 - 100;
#endif

const int Nmax = 2010;
const int Mmax = 4010;
vector<pi> tr[Nmax];
int u[Mmax], v[Mmax], s[Mmax], t[Mmax], pr[Nmax][2], ans[Mmax], ord;
int otherEnd(int i, int x) { return s[i] == x ? t[i] : s[i]; }
bool merged[Mmax];
vi idx[Nmax];
void dfs(int cur, int p) {
  for (auto e : tr[cur])
    if (e.first != p)
      dfs(e.first, cur);
  int last = -1;
  for (auto i : idx[cur])
    if (!merged[i]) {
      if (s[i] == t[i]) {
        // do nothing
      } else {
        if (last == -1) {
          last = i;
        } else {
          int j = ord++;
          u[j] = s[j] = otherEnd(last, cur);
          v[j] = t[j] = otherEnd(i, cur);
          merged[last] = true;
          merged[i] = true;
          pr[j][0] = last;
          pr[j][1] = i;
          idx[u[j]].PB(j);
          idx[v[j]].PB(j);
          last = -1;
        }
      }
    }
  if (last != -1) {
    assert(p != -1);
    (s[last] == cur ? s[last] : t[last]) = p;
    idx[p].PB(last);
  }
}

int cnt[Nmax];
bool walk(int cur, int p, int tar) {
  if (cur == tar)
    return true;
  for (auto e : tr[cur])
    if (e.first != p) {
      bool d = walk(e.first, cur, tar);
      if (d) {
        cnt[e.second]++;
        return true;
      }
    }
  return false;
}

signed main() {
  int n = read(), m = read();
  REP(i, n - 1) {
    int a = read() - 1, b = read() - 1;
    tr[a].PB(pi(b, i));
    tr[b].PB(pi(a, i));
  }
  REP(i, m) {
    u[i] = s[i] = read() - 1;
    v[i] = t[i] = read() - 1;
    idx[u[i]].PB(i);
    idx[v[i]].PB(i);
  }
  ord = m;
  dfs(0, -1);
  for (int i = ord - 1; i >= m; i--) {
    int a = pr[i][0];
    int b = pr[i][1];
    ans[a] = (ans[i] ^ (s[a] != u[i]));
    ans[b] = (ans[i] ^ (t[b] != v[i]));
  }
  REP(i, m) if (ans[i]) swap(u[i], v[i]);
  REP(i, m) {
    cerr << u[i] << " " << v[i] << endl;
    walk(u[i], -1, v[i]);
  }
  int fun = 0;
  REP(i, n - 1)
  fun += min(cnt[i], int(2));
  print(fun);
  REP(i, m) {
    print(u[i] + 1, 2);
    print(v[i] + 1);
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = int64_t;
#define int ll

#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define REP(i, b) FOR(i, 0, b)
#define MP make_pair
#define PB push_back
#define ALL(x) x.begin(), x.end()
#ifdef MAROON_LOCAL
#define cerr (cerr << "-- line " << __LINE__ << " -- ")
#else
class CerrDummy {
} cerrDummy;
template <class T> CerrDummy &operator<<(CerrDummy &cd, const T &) {
  return cd;
}
using charTDummy = char;
using traitsDummy = char_traits<charTDummy>;
CerrDummy &operator<<(CerrDummy &cd,
                      basic_ostream<charTDummy, traitsDummy> &(
                          basic_ostream<charTDummy, traitsDummy> &)) {
  return cd;
}
#define cerr cerrDummy
#endif
#define REACH cerr << "reached" << endl
#define DMP(x) cerr << #x << ":" << x << endl
#define ZERO(x) memset(x, 0, sizeof(x))
#define ONE(x) memset(x, -1, sizeof(x))

using pi = pair<int, int>;
using vi = vector<int>;
using ld = long double;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  REP(i, (int)v.size()) {
    if (i)
      os << ",";
    os << v[i];
  }
  os << "}";
  return os;
}

ll read() {
  ll i;
  scanf("%" SCNd64, &i);
  return i;
}

void printSpace() { printf(" "); }

void printEoln() { printf("\n"); }

void print(ll x, int suc = 1) {
  printf("%" PRId64, x);
  if (suc == 1)
    printEoln();
  if (suc == 2)
    printSpace();
}

string readString() {
  static char buf[3341000];
  scanf("%s", buf);
  return string(buf);
}

char *readCharArray() {
  static char buf[3341000];
  static int bufUsed = 0;
  char *ret = buf + bufUsed;
  scanf("%s", ret);
  bufUsed += strlen(ret) + 1;
  return ret;
}

template <class T, class U> void chmax(T &a, U b) {
  if (a < b)
    a = b;
}

template <class T, class U> void chmin(T &a, U b) {
  if (b < a)
    a = b;
}

template <class T> T Sq(const T &t) { return t * t; }

#define CAPITAL
void Yes(bool ex = true) {
#ifdef CAPITAL
  cout << "YES" << endl;
#else
  cout << "Yes" << endl;
#endif
  if (ex)
    exit(0);
}
void No(bool ex = true) {
#ifdef CAPITAL
  cout << "NO" << endl;
#else
  cout << "No" << endl;
#endif
  if (ex)
    exit(0);
}

const ll infLL = LLONG_MAX / 3;

#ifdef int
const int inf = infLL;
#else
const int inf = INT_MAX / 2 - 100;
#endif

const int Nmax = 2010;
const int Mmax = 4010;
vector<pi> tr[Nmax];
int u[Mmax], v[Mmax], s[Mmax], t[Mmax], pr[Mmax][2], ans[Mmax], ord;
int otherEnd(int i, int x) { return s[i] == x ? t[i] : s[i]; }
bool merged[Mmax];
vi idx[Nmax];
void dfs(int cur, int p) {
  for (auto e : tr[cur])
    if (e.first != p)
      dfs(e.first, cur);
  int last = -1;
  for (auto i : idx[cur])
    if (!merged[i]) {
      if (s[i] == t[i]) {
        // do nothing
      } else {
        if (last == -1) {
          last = i;
        } else {
          int j = ord++;
          u[j] = s[j] = otherEnd(last, cur);
          v[j] = t[j] = otherEnd(i, cur);
          merged[last] = true;
          merged[i] = true;
          pr[j][0] = last;
          pr[j][1] = i;
          idx[u[j]].PB(j);
          idx[v[j]].PB(j);
          last = -1;
        }
      }
    }
  if (last != -1) {
    assert(p != -1);
    (s[last] == cur ? s[last] : t[last]) = p;
    idx[p].PB(last);
  }
}

int cnt[Nmax];
bool walk(int cur, int p, int tar) {
  if (cur == tar)
    return true;
  for (auto e : tr[cur])
    if (e.first != p) {
      bool d = walk(e.first, cur, tar);
      if (d) {
        cnt[e.second]++;
        return true;
      }
    }
  return false;
}

signed main() {
  int n = read(), m = read();
  REP(i, n - 1) {
    int a = read() - 1, b = read() - 1;
    tr[a].PB(pi(b, i));
    tr[b].PB(pi(a, i));
  }
  REP(i, m) {
    u[i] = s[i] = read() - 1;
    v[i] = t[i] = read() - 1;
    idx[u[i]].PB(i);
    idx[v[i]].PB(i);
  }
  ord = m;
  dfs(0, -1);
  for (int i = ord - 1; i >= m; i--) {
    int a = pr[i][0];
    int b = pr[i][1];
    ans[a] = (ans[i] ^ (s[a] != u[i]));
    ans[b] = (ans[i] ^ (t[b] != v[i]));
  }
  REP(i, m) if (ans[i]) swap(u[i], v[i]);
  REP(i, m) {
    cerr << u[i] << " " << v[i] << endl;
    walk(u[i], -1, v[i]);
  }
  int fun = 0;
  REP(i, n - 1)
  fun += min(cnt[i], int(2));
  print(fun);
  REP(i, m) {
    print(u[i] + 1, 2);
    print(v[i] + 1);
  }
}