Problem p02976 (C++)

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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define start_routine() int begtime = clock();
#define end_routine()                                                          \
  int endtime = clock();                                                       \
  cerr << endl                                                                 \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms";                                                               \
  return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;

#define loop(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()

#define print(stuff) cout << stuff << endl
#define printc(stuff)                                                          \
  for (auto x : stuff)                                                         \
    cout << x << " ";                                                          \
  cout << endl;
#define printPrec(stuff) cout << fixed << setprecision(15) << stuff << endl;
#define debug(stuff) cout << #stuff << ": " << stuff << endl
#define debugc(stuff)                                                          \
  cout << #stuff << ": ";                                                      \
  for (auto x : stuff)                                                         \
    cout << x << " ";                                                          \
  cout << endl;
#define len length
#define ret0 return 0
#define ret return

#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fill(ar, x) memset(ar, x, sizeof ar)

#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>

#define inf (long long int)1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN 100001

vl adj[100005];
bool vis[100005];
ll cnt[100005];
bool edge[100005];
vl tree[100005];
ll ans[100005];
ll in[100005];
map<pll, ll> m1;
map<ll, pll> m2;

void dfs(ll s) {
  vis[s] = true;
  for (auto x : adj[s]) {
    if (vis[x] == false) {
      dfs(x);
      edge[m1[mp(x, s)]] = true;
      tree[x].pb(s), tree[s].pb(x);
    }
  }
}

