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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#pragma warning(disable : 4996)
using namespace std;
map<pair<int, int>, int> dt;
int N, Q, cur, cs, tp[40009], a[40009], b[40009], es[40009], id[40009],
    seq[40009];
bool vis[40009];
vector<int> g[40009], gs[40009];
void dfs(int pos) {
  id[pos] = cur;
  for (int i : g[pos]) {
    if (id[i] == -1)
      dfs(i);
  }
}
void dfs2(int pos) {
  vis[pos] = true;
  seq[cs++] = pos;
  for (int i : gs[pos]) {
    if (!vis[i])
      dfs2(i);
  }
}
int main() {
  scanf("%d %d", &N, &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &tp[i], &a[i], &b[i]);
    if (tp[i] == 1)
      dt[make_pair(a[i], b[i])] = i, es[i] = -1;
    if (tp[i] == 2)
      es[dt[make_pair(a[i], b[i])]] = i;
  }
  int B = sqrt(Q);
  for (int i = 0; i < B; i++) {
    int l = i * Q / B, r = (i + 1) * Q / B;
    for (int j = 0; j < N; j++)
      g[j].clear();
    for (int j = 0; j < l; j++) {
      if (tp[j] == 1 && (es[j] == -1 || es[j] >= r)) {
        g[a[j]].push_back(b[j]);
        g[b[j]].push_back(a[j]);
      }
    }
    cur = 0;
    fill(id, id + N, -1);
    for (int j = 0; j < N; j++) {
      if (id[j] == -1) {
        dfs(j);
        cur++;
      }
    }
    for (int j = 0; j < cur; j++)
      gs[j].clear();
    for (int j = 0; j < l; j++) {
      if (tp[j] == 1 && id[a[j]] != id[b[j]] && l <= es[j] && es[j] < r) {
        gs[id[a[j]]].push_back(id[b[j]]);
        gs[id[b[j]]].push_back(id[a[j]]);
      }
    }
    for (int j = l; j < r; j++) {
      if (tp[j] == 1) {
        gs[id[a[j]]].push_back(id[b[j]]);
        gs[id[b[j]]].push_back(id[a[j]]);
      }
      if (tp[j] == 2 && id[a[j]] != id[b[j]]) {
        gs[id[a[j]]].erase(
            find(gs[id[a[j]]].begin(), gs[id[a[j]]].end(), id[b[j]]));
        gs[id[b[j]]].erase(
            find(gs[id[b[j]]].begin(), gs[id[b[j]]].end(), id[a[j]]));
      }
      if (tp[j] == 3) {
        cs = 0;
        dfs2(id[a[j]]);
        printf(vis[id[b[j]]] ? "YES\\n" : "NO\\n");
        for (int k = 0; k < cs; k++)
          vis[seq[k]] = false;
      }
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
using namespace std;
map<pair<int, int>, int> dt;
int N, Q, cur, cs, tp[40009], a[40009], b[40009], es[40009], id[40009],
    seq[40009];
bool vis[40009];
vector<int> g[40009], gs[40009];
void dfs(int pos) {
  id[pos] = cur;
  for (int i : g[pos]) {
    if (id[i] == -1)
      dfs(i);
  }
}
void dfs2(int pos) {
  vis[pos] = true;
  seq[cs++] = pos;
  for (int i : gs[pos]) {
    if (!vis[i])
      dfs2(i);
  }
}
int main() {
  scanf("%d %d", &N, &Q);
  for (int i = 0; i < Q; i++) {
    scanf("%d %d %d", &tp[i], &a[i], &b[i]);
    if (tp[i] == 1)
      dt[make_pair(a[i], b[i])] = i, es[i] = -1;
    if (tp[i] == 2)
      es[dt[make_pair(a[i], b[i])]] = i;
  }
  int B = sqrt(Q);
  for (int i = 0; i < B; i++) {
    int l = i * Q / B, r = (i + 1) * Q / B;
    for (int j = 0; j < N; j++)
      g[j].clear();
    for (int j = 0; j < l; j++) {
      if (tp[j] == 1 && (es[j] == -1 || es[j] >= r)) {
        g[a[j]].push_back(b[j]);
        g[b[j]].push_back(a[j]);
      }
    }
    cur = 0;
    fill(id, id + N, -1);
    for (int j = 0; j < N; j++) {
      if (id[j] == -1) {
        dfs(j);
        cur++;
      }
    }
    for (int j = 0; j < cur; j++)
      gs[j].clear();
    for (int j = 0; j < l; j++) {
      if (id[a[j]] != id[b[j]] && l <= es[j] && es[j] < r) {
        gs[id[a[j]]].push_back(id[b[j]]);
        gs[id[b[j]]].push_back(id[a[j]]);
      }
    }
    for (int j = l; j < r; j++) {
      if (tp[j] == 1) {
        gs[id[a[j]]].push_back(id[b[j]]);
        gs[id[b[j]]].push_back(id[a[j]]);
      }
      if (tp[j] == 2) {
        gs[id[a[j]]].erase(
            find(gs[id[a[j]]].begin(), gs[id[a[j]]].end(), id[b[j]]));
        gs[id[b[j]]].erase(
            find(gs[id[b[j]]].begin(), gs[id[b[j]]].end(), id[a[j]]));
      }
      if (tp[j] == 3) {
        cs = 0;
        dfs2(id[a[j]]);
        printf(vis[id[b[j]]] ? "YES\\n" : "NO\\n");
        for (int k = 0; k < cs; k++)
          vis[seq[k]] = false;
      }
    }
  }
  return 0;
}