prithivMLmods/Coder-Stat huggingface dataset:
Problem p02975 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define MP make_pair
#define PII pair<int, int>
typedef long long LL;
const LL MOD = 998244353;
const int INF = 0x3fffffff;
const int N = 100010;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, x;

int main() {
#ifndef ONLINE_JUDGE
  freopen("test.in", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  map<int, int> cnt;
  rep(i, 1, n) cin >> x, cnt[x]++;
  if (cnt.size() <= 3) {
    int a[4], b[4], m = 0;
    for (auto x : cnt) {
      a[++m] = x.first;
      b[m] = x.second;
    }
    // rep(i,1,m)cout<<a[i]<<" "<<b[i]<<endl;
    bool flag = false;
    if (m == 1 && a[1] == 0)
      flag = true;
    if (m == 2 && b[1] == b[2] * 2 && (a[1] ^ a[1] ^ a[2]) == 0)
      flag = true;
    if (m == 2 && b[1] * 2 == b[2] && (a[1] ^ a[2] ^ a[2]) == 0)
      flag = true;
    if (m == 3 && b[1] == b[2] && b[1] == b[3] && (a[1] ^ a[2] ^ a[3]) == 0)
      flag = true;
    if (flag) {
      cout << "Yes\n";
      return 0;
    }
  }
  cout << "No\n";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
#define rrep(i, a, b) for (int i = (a); i >= (b); --i)
#define PB push_back
#define MP make_pair
#define PII pair<int, int>
typedef long long LL;
const LL MOD = 998244353;
const int INF = 0x3fffffff;
const int N = 100010;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n, x;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  map<int, int> cnt;
  rep(i, 1, n) cin >> x, cnt[x]++;
  if (cnt.size() <= 3) {
    int a[4], b[4], m = 0;
    for (auto x : cnt) {
      a[++m] = x.first;
      b[m] = x.second;
    }
    // rep(i,1,m)cout<<a[i]<<" "<<b[i]<<endl;
    bool flag = false;
    if (m == 1 && a[1] == 0)
      flag = true;
    if (m == 2 && b[1] == b[2] * 2 && (a[1] ^ a[1] ^ a[2]) == 0)
      flag = true;
    if (m == 2 && b[1] * 2 == b[2] && (a[1] ^ a[2] ^ a[2]) == 0)
      flag = true;
    if (m == 3 && b[1] == b[2] && b[1] == b[3] && (a[1] ^ a[2] ^ a[3]) == 0)
      flag = true;
    if (flag) {
      cout << "Yes\n";
      return 0;
    }
  }
  cout << "No\n";
  return 0;
}
Problem p02975 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

void re(int &x);

template <class T> void re(std::vector<T> &a);

void pr(const char *x);

void ps();
template <class T, class... Ts> void ps(const T &t, const Ts &...ts);

#ifdef FFDBG

#else
#define dbg(x...) dsfdsfsdfasd
#endif

typedef vector<int> vi;

int n;

void solve() {
  re(n);
  vi a(n);
  re(a);

  vi h(n, -1);

  h[0] = 0;
  for (int i = 2; i != 0; i = (i + 2) % n) {
    h[i] = (h[(i + n - 2) % n] ^ a[(i + n - 1) % n]);
  }

  if ((h[0] ^ h[n - 2]) != a[n - 1]) {
    ps("No");
    return;
  }

  if (h[1] == -1) {
    h[1] = 0;
    for (int i = 3; i != 0; i = (i + 2) % n) {
      h[i] = (h[(i + n - 2) % n] ^ a[(i + n - 1) % n]);
    }

    if ((h[1] ^ h[n - 1]) != a[0]) {
      ps("No");
      return;
    }
  }

  ps("Yes");
}

int main() { solve(); }

void re(int &x) { scanf("%d", &x); }

template <class T> void re(std::vector<T> &a) {
  for (int i = 0; i < a.size(); i++)
    re(a[i]);
}

void pr(const char *x) { printf("%s", x); }

void ps() { pr("\n"); }
template <class T, class... Ts> void ps(const T &t, const Ts &...ts) {
  pr(t);
  if (sizeof...(ts))
    pr(" ");
  ps(ts...);
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdio>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

void re(int &x);

template <class T> void re(std::vector<T> &a);

void pr(const char *x);

void ps();
template <class T, class... Ts> void ps(const T &t, const Ts &...ts);

#ifdef FFDBG

#else
#define dbg(x...) dsfdsfsdfasd
#endif

typedef vector<int> vi;

int n;

void solve() {
  re(n);
  vi a(n);
  re(a);

  vi h(n, -1);

  h[0] = 0;
  for (int i = 2; i != 0; i = (i + 2) % n) {
    h[i] = (h[(i + n - 2) % n] ^ a[(i + n - 1) % n]);
  }

  if ((h[0] ^ h[n - 2]) != a[n - 1]) {
    ps("No");
    return;
  }

  if (h[1] == -1) {
    h[1] = 0;
    for (int i = 3; i != 1; i = (i + 2) % n) {
      h[i] = (h[(i + n - 2) % n] ^ a[(i + n - 1) % n]);
    }

    if ((h[1] ^ h[n - 1]) != a[0]) {
      ps("No");
      return;
    }
  }

  ps("Yes");
}

int main() { solve(); }

void re(int &x) { scanf("%d", &x); }

template <class T> void re(std::vector<T> &a) {
  for (int i = 0; i < a.size(); i++)
    re(a[i]);
}

void pr(const char *x) { printf("%s", x); }

void ps() { pr("\n"); }
template <class T, class... Ts> void ps(const T &t, const Ts &...ts) {
  pr(t);
  if (sizeof...(ts))
    pr(" ");
  ps(ts...);
}
Problem p02975 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int main() {
  cin.tie(0)->sync_with_stdio(false);

  ll n;
  cin >> n;

  ll cnt0 = 0;
  map<ll, ll> hat;
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    if (tmp == 0)
      cnt0++;
    hat[tmp]++;
  }

  bool ok = false;
  if (cnt0 == n)
    ok = true;
  else if (hat.size() == 2) {
    if (hat.count(0)) {
      ll cnt = 0;
      for (auto i : hat)
        if (i.first != 0)
          cnt = i.second;
      if (cnt0 * 2 == cnt)
        ok = true;
    }
  } else if (!(n % 3)) {
    ll val[3];
    ll cnt[3];
    ll i = 0;
    for (auto itr : hat) {
      val[i] = itr.first;
      cnt[i] = itr.second;
      i++;
    }
    if (cnt[0] == cnt[1] && cnt[1] == cnt[2])
      if (((val[0] ^ val[1]) ^ val[2]) == 0)
        ok = true;
  }

  if (ok)
    cout << "Yes" << '\n';
  else
    cout << "No" << '\n';
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (long long i = 0; i < (n); i++)
using namespace std;
using ll = long long;

int main() {
  cin.tie(0)->sync_with_stdio(false);

  ll n;
  cin >> n;

  ll cnt0 = 0;
  map<ll, ll> hat;
  REP(i, n) {
    ll tmp;
    cin >> tmp;
    if (tmp == 0)
      cnt0++;
    hat[tmp]++;
  }

  bool ok = false;
  if (cnt0 == n)
    ok = true;
  else if (hat.size() == 2) {
    if (hat.count(0)) {
      ll cnt = 0;
      for (auto i : hat)
        if (i.first != 0)
          cnt = i.second;
      if (cnt0 * 2 == cnt)
        ok = true;
    }
  } else if (hat.size() == 3) {
    ll val[3];
    ll cnt[3];
    ll i = 0;
    for (auto itr : hat) {
      val[i] = itr.first;
      cnt[i] = itr.second;
      i++;
    }
    if (cnt[0] == cnt[1] && cnt[1] == cnt[2])
      if (((val[0] ^ val[1]) ^ val[2]) == 0)
        ok = true;
  }

  if (ok)
    cout << "Yes" << '\n';
  else
    cout << "No" << '\n';
  return 0;
}
Problem p02975 (C++)

Buggy submission (status: Time Limit Exceeded):
// A.

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m[a[i]] += 1;
  }
  set<int> s(a.begin(), a.end());
  vector<int> v(n);
  for (int t = 0; t < 10000; ++t) {
    int p = a[rand() % n];
    int q = a[rand() % n];
    int x = p ^ q;
    if (s.find(x) != s.end()) {
      map<int, int> r = m;
      r[p] -= 1;
      r[q] -= 1;
      r[x] -= 1;
      if (r[x] >= 0) {
        v[0] = p;
        v[1] = q;
        v[2] = x;
        int i;
        for (i = 3; i < n; ++i) {
          x = v[i - 2] ^ v[i - 1];
          v[i] = x;
          r[x] -= 1;
          if (r[x] < 0) {
            break;
          }
        }
        if (i >= n && (v[0] == (v[1] ^ v[n - 1]))) {
          cout << "Yes" << endl;
          return 0;
        }
      }
    }
  }
  cout << "No" << endl;
  return 0;
}


