Problem p02776 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;
bool vis[100010];
int par[100010];
vector<pair<int, int>> G[100010];
vector<int> a, b, c, ans;
int dfs(int s, int p) {
  vis[s] = true;
  int cnt = 0;
  for (auto x : G[s]) {
    if (vis[x.first])
      continue;
    par[x.first] = x.second;
    (cnt += dfs(x.first, s)) &= 1;
  }
  if (c[s] != cnt) {
    ans.push_back(par[s]);
    return 1;
  }
  return 0;
}

vector<pair<int, int>> p;
int main() {
  int i, n, m;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    p.push_back({x, y});
  }
  sort(p.begin(), p.end());
  for (i = 0; i < n; i++) {
    a.push_back(p[i].first);
    b.push_back(p[i].second);
  }
  c.push_back(b[0]);
  int s = c[0];
  for (i = 1; i < n; i++) {
    c.push_back((s + b[i]) & 1);
    (s += c[i]) &= 1;
  }
  c.push_back(s & 1);
  for (i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    auto it1 = lower_bound(a.begin(), a.end(), l);
    auto it2 = lower_bound(a.begin(), a.end(), r);
    int x = it1 - a.begin(), y = it2 - a.begin();
    if (x == y)
      continue;
    G[x].push_back({y, i});
    G[y].push_back({x, i});
  }
  for (i = 0; i <= n; i++) {
    vis[i] = false;
  }
  for (i = 0; i <= n; i++) {
    if (vis[i])
      continue;
    par[i] = -1;
    dfs(i, -1);
  }
  sort(ans.begin(), ans.end());
  if (ans[0] == -1) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans.size() << endl;
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i] + 1 << " ";
  }
  cout << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <utility>
#include <vector>
using namespace std;
bool vis[100010];
int par[100010];
vector<pair<int, int>> G[100010];
vector<int> a, b, c, ans;
int dfs(int s, int p) {
  vis[s] = true;
  int cnt = 0;
  for (auto x : G[s]) {
    if (vis[x.first])
      continue;
    par[x.first] = x.second;
    (cnt += dfs(x.first, s)) &= 1;
  }
  if (c[s] != cnt) {
    ans.push_back(par[s]);
    return 1;
  }
  return 0;
}

vector<pair<int, int>> p;
int main() {
  int i, n, m;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    p.push_back({x, y});
  }
  sort(p.begin(), p.end());
  for (i = 0; i < n; i++) {
    a.push_back(p[i].first);
    b.push_back(p[i].second);
  }
  c.push_back(b[0]);
  int s = c[0];
  for (i = 1; i < n; i++) {
    c.push_back((s + b[i]) & 1);
    (s += c[i]) &= 1;
  }
  c.push_back(s & 1);
  for (i = 0; i < m; i++) {
    int l, r;
    cin >> l >> r;
    l--;
    auto it1 = lower_bound(a.begin(), a.end(), l);
    auto it2 = lower_bound(a.begin(), a.end(), r);
    int x = it1 - a.begin(), y = it2 - a.begin();
    if (x == y)
      continue;
    G[x].push_back({y, i});
    G[y].push_back({x, i});
  }
  for (i = 0; i <= n; i++) {
    vis[i] = false;
  }
  for (i = 0; i <= n; i++) {
    if (vis[i])
      continue;
    par[i] = -1;
    dfs(i, -1);
  }
  if (ans.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  sort(ans.begin(), ans.end());
  if (ans[0] == -1) {
    cout << -1 << endl;
    return 0;
  }
  cout << ans.size() << endl;
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i] + 1 << " ";
  }
  cout << endl;
}