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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

vector<pair<int, int>> place;
vector<vector<int>> x(100010), y(100010);
vector<int> groupnum(40010, -1);
vector<int> groupsize;
vector<vector<bool>> isclosetowall;
vector<bool> ischecked_x(100010, false), ischecked_y(100010, false);

int main() {
  int n, w, h;
  cin >> n >> w >> h;
  // cout << __LINE__ <<endl;
  for (int i = 0; i < n; i++) {
    int tmpx, tmpy;
    cin >> tmpx >> tmpy;
    place.push_back(make_pair(tmpx, tmpy));
    x[tmpx].push_back(i);
    // cout << y.size() << " " << tmpy << endl;
    y[tmpy].push_back(i);
    // cout << __LINE__ << endl;
    // cout << i << endl;
  }
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (groupnum[i] < 0) {
      // cout << i << endl;
      queue<int> qu;
      qu.push(i);
      groupnum[i] = cnt;
      isclosetowall.push_back(vector<bool>(4, false));
      groupsize.push_back(1);
      while (!qu.empty()) {
        int now = qu.front();
        int nowx = place[now].first;
        int nowy = place[now].second;
        qu.pop();
        // cout << now << " " << " " << nowx << " " << nowy << " " <<
        // ischecked_x[nowx] << " " << ischecked_y[nowy] << endl;
        if (nowx == 1)
          isclosetowall[cnt][0] = true;
        if (nowx == w)
          isclosetowall[cnt][1] = true;
        if (nowy == 1)
          isclosetowall[cnt][2] = true;
        if (nowy == h)
          isclosetowall[cnt][3] = true;
        if (!ischecked_x[nowx]) {
          ischecked_x[nowx] = true;
          for (int j = 0; j < x[nowx].size(); j++) {
            int nowindex = x[nowx][j];
            int tmpy = place[nowindex].second;
            if (nowindex == now || ischecked_y[tmpy])
              continue;
            groupnum[nowindex] = cnt;
            groupsize[cnt]++;
            qu.push(nowindex);
          }
        }
        // x.erase(x.begin() + nowx);
        if (!ischecked_y[nowy]) {
          ischecked_y[nowy] = true;
          for (int j = 0; j < y[nowy].size(); j++) {
            int nowindex = y[nowy][j];
            int tmpx = place[nowindex].first;
            if (nowindex == now || ischecked_x[tmpx])
              continue;
            groupnum[nowindex] = cnt;
            groupsize[cnt]++;
            qu.push(nowindex);
          }
        }
        // y.erase(y.begin() + nowy);
        // cout << now << " " << groupsize[cnt] << endl;
      }
      cnt++;
    }
    // cout << i << " " << groupnum[i] << " " << groupsize[groupnum[i]] << endl;
  }
  int sum = 0;
  for (int i = 0; i < cnt; i++) {
    sum += groupsize[i] - 1;
  }
  if (cnt == 1) {
    cout << sum << endl;
    return 0;
  }
  int ans = mod;
  for (int i = 0; i < 4; i++) {
    int needtomove = 0;
    for (int j = 0; j < cnt; j++) {
      if (!isclosetowall[j][i])
        needtomove++;
    }
    // cout << sum + needtomove + cnt - 1 << endl;
    ans = min(ans, sum + needtomove + cnt - 1);
  }
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <memory.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define mod 1000000007

int main() {
  int n, w, h;
  cin >> n >> w >> h;
  vector<pair<int, int>> place;
  vector<vector<int>> x(40001), y(40001);
  for (int i = 0; i < n; i++) {
    int tmpx, tmpy;
    cin >> tmpx >> tmpy;
    place.push_back(make_pair(tmpx, tmpy));
    x[tmpx].push_back(i);
    y[tmpy].push_back(i);
  }
  vector<int> groupnum(40000, -1);
  vector<int> groupsize;
  vector<vector<bool>> isclosetowall;
  vector<bool> ischecked_x(100000, false), ischecked_y(100000, false);
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (groupnum[i] < 0) {
      // cout << i << endl;
      queue<int> qu;
      qu.push(i);
      groupnum[i] = cnt;
      isclosetowall.push_back(vector<bool>(4, false));
      groupsize.push_back(1);
      while (!qu.empty()) {
        int now = qu.front();
        int nowx = place[now].first;
        int nowy = place[now].second;
        qu.pop();
        // cout << now << " " << " " << nowx << " " << nowy << " " <<
        // ischecked_x[nowx] << " " << ischecked_y[nowy] << endl;
        if (nowx == 1)
          isclosetowall[cnt][0] = true;
        if (nowx == h)
          isclosetowall[cnt][1] = true;
        if (nowy == 1)
          isclosetowall[cnt][2] = true;
        if (nowy == w)
          isclosetowall[cnt][3] = true;
        if (!ischecked_x[nowx]) {
          ischecked_x[nowx] = true;
          for (int j = 0; j < x[nowx].size(); j++) {
            int nowindex = x[nowx][j];
            int tmpy = place[nowindex].second;
            if (nowindex == now || ischecked_y[tmpy])
              continue;
            groupnum[nowindex] = cnt;
            groupsize[cnt]++;
            qu.push(nowindex);
          }
        }
        // x.erase(x.begin() + nowx);
        if (!ischecked_y[nowy]) {
          ischecked_y[nowy] = true;
          for (int j = 0; j < y[nowy].size(); j++) {
            int nowindex = y[nowy][j];
            int tmpx = place[nowindex].first;
            if (nowindex == now || ischecked_x[tmpx])
              continue;
            groupnum[nowindex] = cnt;
            groupsize[cnt]++;
            qu.push(nowindex);
          }
        }
        // y.erase(y.begin() + nowy);
        // cout << now << " " << groupsize[cnt] << endl;
      }
      cnt++;
    }
    // cout << i << " " << groupnum[i] << groupsize[groupnum[i]] << endl;
  }
  int sum = 0;
  for (int i = 0; i < cnt; i++) {
    sum += groupsize[i] - 1;
  }
  if (cnt == 1) {
    cout << sum << endl;
    return 0;
  }
  int ans = mod;
  for (int i = 0; i < 4; i++) {
    int needtomove = 0;
    for (int j = 0; j < cnt; j++) {
      if (!isclosetowall[j][i])
        needtomove++;
    }
    ans = min(ans, sum + needtomove + cnt - 1);
  }
  cout << ans << endl;
}