Fixed submission:
// A.

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <set>
#include <sstream>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> m;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    m[a[i]] += 1;
  }
  set<int> s(a.begin(), a.end());
  vector<int> v(n);
  for (int t = 0; t < 500; ++t) {
    int ar = rand() % n, br = rand() % n;
    if (ar == br)
      continue;
    int p = a[ar];
    int q = a[br];
    int x = p ^ q;
    if (s.find(x) != s.end()) {
      map<int, int> r = m;
      r[p] -= 1;
      r[q] -= 1;
      r[x] -= 1;
      if (r[x] >= 0) {
        v[0] = p;
        v[1] = q;
        v[2] = x;
        int i;
        for (i = 3; i < n; ++i) {
          x = v[i - 2] ^ v[i - 1];
          v[i] = x;
          r[x] -= 1;
          if (r[x] < 0) {
            break;
          }
        }
        if (i >= n && (v[0] == (v[1] ^ v[n - 1]))) {
          cout << "Yes" << endl;
          return 0;
        }
      }
    }
  }
  cout << "No" << endl;
  return 0;
}
Problem p02975 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1000000000000
#define MAX 1000000000000000000
typedef long long ll;
typedef pair<ll, int> P;
typedef pair<pair<int, int>, int> p;
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/

int main() {
  int N, count = 0;
  cin >> N;
  ll a;
  vector<P> V;
  for (int i = 0; i < N; i++) {
    bool flag = false;
    cin >> a;
    for (int i = 0; i < (int)V.size(); i++) {
      if (V[i].first == a) {
        V[i].second++;
        flag = true;
      }
    }
    if (V.size() >= 4) {
      cout << "No" << endl;
      return 0;
    }
    if (flag)
      continue;
    V.push_back(P(a, 1));
  }
  if ((int)V.size() == 3) {
    return 1;
    if (V[0].first ^ V[1].first ^ V[2].first == 0 &&
        V[0].second == V[1].second && V[1].second == V[2].second) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;

  } else if ((int)V.size() == 2) {
    if ((V[0].first == 0 && V[0].second * 2 == V[1].second) ||
        (V[1].first == 0 && V[1].second * 2 == V[0].second)) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  } else if ((int)V.size() == 1) {
    if (V[0].first == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  } else
    cout << "No" << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF 1000000000000
#define MAX 1000000000000000000
typedef long long ll;
typedef pair<ll, int> P;
typedef pair<pair<int, int>, int> p;
#define bit(n, k) ((n >> k) & 1) /*nのk bit目*/

int main() {
  int N, count = 0;
  cin >> N;
  ll a;
  vector<P> V;
  for (int i = 0; i < N; i++) {
    bool flag = false;
    cin >> a;
    for (int i = 0; i < (int)V.size(); i++) {
      if (V[i].first == a) {
        V[i].second++;
        flag = true;
      }
    }
    if (V.size() >= 4) {
      cout << "No" << endl;
      return 0;
    }
    if (flag)
      continue;
    V.push_back(P(a, 1));
  }
  if ((int)V.size() == 3) {
    int x = V[0].first ^ V[1].first;
    x ^= V[2].first;
    if (x == 0 && V[0].second == V[1].second && V[1].second == V[2].second) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;

  } else if ((int)V.size() == 2) {
    if ((V[0].first == 0 && V[0].second * 2 == V[1].second) ||
        (V[1].first == 0 && V[1].second * 2 == V[0].second)) {
      cout << "Yes" << endl;
    } else
      cout << "No" << endl;
  } else if ((int)V.size() == 1) {
    if (V[0].first == 0)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  } else
    cout << "No" << endl;
}
Problem p02975 (C++)

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll n;
ll a[100001];
vector<ll> vs;
map<ll, ll> tot;

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

  cin >> n;
  rep(i, n) {
    cin >> a[i];
    vs.push_back(a[i]);
    tot[a[i]]++;
  }
  sort(all(vs));
  uni(vs);

  rep(i, vs.size()) rep(j, vs.size()) {
    bool ok = true;
    map<ll, ll> cnt = tot;
    ll b[3];
    b[0] = vs[i];
    b[1] = vs[j];
    b[2] = vs[i] ^ vs[j];
    rep(k, n) {
      if (cnt[b[k % 3]] == 0)
        ok = false;
      cnt[b[k % 3]]--;
    }
    if (ok) {
      cout << "Yes" << endl;
      return 0;
    }
  }

  cout << "No" << endl;
  return 0;
}


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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll n;
ll a[100001];
vector<ll> vs;
map<ll, ll> tot;

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

  cin >> n;
  rep(i, n) {
    cin >> a[i];
    vs.push_back(a[i]);
    tot[a[i]]++;
  }
  sort(all(vs));
  uni(vs);
  if (vs.size() > 3) {
    cout << "No" << endl;
    return 0;
  }

  rep(i, vs.size()) rep(j, vs.size()) {
    bool ok = true;
    map<ll, ll> cnt = tot;
    ll b[3];
    b[0] = vs[i];
    b[1] = vs[j];
    b[2] = vs[i] ^ vs[j];
    rep(k, n) {
      if (cnt[b[k % 3]] == 0)
        ok = false;
      cnt[b[k % 3]]--;
    }
    if (ok) {
      cout << "Yes" << endl;
      return 0;
    }
  }

  cout << "No" << endl;
  return 0;
}
Problem p02976 (Python)

