Problem p03691 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define Fst first
#define Snd second
#define RG register
#define mp make_pair
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int UI;
typedef unsigned long long ULL;
template <typename T> inline void read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}
template <typename T, typename... U> inline void read(T &x, U &...y) {
  read(x), read(y...);
}
const int N = 1e5 + 10;
int n, m, p, cnt;
int ID[N * 5], head[N * 5], X[N], Y[N];
bool vis[N * 5];
map<int, int> S[410][2];
bitset<410> F[N * 5], GG;
struct Edge {
  int to, last;
  Edge() {}
  Edge(int a, int b) : to(a), last(b) {}
} edge[N << 3];
void ADD(int a, int b) {
  // printf("!%d %d\n",a,b);
  edge[++p] = Edge(b, head[a]);
  head[a] = p;
}
void DFS(int u) {
  if (vis[u])
    return;
  vis[u] = true;
  if (ID[u])
    F[u].set(ID[u]);
  for (int i = head[u]; i; i = edge[i].last) {
    int v = edge[i].to;
    DFS(v);
    F[u] |= F[v];
  }
}
// #define rua
int main() {
//	ios::sync_with_stdio(false);
#ifdef rua
  freopen("GG.in", "r", stdin);
#endif
  read(n, m);
  for (int i = 1; i <= m; ++i) {
    read(X[i], Y[i]);
    S[X[i]][0][i] = ++cnt;
    S[X[i]][1][i] = ++cnt;
    S[Y[i]][0][i] = ++cnt;
    S[Y[i]][1][i] = ++cnt;
  }
  for (int i = 1; i <= n; ++i)
    S[i][0][m + 1] = ++cnt, S[i][1][m + 1] = ++cnt;
  for (int i = 1; i <= m; ++i) {
    int x = X[i], y = Y[i];
    ADD(S[x][1][i], (*S[y][1].lower_bound(i + 1)).Snd);
    ADD((*S[x][0].lower_bound(i + 1)).Snd, (*S[y][1].lower_bound(i + 1)).Snd);
    ADD((*S[x][0].lower_bound(i + 1)).Snd, S[y][0][i]);
    // ADD(S[x][0][i],(*S[y][1].lower_bound(i+1)).Snd);
    ADD(S[y][1][i], (*S[x][1].lower_bound(i + 1)).Snd);
    ADD((*S[y][0].lower_bound(i + 1)).Snd, (*S[x][1].lower_bound(i + 1)).Snd);
    ADD((*S[y][0].lower_bound(i + 1)).Snd, S[x][0][i]);
    // ADD(S[y][0][i],(*S[x][1].lower_bound(i+1)).Snd);
  }
  for (int i = 1; i <= n; ++i) {
    ID[S[i][1][m + 1]] = i;
    // printf("!#%d :%d %d\n",i,S[i][0][m+1],S[i][1][m+1]);
    map<int, int>::iterator a, b;
    a = S[i][0].begin();
    b = a;
    ++b;
    while (b != S[i][0].end()) {
      ADD((*b).Snd, (*a).Snd);
      a = b;
      ++b;
    }
    a = S[i][1].begin();
    b = a;
    ++b;
    while (b != S[i][1].end()) {
      ADD((*a).Snd, (*b).Snd);
      a = b;
      ++b;
    }
  }
  for (int i = 1; i <= cnt; ++i)
    if (!vis[i])
      DFS(i);
  for (int i = 1; i <= n; ++i) {
    int u = S[i][0][m + 1];
    if (F[u].test(i))
      GG.set(i);
  }
  // for(int i=1;i<=n;++i) printf("%d ",GG.test(i)); puts("");
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    int u = S[i][0][m + 1];
    F[u] |= GG;
    if (F[u].test(i))
      continue;
    res += n - 1 - F[u].count();
    // printf("#%d %d\n",i,n-1-F[u].count());
  }
  printf("%d\n", res / 2);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define Fst first
