Problem p02565 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N, D;
  cin >> N >> D;
  vector<int> X(N), Y(N);
  for (int i = 0; i < N; ++i)
    cin >> X[i] >> Y[i];

  vector<vector<int>> graph(N * 2), rev(N * 2);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < i; ++j) {
      if (abs(X[i] - X[j]) < D) {
        graph[i * 2 + 1].emplace_back(j * 2);
        graph[j * 2 + 1].emplace_back(i * 2);
        rev[i * 2].emplace_back(j * 2 + 1);
        rev[j * 2].emplace_back(i * 2 + 1);
      }
      if (abs(X[i] - Y[j]) < D) {
        graph[i * 2 + 1].emplace_back(j * 2 + 1);
        graph[j * 2].emplace_back(i * 2);
        rev[i * 2].emplace_back(j * 2);
        rev[j * 2 + 1].emplace_back(i * 2 + 1);
      }
      if (abs(Y[i] - X[j]) < D) {
        graph[i * 2].emplace_back(j * 2);
        graph[j * 2 + 1].emplace_back(i * 2 + 1);
        rev[i * 2 + 1].emplace_back(j * 2 + 1);
        rev[j * 2].emplace_back(i * 2);
      }
      if (abs(Y[i] - Y[j]) < D) {
        graph[i * 2].emplace_back(j * 2 + 1);
        graph[j * 2].emplace_back(i * 2 + 1);
        rev[i * 2 + 1].emplace_back(j * 2);
        rev[j * 2 + 1].emplace_back(i * 2);
      }
    }
  }

  vector<int> vs;
  vector<bool> used(N, false);
  auto dfs = [&](auto &&self, int cur) -> void {
    used[cur] = true;
    for (int nxt : graph[cur]) {
      if (!used[nxt])
        self(self, nxt);
    }
    vs.emplace_back(cur);
  };
  for (int v = 0; v < N * 2; ++v) {
    if (!used[v])
      dfs(dfs, v);
  }
  reverse(vs.begin(), vs.end());

  vector<int> scc_id(N * 2, -1);
  int K = 0;
  auto rdfs = [&](auto &&self, int cur) -> void {
    scc_id[cur] = K;
    for (int nxt : rev[cur]) {
      if (scc_id[nxt] == -1)
        self(self, nxt);
    }
  };
  for (int v : vs) {
    if (scc_id[v] == -1) {
      rdfs(rdfs, v);
      ++K;
    }
  }

  vector<bool> ans(N);
  for (int i = 0; i < N; ++i) {
    if (scc_id[i * 2] == scc_id[i * 2 + 1]) {
      cout << "No\n";
      return 0;
    }
    ans[i] = scc_id[i * 2] < scc_id[i * 2 + 1];
  }

  cout << "Yes\n";
  for (int i = 0; i < N; ++i)
    cout << ((ans[i] ? X[i] : Y[i])) << '\n';
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int N, D;
  cin >> N >> D;
  vector<int> X(N), Y(N);
  for (int i = 0; i < N; ++i)
    cin >> X[i] >> Y[i];

  vector<vector<int>> graph(N * 2), rev(N * 2);
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < i; ++j) {
      if (abs(X[i] - X[j]) < D) {
        graph[i * 2 + 1].emplace_back(j * 2);
        graph[j * 2 + 1].emplace_back(i * 2);
        rev[i * 2].emplace_back(j * 2 + 1);
        rev[j * 2].emplace_back(i * 2 + 1);
      }
      if (abs(X[i] - Y[j]) < D) {
        graph[i * 2 + 1].emplace_back(j * 2 + 1);
        graph[j * 2].emplace_back(i * 2);
        rev[i * 2].emplace_back(j * 2);
        rev[j * 2 + 1].emplace_back(i * 2 + 1);
      }
      if (abs(Y[i] - X[j]) < D) {
        graph[i * 2].emplace_back(j * 2);
        graph[j * 2 + 1].emplace_back(i * 2 + 1);
        rev[i * 2 + 1].emplace_back(j * 2 + 1);
        rev[j * 2].emplace_back(i * 2);
      }
      if (abs(Y[i] - Y[j]) < D) {
        graph[i * 2].emplace_back(j * 2 + 1);
        graph[j * 2].emplace_back(i * 2 + 1);
        rev[i * 2 + 1].emplace_back(j * 2);
        rev[j * 2 + 1].emplace_back(i * 2);
      }
    }
  }

  vector<int> vs;
  vector<bool> used(N * 2, false);
  auto dfs = [&](auto &&self, int cur) -> void {
    used[cur] = true;
    for (int nxt : graph[cur]) {
      if (!used[nxt])
        self(self, nxt);
    }
    vs.emplace_back(cur);
  };
  for (int v = 0; v < N * 2; ++v) {
    if (!used[v])
      dfs(dfs, v);
  }
  reverse(vs.begin(), vs.end());

  vector<int> scc_id(N * 2, -1);
  int K = 0;
  auto rdfs = [&](auto &&self, int cur) -> void {
    scc_id[cur] = K;
    for (int nxt : rev[cur]) {
      if (scc_id[nxt] == -1)
        self(self, nxt);
    }
  };
  for (int v : vs) {
    if (scc_id[v] == -1) {
      rdfs(rdfs, v);
      ++K;
    }
  }

  vector<bool> ans(N);
  for (int i = 0; i < N; ++i) {
    if (scc_id[i * 2] == scc_id[i * 2 + 1]) {
      cout << "No\n";
      return 0;
    }
    ans[i] = scc_id[i * 2] < scc_id[i * 2 + 1];
  }

  cout << "Yes\n";
  for (int i = 0; i < N; ++i)
    cout << ((ans[i] ? X[i] : Y[i])) << '\n';
}