Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename T> void uin(T &a, T b) {
  if (b < a) {
    a = b;
  }
}

template <typename T> void uax(T &a, T b) {
  if (b > a) {
    a = b;
  }
}

#define int long long
#define ghost signed
#define left left228
#define right right228
#define prev prev228
#define list list228

const int N = 100 * 1000 + 228;

namespace DSU {
int p[N], sz[N];

void init(int n) {
  for (int i = 1; i <= n; ++i) {
    p[i] = i;
    sz[i] = 1;
  }
}

int getp(int v) {
  if (v == p[v])
    return v;
  p[v] = getp(p[v]);
  return p[v];
}

bool join(int u, int v) {
  u = getp(u);
  v = getp(v);
  if (u == v)
    return 0;
  if (sz[u] < sz[v]) {
    p[u] = v;
    sz[v] += sz[u];
  } else {
    p[v] = u;
    sz[u] += sz[v];
  }
  return 1;
}
}; // namespace DSU

vector<int> g[N];
vector<pair<int, int>> ans;
int deg[N];

void dfs(int v, int par = -1) {
  for (int to : g[v]) {
    if (to != par)
      dfs(to, v);
  }
  if (deg[v]) {
    ans.emplace_back(v, par);
  } else {
    if (par != -1) {
      ans.emplace_back(par, v);
      deg[par] ^= 1;
    }
  }
}

int a[100];

ghost main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  if (m & 1) {
    cout << -1 << '\n';
    return 0;
  }
  DSU::init(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    if (!DSU::join(u, v)) {
      g[u].push_back(v);
      g[v].push_back(u);
    } else {
      ans.emplace_back(u, v);
      deg[u] ^= 1;
    }
  }
  dfs(1);
  for (auto p : ans)
    cout << p.first << ' ' << p.second << '\n';
  return 0;
} // kek ;


Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native")
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stdio.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

template <typename T> void uin(T &a, T b) {
  if (b < a) {
    a = b;
  }
}

template <typename T> void uax(T &a, T b) {
  if (b > a) {
    a = b;
  }
}

#define int long long
#define ghost signed
#define left left228
#define right right228
#define prev prev228
#define list list228

const int N = 100 * 1000 + 228;

namespace DSU {
int p[N], sz[N];

void init(int n) {
  for (int i = 1; i <= n; ++i) {
    p[i] = i;
    sz[i] = 1;
  }
}

int getp(int v) {
  if (v == p[v])
    return v;
  p[v] = getp(p[v]);
  return p[v];
}

bool join(int u, int v) {
  u = getp(u);
  v = getp(v);
  if (u == v)
    return 0;
  if (sz[u] < sz[v]) {
    p[u] = v;
    sz[v] += sz[u];
  } else {
    p[v] = u;
    sz[u] += sz[v];
  }
  return 1;
}
}; // namespace DSU

vector<int> g[N];
vector<pair<int, int>> ans;
int deg[N];

void dfs(int v, int par = -1) {
  for (int to : g[v]) {
    if (to != par)
      dfs(to, v);
  }
  if (deg[v]) {
    ans.emplace_back(v, par);
  } else {
    if (par != -1) {
      ans.emplace_back(par, v);
      deg[par] ^= 1;
    }
  }
}

int a[100];

ghost main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  if (m & 1) {
    cout << -1 << '\n';
    return 0;
  }
  DSU::init(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    cin >> u >> v;
    if (DSU::join(u, v)) {
      g[u].push_back(v);
      g[v].push_back(u);
    } else {
      ans.emplace_back(u, v);
      deg[u] ^= 1;
    }
  }
  dfs(1);
  for (auto p : ans)
    cout << p.first << ' ' << p.second << '\n';
  return 0;
} // kek ;