Buggy submission (status: Runtime Error):
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(100000)

n, m = [int(x) for x in input().split()]
edges = []
g = [[] for _ in range(n)]
for _ in range(m):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    e = len(edges)
    g[a].append(e)
    g[b].append(e)
    edges.append((a, b))

if m % 2 == 1:
    print(-1)
    sys.exit(0)


visited = [False] * n
visitede = [False] * m


def build_stree(i):
    cts = []
    for e in g[i]:
        j = edges[e][1 if edges[e][0] == i else 0]
        if visited[j]:
            continue
        visitede[e] = True
        visited[j] = True
        cts.append((e, build_stree(j)))
    return i, cts


visited[0] = True
st = build_stree(0)

rev_e = [None if v else False for v in visitede]


def pos(t):
    i, cts = t
    for _, ct in cts:
        pos(ct)
    es = [e for e in g[i] if rev_e[e] is None]
    assert len(es) <= 1
    if not es:
        return
    f = es[0]
    rev_e[f] = (sum((edges[e][0] == i) ^ rev_e[e] for e in g[i] if e != f) % 2 != 0) ^ (
        edges[f][0] == i
    )


pos(st)

for ed, re in zip(edges, rev_e):
    if re:
        b, a = ed
    else:
        a, b = ed
    print(a + 1, b + 1)


Fixed submission:
#!/usr/bin/env python3

import sys

sys.setrecursionlimit(1000000)

n, m = [int(x) for x in input().split()]
edges = []
g = [[] for _ in range(n)]
for _ in range(m):
    a, b = [int(x) for x in input().split()]
    a -= 1
    b -= 1
    e = len(edges)
    g[a].append(e)
    g[b].append(e)
    edges.append((a, b))

if m % 2 == 1:
    print(-1)
    sys.exit(0)


visited = [False] * n
visitede = [False] * m


def build_stree(i):
    cts = []
    for e in g[i]:
        j = edges[e][1 if edges[e][0] == i else 0]
        if visited[j]:
            continue
        visitede[e] = True
        visited[j] = True
        cts.append((e, build_stree(j)))
    return i, cts


visited[0] = True
st = build_stree(0)

rev_e = [None if v else False for v in visitede]


def pos(t):
    i, cts = t
    for _, ct in cts:
        pos(ct)
    es = [e for e in g[i] if rev_e[e] is None]
    assert len(es) <= 1
    if not es:
        return
    f = es[0]
    rev_e[f] = (sum((edges[e][0] == i) ^ rev_e[e] for e in g[i] if e != f) % 2 != 0) ^ (
        edges[f][0] == i
    )


pos(st)

for ed, re in zip(edges, rev_e):
    if re:
        b, a = ed
    else:
        a, b = ed
    print(a + 1, b + 1)
Problem p02976 (C++)

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

