Problem p03993 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i <= int(r); i++)
#define all(a) a.begin(), a.end()
#define sz(a) int(a.size())
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define x first
#define y second

template <typename X> inline X abs(const X &a) { return a < 0 ? -a : a; }
template <typename X> inline X sqr(const X &a) { return a * a; }

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9;
const ld PI = acosl(ld(-1));

const int N = 200 + 3;
int n;
int a[N];

inline bool read() {
  if (scanf("%d", &n) != 1)
    return false;

  forn(i, n) {
    assert(scanf("%d", &a[i]) == 1);

    a[i]--;
  }

  return true;
}

inline void solve() {
  int ans = 0;
  forn(i, n) {
    int j = a[i];
    if (a[j] == i)
      ans++;
  }

  cout << ans / 2 << endl;
}

int main() {
#ifdef SU2_PROJ
  assert(freopen("input.txt", "r", stdin));
  assert(freopen("output.txt", "w", stdout));
#endif

  cout << setprecision(25) << fixed;
  cerr << setprecision(10) << fixed;

  srand(int(time(NULL)));

  assert(read());
  solve();

#ifdef SU2_PROJ
  cerr << "TIME: " << clock() << endl;
#endif

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define forn(i, n) for (int i = 0; i < int(n); i++)
#define ford(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define fore(i, l, r) for (int i = int(l); i <= int(r); i++)
#define all(a) a.begin(), a.end()
#define sz(a) int(a.size())
#define mp make_pair
#define pb push_back
#define ft first
#define sc second
#define x first
#define y second

template <typename X> inline X abs(const X &a) { return a < 0 ? -a : a; }
template <typename X> inline X sqr(const X &a) { return a * a; }

typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;

const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9;
const ld PI = acosl(ld(-1));

const int N = 100 * 1000 + 3;
int n;
int a[N];

inline bool read() {
  if (scanf("%d", &n) != 1)
    return false;

  forn(i, n) {
    assert(scanf("%d", &a[i]) == 1);

    a[i]--;
  }

  return true;
}

inline void solve() {
  int ans = 0;
  forn(i, n) {
    int j = a[i];
    if (a[j] == i)
      ans++;
  }

  cout << ans / 2 << endl;
}

int main() {
#ifdef SU2_PROJ
  assert(freopen("input.txt", "r", stdin));
  assert(freopen("output.txt", "w", stdout));
#endif

  cout << setprecision(25) << fixed;
  cerr << setprecision(10) << fixed;

  srand(int(time(NULL)));

  assert(read());
  solve();

#ifdef SU2_PROJ
  cerr << "TIME: " << clock() << endl;
#endif

  return 0;
}