Problem p02976 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define BUF 1e5
#define INF 1 << 30
constexpr ll mod = 1e9 + 7;
using namespace std;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

void dfs(int node, vector<vector<int>> &tree, vector<int> &parent,
         vector<int> &out) {
  int p = parent[node];
  if (tree[node].size() == 1) {
    if (out[node] == 0) {
      cout << p + 1 << " " << node + 1 << endl;
      out[p] = (++out[p]) % 2;
    } else {
      cout << node + 1 << " " << p + 1 << endl;
      out[node] = (++out[node]) % 2;
    }
  } else {
    for (int adj : tree[node]) {
      if (adj == p)
        continue;
      dfs(adj, tree, parent, out);
    }
    if (node != 0) {
      if (out[node] == 0) {
        cout << p + 1 << " " << node + 1 << endl;
        out[p] = (++out[p]) % 2;
      } else {
        cout << node + 1 << " " << p + 1 << endl;
        out[node] = (++out[node]) % 2;
      }
    }
  }
}

int main() {
  cin >> N >> M;
  vector<vector<int>> g(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  if (M % 2 != 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> flag(N, 0);
  vector<int> out(N, 0);
  vector<vector<int>> tree(N);
  vector<int> parent(N);
  set<pair<int, int>> set;
  queue<int> q;
  q.push(0);
  parent[0] = -1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    int p = parent[now];
    for (int adj : g[now]) {
      if (adj == p)
        continue;
      if (flag[adj]) {
        set.insert({min(now, adj), max(now, adj)});
        continue;
      }
      flag[adj] = 1;
      q.push(adj);
      parent[adj] = now;
      tree[now].push_back(adj);
      tree[adj].push_back(now);
    }
  }
  for (auto edge : set) {
    int u = edge.first;
    int v = edge.second;
    cout << u + 1 << " " << v + 1 << endl;
    out[u] = (++out[u]) % 2;
  }
  dfs(0, tree, parent, out);
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define BUF 1e5
#define INF 1 << 30
constexpr ll mod = 1e9 + 7;
using namespace std;
ll A, B, C, D, G, H, N, M, L, K, P, Q, R, W, X, Y, Z;
string S, T;
ll ans = 0;

void dfs(int node, vector<vector<int>> &tree, vector<int> &parent,
         vector<int> &out) {
  int p = parent[node];
  if (tree[node].size() == 1 && node != 0) {
    if (out[node] == 0) {
      cout << p + 1 << " " << node + 1 << endl;
      out[p] = (++out[p]) % 2;
    } else {
      cout << node + 1 << " " << p + 1 << endl;
      out[node] = (++out[node]) % 2;
    }
  } else {
    for (int adj : tree[node]) {
      if (adj == p)
        continue;
      dfs(adj, tree, parent, out);
    }
    if (node != 0) {
      if (out[node] == 0) {
        cout << p + 1 << " " << node + 1 << endl;
        out[p] = (++out[p]) % 2;
      } else {
        cout << node + 1 << " " << p + 1 << endl;
        out[node] = (++out[node]) % 2;
      }
    }
  }
}

int main() {
  cin >> N >> M;
  vector<vector<int>> g(N);
  for (int i = 0; i < M; i++) {
    int u, v;
    cin >> u >> v;
    u--, v--;
    g[u].push_back(v);
    g[v].push_back(u);
  }

  if (M % 2 != 0) {
    cout << -1 << endl;
    return 0;
  }
  vector<int> flag(N, 0);
  vector<int> out(N, 0);
  vector<vector<int>> tree(N);
  vector<int> parent(N);
  set<pair<int, int>> set;
  queue<int> q;
  q.push(0);
  parent[0] = -1;
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    int p = parent[now];
    for (int adj : g[now]) {
      if (adj == p)
        continue;
      if (flag[adj]) {
        set.insert({min(now, adj), max(now, adj)});
        continue;
      }
      flag[adj] = 1;
      q.push(adj);
      parent[adj] = now;
      tree[now].push_back(adj);
      tree[adj].push_back(now);
    }
  }
  for (auto edge : set) {
    int u = edge.first;
    int v = edge.second;
    cout << u + 1 << " " << v + 1 << endl;
    out[u] = (++out[u]) % 2;
  }
  dfs(0, tree, parent, out);
}