Problem p01526 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 200010

int n;
double r;
vector<pair<int, int>> vec;
set<pair<int, int>> visited;
set<pair<int, int>> exist;

int main() {

  scanf("%d%lf", &n, &r);

  for (int i = 0; i < n; i++) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    if (x >= 0)
      x += r;
    if (y >= 0)
      y += r;
    vec.push_back({x / r, y / r});
    exist.insert(vec[i]);
  }

  int ans = 0;

  int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
  int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

  for (int i = 0; i < n; i++) {
    if (visited.find(vec[i]) != visited.end())
      continue;

    ans++;

    queue<pair<int, int>> que;
    que.push(vec[i]);
    while (que.size()) {
      auto p = que.front();
      que.pop();

      if (visited.find(p) != visited.end())
        continue;
      visited.insert(p);

      for (int j = 0; j < 8; j++) {
        if (exist.find({p.first + dx[j], p.second + dy[j]}) != exist.end())
          que.push({p.first + dx[j], p.second + dy[j]});
      }
    }
  }

  cout << ans << endl;

  return 0;
}


Error: 0
1 1
1 1
1 0
1 4
1 -3


Fixed submission:

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <cassert>
#include <functional>

typedef long long ll;
using namespace std;

#define debug(x) cerr << __LINE__ << " : " << #x << " = " << (x) << endl;

#define mod 1000000007              // 1e9+7(prime number)
#define INF 1000000000              // 1e9
#define LLINF 2000000000000000000LL // 2e18
#define SIZE 100010

int main() {
  int n, r;
  vector<pair<int, int>> vec;
  set<pair<int, int>> visited;
  set<pair<int, int>> exist;

  scanf("%d%d", &n, &r);

  for (int i = 0; i < n; i++) {
    double x, y;
    scanf("%lf%lf", &x, &y);
    if (x >= 0)
      x += r;
    if (y >= 0)
      y += r;
    vec.push_back({x / r, y / r});
    exist.insert(vec[i]);
    cerr << vec[i].first << " " << vec[i].second << endl;
  }

  int ans = 0;

  int dx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
  int dy[8] = {1, -1, 0, 0, 1, -1, 1, -1};

  for (int i = 0; i < n; i++) {
    if (visited.find(vec[i]) != visited.end())
      continue;

    ans++;

    queue<pair<int, int>> que;
    que.push(vec[i]);
    while (que.size()) {
      auto p = que.front();
      que.pop();

      if (visited.find(p) != visited.end())
        continue;
      visited.insert(p);

      for (int j = 0; j < 8; j++) {
        if (exist.find({p.first + dx[j], p.second + dy[j]}) != exist.end())
          que.push({p.first + dx[j], p.second + dy[j]});
      }
    }
  }

  cout << ans << endl;

  return 0;
}