Problem p03356 (C++)

Buggy submission (status: Runtime Error):
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false);
#define DEBUG_VIS(x) cerr << "vis " << x << endl;
#define DEBUG_REACH cerr << "reach here" << endl;
#define DEBUG_SEQ(x) cerr << x << " ";
#define SET_ZERO(x) memset(x, 0, sizeof(x));
#define SET_NEGONE(x) memset(x, -1, sizeof(x));
#define SET_INF(x) memset(x, 127, sizeof(x));
#define SET_NEGINF(x) memset(x, 128, sizeof(x));
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
typedef long long ll;
typedef unsigned int uint;
const int maxn = 1e5 + 10;
template <class T> T mymin(const T a, const T b) { return a < b ? a : b; }
template <class T> T mymax(const T a, const T b) { return a > b ? a : b; }
int N, M;
vector<int> G[maxn];
int a[maxn], dfn[maxn], vis[maxn], bel[maxn], low[maxn], inst[maxn], sz[maxn],
    dfs_clock, scc_cnt;
stack<int> S;
void tarjan(int u) {
  dfn[u] = low[u] = ++dfs_clock;
  S.push(u);
  inst[u] = 1;
  for (int v : G[u]) {
    if (!dfn[v]) {
      tarjan(v);
      low[u] = min(low[u], low[v]);
    } else if (inst[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (low[u] == dfn[u]) {
    int v = 0;
    scc_cnt++;
    while (v != u) {
      v = S.top();
      S.pop();
      bel[v] = scc_cnt;
      sz[scc_cnt]++;
      inst[v] = 0;
    }
  }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("data.in", "r", stdin);
#endif
  FAST_IO
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    cin >> a[i];
  for (int i = 1; i <= M; i++) {
    int u, v;
    cin >> u >> v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }
  for (int i = 1; i <= N; i++)
    if (!dfn[i])
      tarjan(i);
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (bel[a[i]] == bel[i])
      ans++;
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC optimize("unroll-loops")
#include <algorithm>
#include <array>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
#define FAST_IO ios::sync_with_stdio(false);
#define DEBUG_VIS(x) cerr << "vis " << x << endl;
#define DEBUG_REACH cerr << "reach here" << endl;
#define DEBUG_SEQ(x) cerr << x << " ";
#define SET_ZERO(x) memset(x, 0, sizeof(x));
#define SET_NEGONE(x) memset(x, -1, sizeof(x));
#define SET_INF(x) memset(x, 127, sizeof(x));
#define SET_NEGINF(x) memset(x, 128, sizeof(x));
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef pair<double, double> pdd;
typedef long long ll;
typedef unsigned int uint;
const int maxn = 1e5 + 10;
template <class T> T mymin(const T a, const T b) { return a < b ? a : b; }
template <class T> T mymax(const T a, const T b) { return a > b ? a : b; }
int N, M;
vector<int> G[maxn];
int a[maxn], dfn[maxn], vis[maxn], bel[maxn], low[maxn], inst[maxn], sz[maxn],
    dfs_clock, scc_cnt;
stack<int> S;
void tarjan(int u) {
  dfn[u] = low[u] = ++dfs_clock;
  S.push(u);
  inst[u] = 1;
  for (int v : G[u]) {
    if (!dfn[v]) {
      tarjan(v);
      low[u] = min(low[u], low[v]);
    } else if (inst[v])
      low[u] = min(low[u], dfn[v]);
  }
  if (low[u] == dfn[u]) {
    int v = 0;
    scc_cnt++;
    while (v != u) {
      v = S.top();
      S.pop();
      bel[v] = scc_cnt;
      sz[scc_cnt]++;
      inst[v] = 0;
    }
  }
}
int main() {
  FAST_IO
  cin >> N >> M;
  for (int i = 1; i <= N; i++)
    cin >> a[i];
  for (int i = 1; i <= M; i++) {
    int u, v;
    cin >> u >> v;
    G[u].emplace_back(v);
    G[v].emplace_back(u);
  }
  for (int i = 1; i <= N; i++)
    if (!dfn[i])
      tarjan(i);
  int ans = 0;
  for (int i = 1; i <= N; i++) {
    if (bel[a[i]] == bel[i])
      ans++;
  }
  cout << ans << endl;
  return 0;
}