signed main(void) {

  int N, M;
  cin >> N >> M;

  if (M % 2 == 1) {
    cout << "-1" << endl;
    return 0;
  }

  vector<vector<int>> to(M);

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  vector<pair<int, int>> depth(N);
  for (int i = 0; i < N; i++) {
    depth[i] = {-1, i};
  }
  depth[0] = {0, 0};

  queue<int> Q;
  Q.push(0);

  while (!Q.empty()) {

    int p = Q.front();
    Q.pop();

    for (auto q : to[p]) {
      if (depth[q].first != -1) {
        continue;
      }

      depth[q].first = depth[p].first + 1;

      Q.push(q);
    }
  }

  sort(depth.rbegin(), depth.rend()); // 深い順にソートする

  vector<bool> used(N, false);
  vector<bool> even(N, true);

  // depthが深いから決めていく

  set<pair<int, int>> ans;

  for (int i = 0; i < N; i++) {

    int p = depth[i].second;

    for (auto q : to[p]) {
      if (used[q]) {
        continue;
      } // 全て埋めてしまった点については何も見ない

      if (even[p]) { // qからpに辺をはる
        even[q] = !even[q];
        ans.insert({q, p});
      } else { // pからqに辺をはる
        even[p] = true;
        ans.insert({p, q});
      }
    }

    used[p] = true;
  }

  for (auto ne : ans) {
    cout << ne.first + 1 << " " << ne.second + 1 << endl;
  }

  return 0;
}


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

signed main(void) {

  int N, M;
  cin >> N >> M;

  if (M % 2 == 1) {
    cout << "-1" << endl;
    return 0;
  }

  vector<vector<int>> to(N);

  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    to[b].push_back(a);
  }

  vector<pair<int, int>> depth(N);
  for (int i = 0; i < N; i++) {
    depth[i] = {-1, i};
  }
  depth[0] = {0, 0};

  queue<int> Q;
  Q.push(0);

  while (!Q.empty()) {

    int p = Q.front();
    Q.pop();

    for (auto q : to[p]) {
      if (depth[q].first != -1) {
        continue;
      }

      depth[q].first = depth[p].first + 1;

      Q.push(q);
    }
  }

  sort(depth.rbegin(), depth.rend()); // 深い順にソートする

  vector<bool> used(N, false);
  vector<bool> even(N, true);

  // depthが深いから決めていく

  set<pair<int, int>> ans;

  for (int i = 0; i < N; i++) {

    int p = depth[i].second;

    for (auto q : to[p]) {
      if (used[q]) {
        continue;
      } // 全て埋めてしまった点については何も見ない

      if (even[p]) { // qからpに辺をはる
        even[q] = !even[q];
        ans.insert({q, p});
      } else { // pからqに辺をはる
        even[p] = true;
        ans.insert({p, q});
      }
    }

    used[p] = true;
  }

  for (auto ne : ans) {
    cout << ne.first + 1 << " " << ne.second + 1 << endl;
  }

  return 0;
}
Problem p02976 (C++)

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

vector<vector<int>> graph;
vector<int> degree;
vector<int> depth;
vector<pair<int, int>> ans;

