Problem p03356 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . ___
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         ·ð×æ±£ÓÓ       ÓÀÎÞBUG
*/
// use readint!
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define mp make_pair
#define ll long long
using namespace std;
const int INF = 1e9;
inline int pt(int a[], int l, int r) {
  int p, i, j;
  p = a[l];
  i = l;
  j = r + 1;
  for (;;) {
    while (a[++i] < p)
      if (i >= r)
        break;
    while (a[--j] > p)
      if (j <= l)
        break;
    if (i >= j)
      break;
    else
      swap(a[i], a[j]);
  }
  if (j == l)
    return j;
  swap(a[l], a[j]);
  return j;
}
inline void q_sort(int a[], int l, int r) {
  int q;
  if (r > l) {
    q = pt(a, l, r);
    q_sort(a, l, q - 1);
    q_sort(a, q + 1, r);
  }
}
inline int readint() {
  int x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int rot[100005];
int fa[100005];
int a[100005];
inline int ffa(int x) { return fa[x] == 0 ? x : x = (ffa(fa[x])); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, i, j, x, y;
  n = readint();
  m = readint();
  for (i = 1; i <= n; i++)
    a[i] = readint();
  for (i = 1; i <= m; i++) {
    x = readint();
    y = readint();
    int fx, fy;
    fx = ffa(x);
    fy = ffa(y);
    if (fx != fy)
      fa[fx] = fy;
  }
  for (i = 1; i <= n; i++)
    rot[i] = ffa(i);
  int ans = 0;
  for (i = 1; i <= n; i++)
    ans += (rot[i] == rot[a[i]]);
  cout << ans << endl;
  return 0;
}

Fixed submission:
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . ___
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
         ·ð×æ±£ÓÓ       ÓÀÎÞBUG
*/
// use readint!
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define mp make_pair
#define ll long long
using namespace std;
const int INF = 1e9;
inline int pt(int a[], int l, int r) {
  int p, i, j;
  p = a[l];
  i = l;
  j = r + 1;
  for (;;) {
    while (a[++i] < p)
      if (i >= r)
        break;
    while (a[--j] > p)
      if (j <= l)
        break;
    if (i >= j)
      break;
    else
      swap(a[i], a[j]);
  }
  if (j == l)
    return j;
  swap(a[l], a[j]);
  return j;
}
inline void q_sort(int a[], int l, int r) {
  int q;
  if (r > l) {
    q = pt(a, l, r);
    q_sort(a, l, q - 1);
    q_sort(a, q + 1, r);
  }
}
inline int readint() {
  int x = 0;
  int f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9')
    ch = getchar();
  if (ch == '-') {
    f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int rot[100005];
int fa[100005];
int a[100005];
inline int ffa(int x) {
  int t[100005];
  int sz = 1;
  while (fa[x] != 0) {
    t[sz++] = x;
    x = fa[x];
  }
  for (int i = 1; i < sz; i++)
    fa[t[i]] = x;
  return x;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, i, j, x, y;
  n = readint();
  m = readint();
  for (i = 1; i <= n; i++)
    a[i] = readint();
  for (i = 1; i <= m; i++) {
    x = readint();
    y = readint();
    int fx, fy;
    fx = ffa(x);
    fy = ffa(y);
    if (fx != fy)
      fa[fx] = fy;
  }
  for (i = 1; i <= n; i++)
    rot[i] = ffa(i);
  int ans = 0;
  for (i = 1; i <= n; i++)
    ans += (rot[i] == rot[a[i]]);
  cout << ans << endl;
  return 0;
}