Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll MOD = 1000000007;
// const ll MOD=998244353;

template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.F >> A.S;
  return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.F << " " << A.S;
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
  ll i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}

ll N, M;
vector<vector<ll>> edge;
vector<ll> depth, cnt;
vector<pll> ans;

void dfs(ll u, ll p) {
  for (auto &v : edge[u]) {
    if (v == p) {
      continue;
    }
    if (depth[v] != -1) {
      if (depth[v] < depth[u]) {
        cnt[u]++;
        ans.push_back({u, v});
      }
      continue;
    }
    depth[v] = depth[u] + 1;
    dfs(v, u);
  }
  if (cnt[u] & 1) {
    assert(p != -1);
    cnt[u]++;
    ans.push_back({u, p});
  } else if (p != -1) {
    cnt[p]++;
    ans.push_back({p, u});
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  edge.resize(N);
  depth.resize(N, -1);
  cnt.resize(N);
  if (M & 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  dfs(0, -1);
  for (auto &I : ans) {
    I.F++;
    I.S++;
    cout << I << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long int ull;
typedef long long int ll;
typedef pair<ll, ll> pll;
typedef long double D;
typedef complex<D> P;
#define F first
#define S second
const ll MOD = 1000000007;
// const ll MOD=998244353;

template <typename T, typename U>
istream &operator>>(istream &i, pair<T, U> &A) {
  i >> A.F >> A.S;
  return i;
}
template <typename T> istream &operator>>(istream &i, vector<T> &A) {
  for (auto &I : A) {
    i >> I;
  }
  return i;
}
template <typename T, typename U>
ostream &operator<<(ostream &o, const pair<T, U> &A) {
  o << A.F << " " << A.S;
  return o;
}
template <typename T> ostream &operator<<(ostream &o, const vector<T> &A) {
  ll i = A.size();
  for (auto &I : A) {
    o << I << (--i ? " " : "");
  }
  return o;
}

ll N, M;
vector<vector<ll>> edge;
vector<ll> depth, cnt;
vector<pll> ans;

void dfs(ll u, ll p) {
  for (auto &v : edge[u]) {
    if (v == p) {
      continue;
    }
    if (depth[v] != -1) {
      if (depth[v] < depth[u]) {
        cnt[u]++;
        ans.push_back({u, v});
      }
      continue;
    }
    depth[v] = depth[u] + 1;
    dfs(v, u);
  }
  if (cnt[u] & 1) {
    assert(p != -1);
    cnt[u]++;
    ans.push_back({u, p});
  } else if (p != -1) {
    cnt[p]++;
    ans.push_back({p, u});
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N >> M;
  edge.resize(N);
  depth.resize(N, -1);
  cnt.resize(N);
  if (M & 1) {
    cout << -1 << endl;
    return 0;
  }
  for (int i = 0; i < M; i++) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  depth[0] = 0;
  dfs(0, -1);
  for (auto &I : ans) {
    I.F++;
    I.S++;
    cout << I << endl;
  }

  return 0;
}