Problem p02776 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 100005, INF = 1 << 30;
typedef pair<int, pair<int, int>> P;

struct dat {
  int l;
  int r;
  int id;
};

bool ans[MAX];
bool need[MAX];
bool visited[MAX];
pair<int, int> edge[MAX];

bool ok = true;

vector<pair<int, int>> G[MAX];

void DFS(int u, int p, int e) {
  visited[u] = 1;

  for (auto to : G[u]) {
    if (to.fi == p)
      continue;
    if (visited[to.fi])
      continue;
    DFS(to.fi, u, to.se);
  }

  if (need[u]) {
    if (e == -1)
      ok = false;
    else {
      ans[e] = 1;
      need[edge[e].fi] ^= 1;
      need[edge[e].se] ^= 1;
    }
  }
}

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i].fi >> S[i].se;
  }
  sort(all(S));
  S.push_back({INF, INF});
  vector<dat> T(M);
  for (int i = 0; i < M; i++) {
    cin >> T[i].l >> T[i].r;
    auto it = lower_bound(all(S), make_pair(T[i].l, -1));
    T[i].l = it - S.begin();

    it = lower_bound(all(S), make_pair(T[i].r, INF));
    T[i].r = it - S.begin();
    T[i].id = i;

    edge[i] = {T[i].l, T[i].r};

    if (T[i].l == T[i].r)
      continue;

    G[T[i].l].push_back({T[i].r, i});
    G[T[i].r].push_back({T[i].l, i});
  }

  for (int i = 0; i < N; i++) {
    if (i == 0)
      need[i] = S[i].se;
    else {
      need[i] = S[i - 1].se ^ S[i].se;
    }
    if (i == N - 1)
      need[N] = S[i].se;
  }

  sort(all(T), [](auto a, auto b) {
    return make_pair(a.l, a.r) < make_pair(b.l, b.r);
  });

  for (int i = 0; i < N; i++) {
    if (!visited[i])
      DFS(i, -1, -1);
  }

  if (!ok)
    cout << -1 << endl;
  else {
    int cnt = 0;
    for (int i = 0; i < M; i++)
      if (ans[i])
        cnt++;
    cout << cnt << endl;
    for (int i = 0; i < M; i++)
      if (ans[i])
        cout << i + 1 << " ";
    cout << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
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;
}
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define si(x) int(x.size())
const int mod = 1000000007, MAX = 200005, INF = 1 << 30;
typedef pair<int, pair<int, int>> P;

struct dat {
  int l;
  int r;
  int id;
};

bool ans[MAX];
bool need[MAX];
bool visited[MAX];
pair<int, int> edge[MAX];

bool ok = true;

vector<pair<int, int>> G[MAX];

void DFS(int u, int p, int e) {
  visited[u] = 1;

  for (auto to : G[u]) {
    if (to.fi == p)
      continue;
    if (visited[to.fi])
      continue;
    DFS(to.fi, u, to.se);
  }

  if (need[u]) {
    if (e == -1)
      ok = false;
    else {
      ans[e] = 1;
      need[edge[e].fi] ^= 1;
      need[edge[e].se] ^= 1;
    }
  }
}

int main() {

  std::ifstream in("text.txt");
  std::cin.rdbuf(in.rdbuf());
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;
  vector<pair<int, int>> S(N);
  for (int i = 0; i < N; i++) {
    cin >> S[i].fi >> S[i].se;
  }
  sort(all(S));
  S.push_back({INF, INF});
  vector<dat> T(M);
  for (int i = 0; i < M; i++) {
    cin >> T[i].l >> T[i].r;
    auto it = lower_bound(all(S), make_pair(T[i].l, -1));
    T[i].l = it - S.begin();

    it = lower_bound(all(S), make_pair(T[i].r, INF));
    T[i].r = it - S.begin();
    T[i].id = i;

    edge[i] = {T[i].l, T[i].r};

    if (T[i].l == T[i].r)
      continue;

    G[T[i].l].push_back({T[i].r, i});
    G[T[i].r].push_back({T[i].l, i});
  }

  for (int i = 0; i < N; i++) {
    if (i == 0)
      need[i] = S[i].se;
    else {
      need[i] = S[i - 1].se ^ S[i].se;
    }
    if (i == N - 1)
      need[N] = S[i].se;
  }

  sort(all(T), [](auto a, auto b) {
    return make_pair(a.l, a.r) < make_pair(b.l, b.r);
  });

  for (int i = 0; i < N; i++) {
    if (!visited[i])
      DFS(i, -1, -1);
  }

  if (!ok)
    cout << -1 << endl;
  else {
    int cnt = 0;
    for (int i = 0; i < M; i++)
      if (ans[i])
        cnt++;
    cout << cnt << endl;
    for (int i = 0; i < M; i++)
      if (ans[i])
        cout << i + 1 << " ";
    cout << endl;
  }
}