void DFS(int v, int previous_v) {
  for (int adjacent : graph[v]) {
    if (adjacent == previous_v)
      continue;

    if (depth[adjacent] == -1) { // just dive down
      depth[adjacent] = depth[v] + 1;
      DFS(adjacent, v);
    } else {
      if (depth[adjacent] > depth[v]) {
        ans.push_back({v, adjacent});
        degree[v] += 1;
      }
    }
  }

  if (previous_v == -1)
    return;
  if (previous_v != -1) {
    if (degree[v] % 2 ==
        1) { // then use (v --> previous_v) to make degree[v] even
      ans.push_back({v, previous_v});
      degree[v] += 1;
    } else { // degree[v] % 2 == 0
      ans.push_back({previous_v, v});
      degree[previous_v] += 1;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  graph.resize(m);
  degree.resize(n);
  depth.assign(n, -1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a -= 1;
    b -= 1;

    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  if (m % 2 == 1) {
    cout << -1 << endl;
    return 0;
  }

  depth[0] = 0;
  DFS(0, -1);

  for (auto x : ans) {
    cout << x.first + 1 << " " << x.second + 1 << endl;
  }
  return 0;
}

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

vector<vector<int>> graph;
vector<int> degree;
vector<int> depth;
vector<pair<int, int>> ans;

void DFS(int v, int previous_v) {
  for (int adjacent : graph[v]) {
    if (adjacent == previous_v)
      continue;

    if (depth[adjacent] == -1) { // just dive down
      depth[adjacent] = depth[v] + 1;
      DFS(adjacent, v);
    } else {
      if (depth[adjacent] > depth[v]) {
        ans.push_back({v, adjacent});
        degree[v] += 1;
      }
    }
  }

  if (previous_v == -1)
    return;
  if (previous_v != -1) {
    if (degree[v] % 2 ==
        1) { // then use (v --> previous_v) to make degree[v] even
      ans.push_back({v, previous_v});
      degree[v] += 1;
    } else { // degree[v] % 2 == 0
      ans.push_back({previous_v, v});
      degree[previous_v] += 1;
    }
  }
}

int main() {
  int n, m;
  cin >> n >> m;

  graph.resize(n);
  degree.resize(n);
  depth.assign(n, -1);

  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a -= 1;
    b -= 1;

    graph[a].push_back(b);
    graph[b].push_back(a);
  }

  if (m % 2 == 1) {
    cout << -1 << endl;
    return 0;
  }

  depth[0] = 0;
  DFS(0, -1);

  for (auto x : ans) {
    cout << x.first + 1 << " " << x.second + 1 << endl;
  }
  return 0;
}
Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
struct edge {
  int to, next, ok;
} e[N];
int n, m, good = 1, h[N];
int visited[N], check[N];
void add(int u, int v) {
  e[++good] = (edge){v, h[u], 0};
  h[u] = good;
}
void dfs(int u, int p) {
  visited[u] = ++*visited;
  for (int i = h[u]; i; i = e[i].next) {
    if (i ^ p ^ 1) {
      if (!visited[e[i].to]) {
        dfs(e[i].to, i);
        if (check[e[i].to] & 1) {
          e[i].ok = -1;
          e[i ^ 1].ok = 1;
          check[e[i].to] ^= 1;
        } else {
          e[i].ok = 1;
          e[i ^ 1].ok = -1;
          check[u] ^= 1;
        }
      } else if (visited[e[i].to] < visited[u]) {
        e[i].ok = 1;
        e[i ^ 1].ok = -1;
        check[u] ^= 1;
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i, 0);
  }
  for (int i = 1; i <= n; i++) {
    if (check[i]) {
      cout << "-1";
      return 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = h[i]; j; j = e[j].next) {
      if (e[j].ok == 1) {
        cout << i << " " << e[j].to << endl;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
struct edge {
  int to, next, ok;
} e[N];
int n, m, good = 1, h[N];
int visited[N], check[N];
void add(int u, int v) {
  e[++good] = (edge){v, h[u], 0};
  h[u] = good;
}
void dfs(int u, int p) {
  visited[u] = ++*visited;
  for (int i = h[u]; i; i = e[i].next) {
    if (i ^ p ^ 1) {
      if (!visited[e[i].to]) {
        dfs(e[i].to, i);
        if (check[e[i].to] & 1) {
          e[i].ok = -1;
          e[i ^ 1].ok = 1;
          check[e[i].to] ^= 1;
        } else {
          e[i].ok = 1;
          e[i ^ 1].ok = -1;
          check[u] ^= 1;
        }
      } else if (visited[e[i].to] < visited[u]) {
        e[i].ok = 1;
        e[i ^ 1].ok = -1;
        check[u] ^= 1;
      }
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v;
    cin >> u >> v;
    add(u, v);
    add(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (!visited[i])
      dfs(i, 0);
  }
  for (int i = 1; i <= n; i++) {
    if (check[i]) {
      cout << "-1";
      return 0;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = h[i]; j; j = e[j].next) {
      if (e[j].ok == 1) {
        cout << i << " " << e[j].to << endl;
      }
    }
  }
}
Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 10005
int n, m, fa[N], out[N];
vector<int> g[N];
inline int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
inline void dfs(int x, int fa) {
  for (int i = 0; i < g[x].size(); i++) {
    int v = g[x][i];
    if (v == fa)
      continue;
    dfs(v, x);
    if (out[v] & 1)
      printf("%d %d\n", v, x);
    else {
      out[x]++;
      printf("%d %d\n", x, v);
    }
  }
  return;
}
int main() {
  scanf("%d%d", &n, &m);
  if (m & 1) {
    printf("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (Find(u) != Find(v)) {
      g[u].push_back(v);
      g[v].push_back(u);
      fa[Find(u)] = Find(v);
    } else {
      out[v]++;
      printf("%d %d\n", v, u);
    }
  }
  dfs(1, 0);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int n, m, fa[N], out[N];
vector<int> g[N];
inline int Find(int x) { return x == fa[x] ? x : fa[x] = Find(fa[x]); }
inline void dfs(int x, int fa) {
  for (int i = 0; i < g[x].size(); i++) {
    int v = g[x][i];
    if (v == fa)
      continue;
    dfs(v, x);
    if (out[v] & 1)
      printf("%d %d\n", v, x);
    else {
      out[x]++;
      printf("%d %d\n", x, v);
    }
  }
  return;
}
int main() {
  scanf("%d%d", &n, &m);
  if (m & 1) {
    printf("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    if (Find(u) != Find(v)) {
      g[u].push_back(v);
      g[v].push_back(u);
      fa[Find(u)] = Find(v);
    } else {
      out[v]++;
      printf("%d %d\n", v, u);
    }
  }
  dfs(1, 0);
  return 0;
}
Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 1e5 + 8;

vector<int> g[N];

int c[N];
int used[N];
set<pair<int, int>> ans;

void inv(int u, int v) {
  if (ans.count({u, v})) {
    ans.erase({u, v});
    ans.insert({v, u});
  } else if (ans.count({v, u})) {
    ans.erase({v, u});
    ans.insert({u, v});
  } else {
    assert(false);
  }
}

int dfs(int cur) {
  int cv = 0;
  used[cur] = 1;
  for (auto t : g[cur]) {
    if (!used[t]) {
      int v = dfs(t);
      if (v & 1)
        inv(cur, v);
      cv += v;
    }
  }
  cv += c[cur];
  return cv;
}

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

  int n, m;
  cin >> n >> m;
  if (m & 1) {
    cout << -1 << endl;
    return 0;
  }
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    c[u] ^= 1;
    ans.insert({u, v});
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0);
  for (auto t : ans) {
    cout << t.first + 1 << ' ' << t.second + 1 << '\n';
  }
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

const int N = 1e5 + 8;

vector<int> g[N];

int c[N];
int used[N];
set<pair<int, int>> ans;

void inv(int u, int v) {
  if (ans.count({u, v})) {
    ans.erase({u, v});
    ans.insert({v, u});
  } else if (ans.count({v, u})) {
    ans.erase({v, u});
    ans.insert({u, v});
  } else {
    assert(false);
  }
}

int dfs(int cur) {
  int cv = 0;
  used[cur] = 1;
  for (auto t : g[cur]) {
    if (!used[t]) {
      int v = dfs(t);
      if (v & 1)
        inv(cur, t);
      cv += v;
    }
  }
  cv += c[cur];
  return cv;
}

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

  int n, m;
  cin >> n >> m;
  if (m & 1) {
    cout << -1 << endl;
    return 0;
  }
  while (m--) {
    int u, v;
    cin >> u >> v;
    u--;
    v--;
    c[u] ^= 1;
    ans.insert({u, v});
    g[u].push_back(v);
    g[v].push_back(u);
  }
  dfs(0);
  for (auto t : ans) {
    cout << t.first + 1 << ' ' << t.second + 1 << '\n';
  }
}
Problem p02976 (C++)

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

const int maxn = 1e5 + 10;

pair<int, int> edge[maxn];
int taken[maxn], deg[maxn], ansu[maxn], ansv[maxn], fa[maxn], tot;
vector<int> adj[maxn];

int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

void dfs(int u, int parent) {
  for (auto v : adj[u]) {
    if (v != parent)
      dfs(v, u);
  }
  if (parent == -1)
    return;
  if (deg[u] & 1)
    ansu[tot] = u, ansv[tot] = parent, ++tot;
  else
    ansu[tot] = parent, ansv[tot] = u, deg[parent] ^= 1, ++tot;
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  if (m & 1) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < m; ++i) {
    cin >> edge[i].first >> edge[i].second;
  }
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  int root;
  try {
    for (int i = 0; i < m; ++i)
      if (find(edge[i].first) != find(edge[i].second)) {
        fa[edge[i].second] = find(edge[i].first);
        root = edge[i].first;
        adj[edge[i].first].push_back(edge[i].second);
        adj[edge[i].second].push_back(edge[i].first);
      } else {
        ansu[tot] = edge[i].first;
        ansv[tot] = edge[i].second;
        ++tot;
        deg[edge[i].first] ^= 1;
      }
  } catch (exception &e) {
    while (1)
      ;
  }
  dfs(root, -1);
  for (int i = 0; i < m; ++i)
    cout << ansu[i] << ' ' << ansv[i] << endl;
  return 0;
}


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

const int maxn = 1e5 + 10;

pair<int, int> edge[maxn];
int taken[maxn], deg[maxn], ansu[maxn], ansv[maxn], fa[maxn], tot;
vector<int> adj[maxn];

int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }

void dfs(int u, int parent) {
  for (auto v : adj[u]) {
    if (v != parent)
      dfs(v, u);
  }
  if (parent == -1)
    return;
  if (deg[u] & 1)
    ansu[tot] = u, ansv[tot] = parent, ++tot;
  else
    ansu[tot] = parent, ansv[tot] = u, deg[parent] ^= 1, ++tot;
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
#endif
  ios::sync_with_stdio(false);
  int n, m;
  cin >> n >> m;
  if (m & 1) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < m; ++i) {
    cin >> edge[i].first >> edge[i].second;
  }
  for (int i = 1; i <= n; ++i)
    fa[i] = i;
  int root;
  for (int i = 0; i < m; ++i)
    if (find(edge[i].first) != find(edge[i].second)) {
      fa[find(edge[i].second)] = find(edge[i].first);
      root = edge[i].first;
      adj[edge[i].first].push_back(edge[i].second);
      adj[edge[i].second].push_back(edge[i].first);
    } else {
      ansu[tot] = edge[i].first;
      ansv[tot] = edge[i].second;
      ++tot;
      deg[edge[i].first] ^= 1;
    }
  dfs(root, -1);
  for (int i = 0; i < m; ++i)
    cout << ansu[i] << ' ' << ansv[i] << endl;
  return 0;
}
Problem p02976 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"
using namespace std;

const int MAXN = 1e5 + 5;
#define ii pair<int, int>

int n, m;
int lvl[MAXN], p[MAXN], par[MAXN];
bool visited[MAXN];
vector<int> adj[MAXN];
vector<ii> e;

int getParent(int x) { return (p[x] == x) ? x : getParent(p[x]); }

void conn(int a, int b) {
  int pa = getParent(a), pb = getParent(b);
  if (pa != pb) {
    if (lvl[pa] < pb)
      p[pa] = pb;
    else
      p[pb] = pa;
    if (lvl[pa] == lvl[pb])
      lvl[pa]++;
  }
}

void dfs(int pos, int prev) {
  visited[pos] = true;
  for (int i : adj[pos]) {
    if (visited[i])
      continue;
    dfs(i, pos);
  }
  if (par[pos] == 1) {
    e.push_back(ii(pos, prev));
    par[pos] = 1 - par[pos];
  } else if (pos != 0) {
    e.push_back(ii(prev, pos));
    par[prev] = 1 - par[prev];
  }
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    p[i] = i;
    lvl[i] = 0;
    par[i] = 0;
  }
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    if (getParent(u) != getParent(v)) {
      conn(u, v);
      adj[u].push_back(v);
      adj[v].push_back(u);
    } else {
      e.push_back(ii(u, v));
      par[u] = 1 - par[u];
    }
  }
  dfs(0, -1);
  if (m % 2 == 1) {
    printf("-1\n");
  } else {
    for (ii i : e) {
      printf("%d %d\n", i.first + 1, i.second + 1);
    }
  }
}

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

const int MAXN = 1e5 + 5;
#define ii pair<int, int>

int n, m;
int lvl[MAXN], p[MAXN], par[MAXN];
bool visited[MAXN];
vector<int> adj[MAXN];
vector<ii> e;

int getParent(int x) { return (p[x] == x) ? x : getParent(p[x]); }

void conn(int a, int b) {
  int pa = getParent(a), pb = getParent(b);
  if (pa != pb) {
    if (lvl[pa] < lvl[pb])
      p[pa] = pb;
    else
      p[pb] = pa;
    if (lvl[pa] == lvl[pb])
      lvl[pa]++;
  }
}

void dfs(int pos, int prev) {
  visited[pos] = true;
  for (int i : adj[pos]) {
    if (visited[i])
      continue;
    dfs(i, pos);
  }
  if (par[pos] == 1) {
    e.push_back(ii(pos, prev));
    par[pos] = 1 - par[pos];
  } else if (pos != 0) {
    e.push_back(ii(prev, pos));
    par[prev] = 1 - par[prev];
  }
}

int main() {
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; ++i) {
    p[i] = i;
    lvl[i] = 0;
    par[i] = 0;
  }
  for (int i = 0; i < m; ++i) {
    int u, v;
    scanf("%d %d", &u, &v);
    u--;
    v--;
    if (getParent(u) != getParent(v)) {
      conn(u, v);
      adj[u].push_back(v);
      adj[v].push_back(u);
    } else {
      e.push_back(ii(u, v));
      par[u] = 1 - par[u];
    }
  }
  dfs(0, -1);
  if (m % 2 == 1) {
    printf("-1\n");
  } else {
    for (ii i : e) {
      printf("%d %d\n", i.first + 1, i.second + 1);
    }
  }
}
Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <vector>

using namespace std;

const int maxn = 2e5 + 7;
vector<int> g[maxn];
int xx[maxn], yy[maxn], len = 0;
int dep[maxn], in[maxn];
void dfs(int u, int pre) {
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v == pre)
      continue;
    if (dep[v] == 0) {
      dfs(v, u);
    } else if (dep[v] > dep[u]) {
      in[u]++;
      xx[++len] = u;
      yy[len] = v;
    }
  }
  if (in[u] & 1) {
    assert(pre != 0);
    len++;
    in[u]++;
    xx[len] = u;
    yy[len] = pre;
  } else {
    len++;
    in[pre]++;
    xx[len] = pre;
    yy[len] = u;
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  if (m & 1) {
    printf("-1\n");
    return 0;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    assert((in[i] & 1) == 0);
  for (int i = 1; i <= m; i++)
    printf("%d %d\n", xx[i], yy[i]);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <vector>

using namespace std;

const int maxn = 2e5 + 7;
vector<int> g[maxn];
int xx[maxn], yy[maxn], len = 0;
int dep[maxn], in[maxn];
void dfs(int u, int pre) {
  dep[u] = dep[pre] + 1;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (v == pre)
      continue;
    if (dep[v] == 0) {
      dfs(v, u);
    } else if (dep[v] > dep[u]) {
      in[u]++;
      xx[++len] = u;
      yy[len] = v;
    }
  }
  if (in[u] & 1) {
    assert(pre != 0);
    len++;
    in[u]++;
    xx[len] = u;
    yy[len] = pre;
  } else {
    len++;
    in[pre]++;
    xx[len] = pre;
    yy[len] = u;
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  if (m & 1) {
    printf("-1\n");
    return 0;
  }
  dfs(1, 0);
  for (int i = 1; i <= n; i++)
    assert((in[i] & 1) == 0);
  for (int i = 1; i <= m; i++)
    printf("%d %d\n", xx[i], yy[i]);
  return 0;
}
Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define LL long long
using namespace std;
int read() {
  bool f = 0;
  int x = 0;
  char c = getchar();
  while (c < '0' || '9' < c) {
    if (c == '-')
      f = 1;
    c = getchar();
  }
  while ('0' <= c && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return !f ? x : -x;
}
#define MAXN 100000
#define INF 0x3f3f3f3f
int fa[MAXN + 5];
vector<int> G[MAXN + 5];
int Find(int u) { return fa[u] == u ? u : fa[u] = Find(fa[u]); }
int out[MAXN + 5];
void DFS(int u, int f) {
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i];
    if (v == f)
      continue;
    DFS(v, u);
    if (out[v] & 1) {
      out[v]++;
      printf("%d %d\n", v, u);
    } else {
      out[u]++;
      printf("%d %d\n", u, v);
    }
  }
  return;
}
int main() {
  int n = read(), m = read();
  if (m & 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    if (Find(u) != Find(v)) {
      G[Find(u)].push_back(Find(v));
      G[Find(v)].push_back(Find(u));
    } else {
      out[u]++;
      printf("%d %d\n", u, v);
    }
  }
  DFS(1, 0);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define LL long long
using namespace std;
int read() {
  bool f = 0;
  int x = 0;
  char c = getchar();
  while (c < '0' || '9' < c) {
    if (c == '-')
      f = 1;
    c = getchar();
  }
  while ('0' <= c && c <= '9')
    x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();
  return !f ? x : -x;
}
#define MAXN 100000
#define INF 0x3f3f3f3f
int fa[MAXN + 5];
vector<int> G[MAXN + 5];
int Find(int u) { return fa[u] == u ? u : fa[u] = Find(fa[u]); }
int out[MAXN + 5];
void DFS(int u, int f) {
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i];
    if (v == f)
      continue;
    DFS(v, u);
    if (out[v] & 1) {
      out[v]++;
      printf("%d %d\n", v, u);
    } else {
      out[u]++;
      printf("%d %d\n", u, v);
    }
  }
  return;
}
int main() {
  int n = read(), m = read();
  if (m & 1) {
    puts("-1");
    return 0;
  }
  for (int i = 1; i <= n; i++)
    fa[i] = i;
  for (int i = 1; i <= m; i++) {
    int u = read(), v = read();
    if (Find(u) != Find(v)) {
      G[u].push_back(v);
      G[v].push_back(u);
      fa[Find(u)] = Find(v);
    } else {
      out[u]++;
      printf("%d %d\n", u, v);
    }
  }
  DFS(1, 0);
  return 0;
}
Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#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, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define rn return
#define fi first
#define se second
typedef std::pair<int, int> P;
using namespace std;
using ll = long long;

int main() {
  int N, M;
  cin >> N >> M;
  if (M % 2 == 1) {
    cout << "-1";
    rn 0;
  }
  vector<vector<int>> g(N);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[y].push_back(x);
    g[x].push_back(y);
  }

  vector<pair<int, int>> ans;
  vector<int> degree(N, 0);
  vector<int> depth(N, -1);

  function<void(int, int)> DFS = [&](int v, int pr) {
    for (int u : g[v]) {
      if (depth[u] == -1) {
        depth[u] = depth[v] + 1;
        DFS(u, v);
      } else if (depth[u] > depth[v]) {
        degree[v]++;
        ans.emplace_back(v, u);
      }
    }
    if (degree[v] % 2 == 1) {
      assert(pr != -1);
      ans.emplace_back(v, pr);
      degree[v]++;
    } else {
      assert(pr != -1);
      ans.emplace_back(pr, v);
      degree[pr]++;
    }
  };

  depth[0] = 0;
  DFS(0, -1);

  for (auto &p : ans) {
    cout << p.first + 1 << " " << p.second + 1 << endl;
  }
  rn 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#define maxs(x, y) x = max(x, y)
#define mins(x, y) x = min(x, y)
#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, i0, n) for (int(i) = (i0); (i) < (n); (i)++)
#define FORR(i, i0, n) for (int(i) = (n)-1; (i) >= (i0); (i)--)
#define rn return
#define fi first
#define se second
typedef std::pair<int, int> P;
using namespace std;
using ll = long long;

int main() {
  int N, M;
  cin >> N >> M;
  if (M % 2 == 1) {
    cout << "-1";
    rn 0;
  }
  vector<vector<int>> g(N);
  rep(i, M) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    g[y].push_back(x);
    g[x].push_back(y);
  }

  vector<pair<int, int>> ans;
  vector<int> degree(N, 0);
  vector<int> depth(N, -1);

  function<void(int, int)> DFS = [&](int v, int pr) {
    for (int u : g[v]) {
      if (depth[u] == -1) {
        depth[u] = depth[v] + 1;
        DFS(u, v);
      } else if (depth[u] > depth[v]) {
        degree[v]++;
        ans.emplace_back(v, u);
      }
    }
    if (degree[v] % 2 == 1) {
      assert(pr != -1);
      ans.emplace_back(v, pr);
      degree[v]++;
    } else {
      if (pr != -1) {
        ans.emplace_back(pr, v);
        degree[pr]++;
      }
    }
  };

  depth[0] = 0;
  DFS(0, -1);

  for (auto &p : ans) {
    cout << p.first + 1 << " " << p.second + 1 << endl;
  }
  rn 0;
}
Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  vector<int> D(N);
  vector<vector<int>> E(N);
  vector<vector<int>> EI(N);
  vector<int> C(N);
  for (int i = 0; i < M; i++) {
    E[A[i]].push_back(B[i]);
    E[B[i]].push_back(A[i]);
    EI[A[i]].push_back(i);
    EI[B[i]].push_back(i);
    C[A[i]] ^= 1;
  }

  int c = 0;
  for (int i = 0; i < N; i++)
    c += C[i];
  if (c % 2 != 0) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> PV;
  vector<int> PE;
  vector<int> V(N);
  function<void(int)> f = [&](int c) {
    PV.push_back(c);
    V[c] = true;

    for (int i = 0; i < (int)E[c].size(); i++)
      if (!V[E[c][i]]) {
        PE.push_back(EI[c][i]);
        f(E[c][i]);
        PE.push_back(EI[c][i]);
        PV.push_back(c);
      }
  };
  f(0);

  for (int i = 0; i < (int)PV.size() - 1;) {
    if (C[PV[i]] == 0) {
      i++;
    } else {
      while (C[PV[i]] != 0 && i < (int)PV.size() - 1) {
        C[PV[i]] ^= 1;
        C[PV[i + 1]] ^= 1;
        D[PE[i]] ^= 1;
        i++;
      }
    }
  }

  for (int i = 0; i < M; i++) {
    if (D[i] == 0)
      cout << A[i] + 1 << " " << B[i] + 1 << endl;
    else
      cout << B[i] + 1 << " " << A[i] + 1 << endl;
  }
}


