Problem p03527 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define pdi pair<db, int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
char s[MAXN];
int a[MAXN], N, M;
struct node {
  int to, next;
} E[MAXN * 2];
int head[MAXN], sumE, sum;
bool vis[MAXN];
void add(int u, int v) {
  E[++sumE].to = v;
  E[sumE].next = head[u];
  head[u] = sumE;
}
void Init() {
  scanf("%s", s + 1);
  N = strlen(s + 1);
  s[0] = 'a';
  s[N + 1] = 'a';
  for (int i = 1; i <= N + 1; ++i)
    a[i] = (s[i] - s[i - 1] + 26) % 26;
  read(M);
  int L, R;
  for (int i = 1; i <= M; ++i) {
    read(L);
    read(R);
    add(L, R + 1);
    add(R + 1, L);
  }
  for (int i = 1; i <= N + 1; ++i) {
    add(i, N + 2 - i);
  }
}
void dfs(int u) {
  vis[u] = 1;
  sum = (sum + a[u]) % 26;
  for (int i = head[u]; i; i = E[i].next) {
    int v = E[i].to;
    if (!vis[v]) {
      dfs(v);
    }
  }
}
void Solve() {
  bool flag = 1;
  for (int i = 1; i <= N + 1; ++i) {
    if (!vis[i]) {
      sum = 0;
      dfs(i);
      if (sum != 0) {
        flag = 0;
        break;
      }
    }
  }
  if (flag)
    puts("YES");
  else
    puts("NO");
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Init();
  Solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int, int>
#define pdi pair<db, int>
#define mp make_pair
#define pb push_back
#define enter putchar('\n')
#define space putchar(' ')
#define eps 1e-8
#define mo 974711
#define MAXN 100005
// #define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template <class T> void read(T &res) {
  res = 0;
  char c = getchar();
  T f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    res = res * 10 + c - '0';
    c = getchar();
  }
  res *= f;
}
template <class T> void out(T x) {
  if (x < 0) {
    x = -x;
    putchar('-');
  }
  if (x >= 10) {
    out(x / 10);
  }
  putchar('0' + x % 10);
}
char s[MAXN];
int a[MAXN], N, M;
struct node {
  int to, next;
} E[MAXN * 10];
int head[MAXN], sumE, sum;
bool vis[MAXN];
void add(int u, int v) {
  E[++sumE].to = v;
  E[sumE].next = head[u];
  head[u] = sumE;
}
void Init() {
  scanf("%s", s + 1);
  N = strlen(s + 1);
  s[0] = 'a';
  s[N + 1] = 'a';
  for (int i = 1; i <= N + 1; ++i)
    a[i] = (s[i] - s[i - 1] + 26) % 26;
  read(M);
  int L, R;
  for (int i = 1; i <= M; ++i) {
    read(L);
    read(R);
    add(L, R + 1);
    add(R + 1, L);
  }
  for (int i = 1; i <= N + 1; ++i) {
    add(i, N + 2 - i);
  }
}
void dfs(int u) {
  vis[u] = 1;
  sum = (sum + a[u]) % 26;
  for (int i = head[u]; i; i = E[i].next) {
    int v = E[i].to;
    if (!vis[v]) {
      dfs(v);
    }
  }
}
void Solve() {
  bool flag = 1;
  for (int i = 1; i <= N + 1; ++i) {
    if (!vis[i]) {
      sum = 0;
      dfs(i);
      if (sum != 0) {
        flag = 0;
        break;
      }
    }
  }
  if (flag)
    puts("YES");
  else
    puts("NO");
}
int main() {
#ifdef ivorysi
  freopen("f1.in", "r", stdin);
#endif
  Init();
  Solve();
  return 0;
}