Problem:
Score : 200 points

Problem Statement

There are N rabbits, numbered 1 through N.

The i-th (1≤i≤N) rabbit likes rabbit a_i.
Note that no rabbit can like itself, that is, a_i≠i.

For a pair of rabbits i and j (i＜j), we call the pair (i，j) a friendly pair if the following condition is met.

Rabbit i likes rabbit j and rabbit j likes rabbit i.

Calculate the number of the friendly pairs.

Constraints

2≤N≤10^5

1≤a_i≤N

a_i≠i

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of the friendly pairs.

Sample Input 1

4
2 1 4 3

Sample Output 1

2

There are two friendly pairs: (1，2) and (3，4).

Sample Input 2

3
2 3 1

Sample Output 2

0

There are no friendly pairs.

Sample Input 3

5
5 5 5 5 1

Sample Output 3

1

There is one friendly pair: (1，5).

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Memory Limit Exceeded)

import java.util.*;
public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] ar = new int[n];
		int count =0;
		int[][] sys_m = new int[n][n];
		// create second array 
		for (int i=0;i<n;i++){
			 ar[i]= sc.nextInt();
		}
		//create symmetric matrix
		for (int i=0;i<n;i++){
			for (int j=0;j<n;j++){
					sys_m[i][j] = 0;
			}
		}
		//set relation on symmetirc matrix
		for (int i=0;i<n;i++){
			int tmp = ar[i]-1;
			sys_m[i][tmp] = 1;
		}
//		for (int i=0;i<n;i++){
//			for (int j=0;j<n;j++){
//				System.out.print(sys_m[i][j] + " ");
//			}
//			System.out.println();
//		}
		// count relation pair
		for (int i=0;i<n;i++){
			int tmp = ar[i]-1;
			if (sys_m[i][tmp]==1&& sys_m[tmp][i]==1){
				count++;
			}
		}
		System.out.print(count/2);
	}

}