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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unistd.h>
#include <vector>
using namespace std;
#define MAX 110

// ものすごくアホだった(3回目)

int dxy[4] = {-1, 1, 0, 0};
int dd[] = {0, 1};
int N, a, b, ans[MAX][MAX] = {};
vector<int> R[MAX * MAX];

template <typename T> void remove(vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

inline bool is_out_of_range(int z) { return (z <= 0 || N < z); }

inline void hoge(int y, int x) {
  // cout<<y<<" "<<x<<endl;
  // sleep(2);
  // string hogehoge;
  // cin>>hogehoge;
  if (y == 1) {
    for (int z : R[ans[y][x - 1]]) {
      // cout<<z<<" -----"<<endl;
      if (R[z].size() == 4)
        continue;
      ans[y][x] = z;
    }
  } else {
    for (int z : R[ans[y - 1][x]]) {
      // cout<<z<<" -----"<<endl;
      ans[y][x] = z;
    }
  }
  // cout<<ans[y][x]<<" ans "<<endl;

  for (int i = 0; i < 4; i++) {
    int ny = y + dxy[i], nx = x + dxy[3 - i];
    if (is_out_of_range(ny) || is_out_of_range(nx))
      continue;
    if (ans[ny][nx] == 0)
      continue;
    for (int i = 0; i < R[ans[ny][nx]].size(); i++) {
      if (R[ans[ny][nx]][i] == ans[y][x])
        remove(R[ans[ny][nx]], i);
    }
    for (int i = 0; i < R[ans[y][x]].size(); i++) {
      if (R[ans[y][x]][i] == ans[ny][nx])
        remove(R[ans[y][x]], i);
    }

    // R[ans[ny][nx]].erase(ans[y][x]);
    // R[ans[y][x]].erase(ans[ny][nx]);
  }
}

int huga() {
  queue<pair<int, int>> Q;

  Q.push(make_pair(1, 1));

  while (!Q.empty()) {
    pair<int, int> P = Q.front();
    Q.pop(); // string hogehoge; cin>>hogehoge;
    int y = P.first, x = P.second;
    for (int i = 0; i < 2; i++) {
      int ny = y + dd[i], nx = x + dd[1 - i];
      if (is_out_of_range(ny) || is_out_of_range(nx))
        continue;
      if (ans[ny][nx])
        continue;
      Q.push(make_pair(ny, nx));
      hoge(ny, nx);
    }
  }
}

signed main() {

  cin >> N;

  for (int i = 1; i <= 2 * N * N - 2 * N; i++) {
    cin >> a >> b;
    R[a].push_back(b);
    R[b].push_back(a);

    // if(a==950) cout<<"a "<<b<<endl;
    // if(b==950) cout<<"b "<<a<<endl;
  }
  for (int i = 1; i <= N * N; i++) {
    if (R[i].size() == 2)
      ans[1][1] = i;
  }

  // cout<<ans[1][1]<<" "<<R[ans[1][1]].size()<<endl;
  huga();

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (j != 1)
        cout << " ";
      cout << ans[i][j];
    }
    cout << endl;
  }

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unistd.h>
#include <vector>
using namespace std;
#define MAX 110

int dxy[4] = {-1, 1, 0, 0};
int dd[] = {0, 1};
int N, a, b, ans[MAX][MAX] = {};
vector<int> R[MAX * MAX];
// ものすごくアホだった(2回目)

template <typename T> void remove(vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

inline bool is_out_of_range(int z) { return (z <= 0 || N < z); }

inline void hoge(int y, int x) {
  // cout<<y<<" "<<x<<endl;
  // sleep(2);
  // string hogehoge;
  // cin>>hogehoge;
  if (y == 1) {
    for (int z : R[ans[y][x - 1]]) {
      // cout<<z<<" -----"<<endl;
      if (R[z].size() == 4)
        continue;
      ans[y][x] = z;
    }
  } else {
    for (int z : R[ans[y - 1][x]]) {
      // cout<<z<<" -----"<<endl;
      ans[y][x] = z;
    }
  }
  // cout<<ans[y][x]<<" ans "<<endl;

  for (int i = 0; i < 4; i++) {
    int ny = y + dxy[i], nx = x + dxy[3 - i];
    if (is_out_of_range(ny) || is_out_of_range(nx))
      continue;
    if (ans[ny][nx] == 0)
      continue;
    for (int i = 0; i < R[ans[ny][nx]].size(); i++) {
      if (R[ans[ny][nx]][i] == ans[y][x])
        remove(R[ans[ny][nx]], i);
    }
    for (int i = 0; i < R[ans[y][x]].size(); i++) {
      if (R[ans[y][x]][i] == ans[ny][nx])
        remove(R[ans[y][x]], i);
    }

    // R[ans[ny][nx]].erase(ans[y][x]);
    // R[ans[y][x]].erase(ans[ny][nx]);
  }
}

int huga() {
  queue<pair<int, int>> Q;

  Q.push(make_pair(1, 1));

  while (!Q.empty()) {
    pair<int, int> P = Q.front();
    Q.pop(); // string hogehoge; cin>>hogehoge;
    int y = P.first, x = P.second;
    for (int i = 0; i < 2; i++) {
      int ny = y + dd[i], nx = x + dd[1 - i];
      if (is_out_of_range(ny) || is_out_of_range(nx))
        continue;
      if (ans[ny][nx])
        continue;
      Q.push(make_pair(ny, nx));
      hoge(ny, nx);
    }
  }
}

signed main() {

  cin >> N;

  for (int i = 1; i <= 2 * N * N - 2 * N; i++) {
    cin >> a >> b;
    R[a].push_back(b);
    R[b].push_back(a);

    // if(a==950) cout<<"a "<<b<<endl;
    // if(b==950) cout<<"b "<<a<<endl;
  }
  for (int i = 1; i <= 2 * N * N - 2 * N; i++) {
    if (R[i].size() == 2)
      ans[1][1] = i;
  }

  // cout<<ans[1][1]<<" "<<R[ans[1][1]].size()<<endl;
  huga();

  for (int i = 1; i <= N; i++) {
    for (int j = 1; j <= N; j++) {
      if (j != 1)
        cout << " ";
      cout << ans[i][j];
    }
    cout << endl;
  }

  return 0;
}