Problem p03993 (C++)

Buggy submission (status: Runtime Error):
// ProblemB.cpp

#include <iostream>

static std::istream &ip = std::cin;
static std::ostream &op = std::cout;

#if OJ_MYPC
#include <ojio.h>
#endif

#ifndef OPENOJIO
#define OPENOJIO
#endif

#if 1 || DEFINE
/***************************************************************/
typedef unsigned long long u64;
typedef long long s64;

typedef unsigned uint;

#define ABS(x) ((x) > 0 ? (x) : -(x))

#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) > (y) ? (x) : (y))

#define MIN3(x, y, z) MIN(x, MIN(y, z))
#define MAX3(x, y, z) MAX(x, MAX(y, z))

#define FillZero(arr) memset(arr, 0, sizeof(arr));

/***************************************************************/
#endif // 1 || DEFINE

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// 001
// op << setfill('0') << setw(3) << setiosflags(ios::right) << 1;

// op << fixed << setprecision(20);

using namespace std;

// ProblemB.cpp

#define MAXN 10003

int main(int argc, char *argv[]) {
  OPENOJIO;

  int n;
  int a[MAXN];

  ip >> n;
  for (int i = 1; i <= n; ++i)
    ip >> a[i];

  int rst = 0;

  bool used[MAXN];
  for (int i = 1; i <= n; ++i)
    used[i] = false;

  for (int i = 1; i <= n; ++i) {
    if (used[i])
      continue;

    if (i == a[a[i]]) {
      ++rst;
      used[i] = used[a[i]] = true;
    }
  }

  op << rst << endl;

  return 0;
}
/***************************************************************/


Fixed submission:
// ProblemB.cpp

#include <iostream>

static std::istream &ip = std::cin;
static std::ostream &op = std::cout;

#if OJ_MYPC
#include <ojio.h>
#endif

#ifndef OPENOJIO
#define OPENOJIO
#endif

#if 1 || DEFINE
/***************************************************************/
typedef unsigned long long u64;
typedef long long s64;

typedef unsigned uint;

#define ABS(x) ((x) > 0 ? (x) : -(x))

#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define MAX(x, y) ((x) > (y) ? (x) : (y))

#define MIN3(x, y, z) MIN(x, MIN(y, z))
#define MAX3(x, y, z) MAX(x, MAX(y, z))

#define FillZero(arr) memset(arr, 0, sizeof(arr));

/***************************************************************/
#endif // 1 || DEFINE

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

// 001
// op << setfill('0') << setw(3) << setiosflags(ios::right) << 1;

// op << fixed << setprecision(20);

using namespace std;

// ProblemB.cpp

#define MAXN 100003

int main(int argc, char *argv[]) {
  OPENOJIO;

  int n;
  int a[MAXN];

  ip >> n;
  for (int i = 1; i <= n; ++i)
    ip >> a[i];

  int rst = 0;

  bool used[MAXN];
  for (int i = 1; i <= n; ++i)
    used[i] = false;

  for (int i = 1; i <= n; ++i) {
    if (used[i])
      continue;

    if (i == a[a[i]]) {
      ++rst;
      used[i] = used[a[i]] = true;
    }
  }

  op << rst << endl;

  return 0;
}
/***************************************************************/