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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

struct SCC { // O(V+E)
  static const int MAX_V = 101000;
  int V;
  vector<int> G[MAX_V], rG[MAX_V];
  vector<int> vs;
  bool used[MAX_V];
  int cmp[MAX_V];
  vector<vector<int>> res;

  SCC(int v) : V(v){};
  void add_edge(int from, int to) {
    G[from].push_back(to);
    rG[to].push_back(from);
  }

  void dfs(int v) {
    used[v] = true;
    for (int i = 0; i < G[v].size(); i++) {
      if (!used[G[v][i]])
        dfs(G[v][i]);
    }
    vs.push_back(v);
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    for (int i = 0; i < rG[v].size(); i++) {
      if (!used[rG[v][i]])
        rdfs(rG[v][i], k);
    }
  }

  int scc() {
    memset(used, 0, sizeof(used));
    vs.clear();
    for (int v = 0; v < V; v++) {
      if (!used[v])
        dfs(v);
    }
    memset(used, 0, sizeof(used));
    int k = 0;
    for (int i = vs.size() - 1; i >= 0; i--) {
      if (!used[vs[i]])
        rdfs(vs[i], k++);
    }

    res.clear();
    res.resize(k);
    for (int i = 0; i < V; i++) {
      res[cmp[i]].push_back(i);
    }
    return k;
  }
};

int main() {
  int ans = 0;
  int n;
  cin >> n;
  SCC scc(n);
  rep(i, n) {
    int a;
    cin >> a;
    (a += i) %= n;
    if (a == i)
      ans++;
    else
      scc.add_edge(i, a);
  }
  scc.scc();
  for (auto a : scc.res) {
    if (a.size() != 1)
      ans += a.size();
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

struct SCC { // O(V+E)
  static const int MAX_V = 10100;
  int V;
  vector<int> G[MAX_V], rG[MAX_V];
  vector<int> vs;
  bool used[MAX_V];
  int cmp[MAX_V];
  vector<vector<int>> res;

  SCC(int v) : V(v){};
  void add_edge(int from, int to) {
    G[from].push_back(to);
    rG[to].push_back(from);
  }

  void dfs(int v) {
    used[v] = true;
    for (int i = 0; i < G[v].size(); i++) {
      if (!used[G[v][i]])
        dfs(G[v][i]);
    }
    vs.push_back(v);
  }
  void rdfs(int v, int k) {
    used[v] = true;
    cmp[v] = k;
    for (int i = 0; i < rG[v].size(); i++) {
      if (!used[rG[v][i]])
        rdfs(rG[v][i], k);
    }
  }

  int scc() {
    memset(used, 0, sizeof(used));
    vs.clear();
    for (int v = 0; v < V; v++) {
      if (!used[v])
        dfs(v);
    }
    memset(used, 0, sizeof(used));
    int k = 0;
    for (int i = vs.size() - 1; i >= 0; i--) {
      if (!used[vs[i]])
        rdfs(vs[i], k++);
    }

    res.clear();
    res.resize(k);
    for (int i = 0; i < V; i++) {
      res[cmp[i]].push_back(i);
    }
    return k;
  }
};

int main() {
  int ans = 0;
  int n;
  cin >> n;
  SCC scc(n);
  rep(i, n) {
    int a;
    cin >> a;
    (a += i) %= n;
    if (a == i)
      ans++;
    else
      scc.add_edge(i, a);
  }
  scc.scc();
  for (auto a : scc.res) {
    if (a.size() != 1)
      ans += a.size();
  }
  cout << ans << endl;
}