#define Snd second
#define RG register
#define mp make_pair
#define mem(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long LL;
typedef long double LD;
typedef unsigned int UI;
typedef unsigned long long ULL;
template <typename T> inline void read(T &x) {
  char c = getchar();
  bool f = false;
  for (x = 0; !isdigit(c); c = getchar()) {
    if (c == '-') {
      f = true;
    }
  }
  for (; isdigit(c); c = getchar()) {
    x = x * 10 + c - '0';
  }
  if (f) {
    x = -x;
  }
}
template <typename T, typename... U> inline void read(T &x, U &...y) {
  read(x), read(y...);
}
const int N = 1e5 + 10;
int n, m, p, cnt;
int ID[N * 5], head[N * 5], X[N], Y[N];
bool vis[N * 5];
map<int, int> S[410][2];
bitset<410> F[N * 5], GG;
struct Edge {
  int to, last;
  Edge() {}
  Edge(int a, int b) : to(a), last(b) {}
} edge[N * 10];
void ADD(int a, int b) {
  // printf("!%d %d\n",a,b);
  edge[++p] = Edge(b, head[a]);
  head[a] = p;
}
void DFS(int u) {
  if (vis[u])
    return;
  vis[u] = true;
  if (ID[u])
    F[u].set(ID[u]);
  for (int i = head[u]; i; i = edge[i].last) {
    int v = edge[i].to;
    DFS(v);
    F[u] |= F[v];
  }
}
// #define rua
int main() {
//	ios::sync_with_stdio(false);
#ifdef rua
  freopen("GG.in", "r", stdin);
#endif
  read(n, m);
  for (int i = 1; i <= m; ++i) {
    read(X[i], Y[i]);
    S[X[i]][0][i] = ++cnt;
    S[X[i]][1][i] = ++cnt;
    S[Y[i]][0][i] = ++cnt;
    S[Y[i]][1][i] = ++cnt;
  }
  for (int i = 1; i <= n; ++i)
    S[i][0][m + 1] = ++cnt, S[i][1][m + 1] = ++cnt;
  for (int i = 1; i <= m; ++i) {
    int x = X[i], y = Y[i];
    ADD(S[x][1][i], (*S[y][1].lower_bound(i + 1)).Snd);
    ADD((*S[x][0].lower_bound(i + 1)).Snd, (*S[y][1].lower_bound(i + 1)).Snd);
    ADD((*S[x][0].lower_bound(i + 1)).Snd, S[y][0][i]);
    // ADD(S[x][0][i],(*S[y][1].lower_bound(i+1)).Snd);
    ADD(S[y][1][i], (*S[x][1].lower_bound(i + 1)).Snd);
    ADD((*S[y][0].lower_bound(i + 1)).Snd, (*S[x][1].lower_bound(i + 1)).Snd);
    ADD((*S[y][0].lower_bound(i + 1)).Snd, S[x][0][i]);
    // ADD(S[y][0][i],(*S[x][1].lower_bound(i+1)).Snd);
  }
  for (int i = 1; i <= n; ++i) {
    ID[S[i][1][m + 1]] = i;
    // printf("!#%d :%d %d\n",i,S[i][0][m+1],S[i][1][m+1]);
    map<int, int>::iterator a, b;
    a = S[i][0].begin();
    b = a;
    ++b;
    while (b != S[i][0].end()) {
      ADD((*b).Snd, (*a).Snd);
      a = b;
      ++b;
    }
    a = S[i][1].begin();
    b = a;
    ++b;
    while (b != S[i][1].end()) {
      ADD((*a).Snd, (*b).Snd);
      a = b;
      ++b;
    }
  }
  for (int i = 1; i <= cnt; ++i)
    if (!vis[i])
      DFS(i);
  for (int i = 1; i <= n; ++i) {
    int u = S[i][0][m + 1];
    if (F[u].test(i))
      GG.set(i);
  }
  // for(int i=1;i<=n;++i) printf("%d ",GG.test(i)); puts("");
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    int u = S[i][0][m + 1];
    F[u] |= GG;
    if (F[u].test(i))
      continue;
    res += n - 1 - F[u].count();
    // printf("#%d %d\n",i,n-1-F[u].count());
  }
  printf("%d\n", res / 2);
  return 0;
}