void dfs2(ll s, ll e) {
  for (auto x : tree[s]) {
    if (x != e) {
      dfs2(x, s);
      if (cnt[x] % 2 == 0) {
        if (in[x] % 2 == 0) {
          in[s]++;
          ll rr = m1[{s, x}];
          pll pp = m2[rr];
          if (pp.fi == x)
            ans[rr] = 0;
          else if (pp.fi == s)
            ans[rr] = 1;
        } else {
          in[x]++;
          ll rr = m1[{x, s}];
          pll pp = m2[rr];
          if (pp.fi == x)
            ans[rr] = 1;
          else if (pp.fi == s)
            ans[rr] = 0;
        }
      } else {
        if (in[x] % 2 == 0) {
          in[x]++;
          ll rr = m1[{s, x}];
          pll pp = m2[rr];
          if (pp.fi == x)
            ans[rr] = 1;
          else if (pp.fi == s)
            ans[rr] = 0;
        } else {
          in[s]++;
          ll rr = m1[{x, s}];
          pll pp = m2[rr];
          if (pp.fi == x)
            ans[rr] = 0;
          else if (pp.fi == s)
            ans[rr] = 1;
        }
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ll n, m;
  cin >> n >> m;
  loop(i, 1, n + 1) vis[i] = false;
  loop(i, 1, n + 1) cnt[i] = 0;
  loop(i, 1, m + 1) {
    edge[i] = false;
    ans[i] = -1;
  }
  loop(i, 1, n + 1) {
    in[i] = 0;
    adj[i].clear();
    tree[i].clear();
  }
  m1.clear();
  m2.clear();
  loop(i, 1, m + 1) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v), adj[v].pb(u);
    m1[mp(u, v)] = m1[mp(v, u)] = i;
    m2[i] = mp(u, v);
  }

  dfs(1);
  loop(i, 1, m + 1) {
    if (edge[i] == false) {
      pll p = m2[i];
      ll x = p.fi, y = p.se;
      if (x < y)
        cnt[x]++;
      else
        cnt[y]++;
    }
  }

  dfs2(1, 0);

  loop(i, 1, m + 1) {
    if (edge[i] == false) {
      pll ppp = m2[i];
      if (ppp.fi < ppp.se) {
        ans[i] = 1;
        in[ppp.fi]++;
      } else {
        ans[i] = 0;
        in[ppp.se]++;
      }
    }
  }

  bool done = true;
  loop(i, 1, n + 1) {
    if (in[i] % 2 == 1) {
      done = false;
      break;
    }
  }
  if (done == true) {
    loop(i, 1, m + 1) {
      if (ans[i] == 0) {
        ll x = m2[i].fi, y = m2[i].se;
        cout << y << " " << x << endl;
      } else if (ans[i] == 1) {
        ll x = m2[i].fi, y = m2[i].se;
        cout << x << " " << y << endl;
      }
    }
  } else {
    cout << -1 << endl;
  }
}


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

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define start_routine() int begtime = clock();
#define end_routine()                                                          \
  int endtime = clock();                                                       \
  cerr << endl                                                                 \
       << "Time elapsed: " << (endtime - begtime) * 1000 / CLOCKS_PER_SEC      \
       << " ms";                                                               \
  return 0
#define speed() cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(false)
// #define exit(a, b) return cout << a, b;

#define loop(i, a, b) for (ll i = a; i < b; i++)
#define all(v) v.begin(), v.end()

#define print(stuff) cout << stuff << endl
#define printc(stuff)                                                          \
  for (auto x : stuff)                                                         \
    cout << x << " ";                                                          \
  cout << endl;
#define printPrec(stuff) cout << fixed << setprecision(15) << stuff << endl;
#define debug(stuff) cout << #stuff << ": " << stuff << endl
#define debugc(stuff)                                                          \
  cout << #stuff << ": ";                                                      \
  for (auto x : stuff)                                                         \
    cout << x << " ";                                                          \
  cout << endl;
#define len length
#define ret0 return 0
#define ret return

#define ll long long
#define ld long double
#define fi first
#define endl '\n'
#define se second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define fill(ar, x) memset(ar, x, sizeof ar)

#define vl vector<ll>
#define sl set<ll>
#define pll pair<ll, ll>
#define mll map<ll, ll>
#define pq priority_queue<ll>

#define inf (long long int)1e18
#define eps 0.000001
#define mod 1000000007
#define mod1 998244353
#define MAXN 100001

vl adj[100005];
bool vis[100005];
ll cnt[100005];
bool edge[100005];
vl tree[100005];
ll ans[100005];
ll in[100005];
map<pll, ll> m1;
map<ll, pll> m2;

void dfs(ll s) {
  vis[s] = true;
  for (auto x : adj[s]) {
    if (vis[x] == false) {
      dfs(x);
      edge[m1[mp(x, s)]] = true;
      tree[x].pb(s), tree[s].pb(x);
    }
  }
}

void dfs2(ll s, ll e) {
  for (auto x : tree[s]) {
    if (x != e) {
      dfs2(x, s);
      if (cnt[x] % 2 == 0) {
        if (in[x] % 2 == 0) {
          in[s]++;
          ll rr = m1[{s, x}];
          pll pp = m2[rr];
          if (pp.fi == x)
            ans[rr] = 0;
          else if (pp.fi == s)
            ans[rr] = 1;
        } else {
          in[x]++;
          ll rr = m1[{x, s}];
          pll pp = m2[rr];
          if (pp.fi == x)
            ans[rr] = 1;
          else if (pp.fi == s)
            ans[rr] = 0;
        }
      } else {
        if (in[x] % 2 == 0) {
          in[x]++;
          ll rr = m1[{s, x}];
          pll pp = m2[rr];
          if (pp.fi == x)
            ans[rr] = 1;
          else if (pp.fi == s)
            ans[rr] = 0;
        } else {
          in[s]++;
          ll rr = m1[{x, s}];
          pll pp = m2[rr];
          if (pp.fi == x)
            ans[rr] = 0;
          else if (pp.fi == s)
            ans[rr] = 1;
        }
      }
    }
  }
}

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

  ll n, m;
  cin >> n >> m;
  loop(i, 1, n + 1) vis[i] = false;
  loop(i, 1, n + 1) cnt[i] = 0;
  loop(i, 1, m + 1) {
    edge[i] = false;
    ans[i] = -1;
  }
  loop(i, 1, n + 1) {
    in[i] = 0;
    adj[i].clear();
    tree[i].clear();
  }
  m1.clear();
  m2.clear();
  loop(i, 1, m + 1) {
    ll u, v;
    cin >> u >> v;
    adj[u].pb(v), adj[v].pb(u);
    m1[mp(u, v)] = m1[mp(v, u)] = i;
    m2[i] = mp(u, v);
  }

  dfs(1);
  loop(i, 1, m + 1) {
    if (edge[i] == false) {
      pll p = m2[i];
      ll x = p.fi, y = p.se;
      if (x < y)
        cnt[x]++;
      else
        cnt[y]++;
    }
  }

  dfs2(1, 0);

  loop(i, 1, m + 1) {
    if (edge[i] == false) {
      pll ppp = m2[i];
      if (ppp.fi < ppp.se) {
        ans[i] = 1;
        in[ppp.fi]++;
      } else {
        ans[i] = 0;
        in[ppp.se]++;
      }
    }
  }

  bool done = true;
  loop(i, 1, n + 1) {
    if (in[i] % 2 == 1) {
      done = false;
      break;
    }
  }
  if (done == true) {
    loop(i, 1, m + 1) {
      if (ans[i] == 0) {
        ll x = m2[i].fi, y = m2[i].se;
        cout << y << " " << x << endl;
      } else if (ans[i] == 1) {
        ll x = m2[i].fi, y = m2[i].se;
        cout << x << " " << y << endl;
      }
    }
  } else {
    cout << -1 << endl;
  }
}