Fixed submission:
#include <functional>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> A(M), B(M);
  for (int i = 0; i < M; i++) {
    cin >> A[i] >> B[i];
    A[i]--;
    B[i]--;
  }

  vector<int> D(M);
  vector<vector<int>> E(N);
  vector<vector<int>> EI(N);
  vector<int> C(N);
  for (int i = 0; i < M; i++) {
    E[A[i]].push_back(B[i]);
    E[B[i]].push_back(A[i]);
    EI[A[i]].push_back(i);
    EI[B[i]].push_back(i);
    C[A[i]] ^= 1;
  }

  int c = 0;
  for (int i = 0; i < N; i++)
    c += C[i];
  if (c % 2 != 0) {
    cout << -1 << endl;
    return 0;
  }

  vector<int> PV;
  vector<int> PE;
  vector<int> V(N);
  function<void(int)> f = [&](int c) {
    PV.push_back(c);
    V[c] = true;

    for (int i = 0; i < (int)E[c].size(); i++)
      if (!V[E[c][i]]) {
        PE.push_back(EI[c][i]);
        f(E[c][i]);
        PE.push_back(EI[c][i]);
        PV.push_back(c);
      }
  };
  f(0);

  for (int i = 0; i < (int)PV.size() - 1;) {
    if (C[PV[i]] == 0) {
      i++;
    } else {
      while (C[PV[i]] != 0 && i < (int)PV.size() - 1) {
        C[PV[i]] ^= 1;
        C[PV[i + 1]] ^= 1;
        D[PE[i]] ^= 1;
        i++;
      }
    }
  }

  for (int i = 0; i < M; i++) {
    if (D[i] == 0)
      cout << A[i] + 1 << " " << B[i] + 1 << endl;
    else
      cout << B[i] + 1 << " " << A[i] + 1 << endl;
  }
}