Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\t";
  }
  return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int M = 100001;

vector<int> graph[M];
int par[M];
int visit[M];
int out[M];        // 出て行く辺の本数
vector<int> order; // 親子の順
set<ll> dir;

void bfs(int start) {
  queue<int> remain;
  remain.push(start);
  visit[start] = 1;
  while (remain.size() > 0) {
    ll from = remain.front();
    remain.pop();
    order.push_back(from);
    for (ll to : graph[from]) {
      if (visit[to] != 0) {
        if (par[from] == to)
          continue;
        if (dir.find(to * M + from) != dir.end())
          continue;
        dir.insert(from * M + to);
        out[from] += 1;
        continue;
      }
      remain.push(to);
      par[to] = from;
      visit[to] = 1;
    }
  }
}

int main() {
  memset(par, 0, sizeof(par));
  memset(visit, 0, sizeof(visit));
  memset(out, 0, sizeof(out));

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int from, to;
    cin >> from >> to;
    graph[from].push_back(to);
    graph[to].push_back(from);
  }
  if (m % 2 == 1) {
    cout << -1 << endl;
    return 0;
  }
  bfs(1);
  for (int i = m - 1; i > 0; --i) {
    ll v = order[i];
    ll p = par[v];
    if (out[v] % 2 == 0) {
      dir.insert(p * M + v);
      out[p] += 1;
    } else {
      dir.insert(v * M + p);
      out[v] += 1;
    }
  }
  // cout << order << endl;
  // for (int i = 1; i <= m; ++i) {
  //   cout << par[i] << " " << visit[i] << " " << out[i] << " " << order[i] <<
  //   endl;
  // }
  for (ll e : dir) {
    cout << (int)(e / M) << " " << (int)(e % M) << endl;
  }
}


Fixed submission:
#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// vector
template <typename T> ostream &operator<<(ostream &s, const vector<T> &v) {
  int len = v.size();
  for (int i = 0; i < len; ++i) {
    s << v[i];
    if (i < len - 1)
      s << "\t";
  }
  return s;
}
// 2 dimentional vector
template <typename T>
ostream &operator<<(ostream &s, const vector<vector<T>> &vv) {
  int len = vv.size();
  for (int i = 0; i < len; ++i) {
    s << vv[i] << endl;
  }
  return s;
}

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

const int M = 100001;

vector<int> graph[M];
int par[M];
int visit[M];
int out[M];        // 出て行く辺の本数
vector<int> order; // 親子の順
set<ll> dir;

void bfs(int start) {
  queue<int> remain;
  remain.push(start);
  visit[start] = 1;
  while (remain.size() > 0) {
    ll from = remain.front();
    remain.pop();
    order.push_back(from);
    for (ll to : graph[from]) {
      if (visit[to] != 0) {
        if (par[from] == to)
          continue;
        if (dir.find(to * M + from) != dir.end())
          continue;
        dir.insert(from * M + to);
        out[from] += 1;
        continue;
      }
      remain.push(to);
      par[to] = from;
      visit[to] = 1;
    }
  }
}

int main() {
  memset(par, 0, sizeof(par));
  memset(visit, 0, sizeof(visit));
  memset(out, 0, sizeof(out));

  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int from, to;
    cin >> from >> to;
    graph[from].push_back(to);
    graph[to].push_back(from);
  }
  if (m % 2 == 1) {
    cout << -1 << endl;
    return 0;
  }
  bfs(1);
  for (int i = n - 1; i > 0; --i) {
    ll v = order[i];
    ll p = par[v];
    if (out[v] % 2 == 0) {
      dir.insert(p * M + v);
      out[p] += 1;
    } else {
      dir.insert(v * M + p);
      out[v] += 1;
    }
  }
  // cout << order << endl;
  // for (int i = 1; i <= m; ++i) {
  //   cout << par[i] << " " << visit[i] << " " << out[i] << " " << order[i] <<
  //   endl;
  // }
  for (ll e : dir) {
    cout << (int)(e / M) << " " << (int)(e % M) << endl;
  }
}