Problem p02976 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize ("-O3","unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define pb(a) push_back
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define int long long
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \
  REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
const int MOD = 998244353;
const int MAX = 200020;
const double pi = acos(-1);
const double EPS = 1e-12;
const ll INF = 1e18;
int siz[MAX];  // 錬結成分のサイズ
int par[MAX];  // 親
int Rank[MAX]; // 木の深さ

// n要素で初期化
void init(int n) {
  REP(i, n) {
    par[i] = i;
    Rank[i] = 0;
    siz[i] = 1;
  }
}

// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]); // 経路圧縮(根に直接つなぎなおす)もしつつ
  }
}

// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  // 低いほうを高いほうにつなげる(効率化)
  if (Rank[x] < Rank[y]) {
    par[x] = y;
    siz[x] += siz[y];
    siz[y] = siz[x];
  } else {
    siz[y] += siz[x];
    siz[x] = siz[y];
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
vector<P> ans;
vector<int> G[100010];
int score[100010];
set<P> edges;
void dfs(int n, int pre) {
  for (auto nxt : G[n]) {
    if (pre == nxt)
      continue;
    dfs(nxt, n);
  }
  if (n != 0) {
    if (!(score[n] & 1)) {
      score[pre] ^= 1;
      ans.push_back({pre, n});
    } else {
      ans.push_back({n, pre});
    }
  }
}

void solve() {
  int N, M;
  cin >> N >> M;
  REP(i, M) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    edges.insert({u, v});
  }
  if (M & 1) {
    print(-1);
    return;
  }
  init(N + 1);
  auto itr = edges.begin();
  // while (itr != edges.end()) {
  for (auto p : edges) {
    // int u = itr->first, v = itr->second;
    int u = p.first, v = p.second;
    if (same(u, v)) {
      score[u] ^= 1;
      itr++;
      ans.push_back({u, v});
    } else {
      G[u].push_back(v);
      unite(u, v);
      itr++;
    }
  }
  dfs(0, 0);
  if (score[0] & 1) {
    print(-1);
    return;
  } else {
    REP(i, M) {
      pe(ans[i].first + 1);
      print(ans[i].second + 1);
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();
}


Fixed submission:
// #pragma GCC optimize ("-O3","unroll-loops")
#include <algorithm>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits.h>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define pb(a) push_back
#define print(x) cout << (x) << '\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), n)
#define ub(v, n) upper_bound(v.begin(), v.end(), n)
#define int long long
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \
  REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\n" : " ")

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
typedef pair<int, int> P;
const int MOD = 998244353;
const int MAX = 200020;
const double pi = acos(-1);
const double EPS = 1e-12;
const ll INF = 1e18;
int siz[MAX];  // 錬結成分のサイズ
int par[MAX];  // 親
int Rank[MAX]; // 木の深さ

// n要素で初期化
void init(int n) {
  REP(i, n) {
    par[i] = i;
    Rank[i] = 0;
    siz[i] = 1;
  }
}

// 木の根を求める
int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]); // 経路圧縮(根に直接つなぎなおす)もしつつ
  }
}

// xとyの属する集合を併合
void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;
  // 低いほうを高いほうにつなげる(効率化)
  if (Rank[x] < Rank[y]) {
    par[x] = y;
    siz[x] += siz[y];
    siz[y] = siz[x];
  } else {
    siz[y] += siz[x];
    siz[x] = siz[y];
    par[y] = x;
    if (Rank[x] == Rank[y])
      Rank[x]++;
  }
}

// xとyが同じ集合に属するか否か
bool same(int x, int y) { return find(x) == find(y); }
vector<P> ans;
vector<int> G[100010];
int score[100010];
set<P> edges;
void dfs(int n, int pre) {
  for (auto nxt : G[n]) {
    if (pre == nxt)
      continue;
    dfs(nxt, n);
  }
  if (n != 0) {
    if (!(score[n] & 1)) {
      score[pre] ^= 1;
      ans.push_back({pre, n});
    } else {
      ans.push_back({n, pre});
    }
  }
}

void solve() {
  int N, M;
  cin >> N >> M;
  REP(i, M) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    edges.insert({u, v});
  }
  if (M & 1) {
    print(-1);
    return;
  }
  init(N + 1);
  auto itr = edges.begin();
  // while (itr != edges.end()) {
  for (auto p : edges) {
    // int u = itr->first, v = itr->second;
    int u = p.first, v = p.second;
    if (same(u, v)) {
      score[u] ^= 1;
      itr++;
      ans.push_back({u, v});
    } else {
      G[u].push_back(v);
      G[v].push_back(u);
      unite(u, v);
      itr++;
    }
  }
  dfs(0, 0);
  if (score[0] & 1) {
    print(-1);
    return;
  } else {
    REP(i, M) {
      pe(ans[i].first + 1);
      print(ans[i].second + 1);
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  solve();
}