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).



Sample input:
4
2 1 4 3


Sample output:
2

Submission status: Runtime Error

Solution code:
for i in range(N):
    for k in num_ban:
        if k == i + 1:
            ban = 1
            break
    if ban == 1:
        ban = 0
        continue
    for j in A:
        count_j += 1
        if count_j != i + 1 and j == i + 1:
            if A[i] == count_j:
                count_ans += 1
                num_ban.append(j)
                num_ban.append(count_j)
    else:
        count_j = 0
else:
    print(int(count_ans))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
res = 0

for i in range(n):
  sel = a[i]
  if a[sel] == i:
    res += 1
else:
  print(res/2)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
res = 0

for i in range(n):
  sel = a[i]
  if a[sel] = i:
    res += 1
else:
  print(res/2)

Submission status: Runtime Error

Solution code:
n = int(input())
nums = list(map(int,input().split()))
count = 0
for i in range(n):
  if i < nums[i] and (i+1) == nums[nums[i]]:
    count += 1
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
nums = list(map(int,input().split()))
count += 1
for i in range(n):
  if i < nums[i] and (i+1) == nums[nums[i]]:
    count += 1
print(count)

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int, input().split())
c = 0
for i in range(n):
  if a[a[i]] == i:
    c++
print(c)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 23 19:10:57 2017

@author: taichiNakamura
"""

# -*- coding: utf-8 -*-
# 整数の入力
N = int(input())
# スペース区切りの整数の入力
a = map(int, input().split())

favorite = 0
for i,ai in enumerate(a):
    if a[ai-1] == (i+1):
        favorite += 1

print(favorite//2)

Submission status: Runtime Error

Solution code:
N = int(input())
usa = list(map(int, input().split()))
flag = [0 for x in range(N)]
count = 0
for i, j in enumerate(usa):
    if(flag[i]):
        continue
    if(usa[j-1]-1 == i):
        count+=1
        flag[usa[j - 1]] = 1

print (count)

Submission status: Runtime Error

Solution code:
number = int(input())
a = list(map(int,input().split()))
count = 0
for i in range(number):
    for j in range(number):
        if a[i] ==j+1 and a[j] == i+1:
            count += 1
print(int(count/2)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
for i in range(a):
    if a[a[i]-1] -1 == i:
        cnt +=1
print(cnt // 2)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
cnt = 0
for i in range(a):
    if a[a[i]] -1 == i:
        cnt +=1
print(cnt // 2)

Submission status: Runtime Error

Solution code:
n = input()
a = map(int,raw_input.split())
print sum(i == a[a[i-1]]-1 for i in range(n)) / 2

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int, input().split())
 
d = {i+1: j for (i,j) in enumerate(a)}
 
sum = 0
for x in range(n):
    if( x == d[d[x]]):
        sum += 1
 
print(sum//2)

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int, input().split())

d = {i+1: j for (i,j) in enumerate(a)}

sum = 0
for x in range(n):
    if( x == d[d[x]]):
        sum += 1

print sum//2

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
# !/usr/bin/env python
# vim: set fileencoding=utf-8 :

"""
#
# Author:   Noname
# URL:      https://github.com/pettan0818
# License:  MIT License
# Created:  2016-09-28
#

# Usage
#
"""
import sys

def input_single_line():
    """Receive Inputs."""
    return input()

def input_two_line():
    """Receive Two Lined Inputs.
    Like this.

    N
    1 2 3 4 5
    """
    sys.stdin.readline()
    target = sys.stdin.readline()
    target = target.rstrip("\n")
    target = target.split(" ")

    return target


def search_lovers(target_list: list) -> None:
    """Search Simple."""
    lovers = []
    for i in target_list:
        if target_list[i-1] == i-1:
            lovers.append(i-1)

    print(len(lovers))


if __name__ == "__main__":
    import doctest
    doctest.testmod()

    target = input_two_line()

    search_lovers(target)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int, input().split()))

b = {}
ans = 0
for i, v in enumerate(a):
    if v > i+1:
        b[i+1] = v
    elif v < i+1:
        if b[v] == i+1:
            ans += 1
print(ans)

Submission status: Runtime Error

Solution code:
n = input()
a = input()
a = a.split(sep=' ')

result = 0
for i in range(1,int(n)+1):
    if int(a[int(a[i])]) == i:
        result = result+1
result = int(result/2)
print(result)

Submission status: Runtime Error

Solution code:
n = input()
a = input()
a = a.split(sep=' ')

result = 0
for i in range(1,int(n)+1):
    if a[a[i]] == i:
        result = result+1
result = int(result/2)
print(result)

Submission status: Runtime Error

Solution code:
rabbit_number = input()
rabbit_like = []
for i in range(rabbit_number):
	like = raw_input()
	rabbit_like.append(like)
couple = 0
for i in range(rabbit_number):
	if rabbit_like[i] == rabbit_like[rabbit_like[i]-1]:
		couple += 1
print couple

Submission status: Runtime Error

Solution code:
n = input()
s = list(input())
a = {}
for i in range(1,int(n)+1):
    a[i] = int(s[2*i-2])

result = 0
for i in range(1,int(n)+1):
    if a[a[i]] == i:
        result = result+1
result = int(result/2)
print(result)

Submission status: Runtime Error

Solution code:
n = input()
s = []
while len(s) < 2*n:
    s = s+list(input())+[' ']
    
a = {}
for i in range(1,int(n)+1):
    a[i] = int(s[2*i-2])

result = 0
for i in range(1,int(n)+1):
    if a[a[i]] == i:
        result = result+1
result = int(result/2)
print(result)

Submission status: Runtime Error

Solution code:
n = input()
l = 0
s = []
while len(s) < 2*n:
    s = s+list(input())+[' ']
    
a = {}
for i in range(1,int(n)+1):
    a[i] = int(s[2*i-2])

result = 0
for i in range(1,int(n)+1):
    if a[a[i]] == i:
        result = result+1
result = int(result/2)
print(result)

Submission status: Runtime Error

Solution code:
n = input()
l = 0
s = []
while len(s) < 2*n-1:
    s = s+[' ']+list(input())
    
a = {}
for i in range(1,int(n)+1):
    a[i] = int(s[2*i-2])

result = 0
for i in range(1,int(n)+1):
    if a[a[i]] == i:
        result = result+1
result = int(result/2)
print(result)

Submission status: Runtime Error

Solution code:
n = int(input())
s = list(input())
r = 0
for i in range(0,n):
    r = r+int(s[i])
    
print(r)

Submission status: Runtime Error

Solution code:
N = int(raw_input())
a = map(int, raw_input().split(" "))
b = [-1]
a = b + a
#print a
count = 0
for i in range(1,N+1):

    #print a.index(a[int(a[i])])
    if i == int(a[(a[i])]) and a.index(a[(a[i])]) == (a[i]):
        count +=1
        a[i] = '0'
        a[(a[i])] ='0'

print count

Submission status: Runtime Error

Solution code:
s1 = raw_input()
s2 = raw_input()
s2 = s2.split(" ")
f = 0
for a,b in enumerate(s2):
    if str(s2[int(b)-1]) == str(a+1):
        f = f+1
print f/2

Submission status: Runtime Error

Solution code:
s1 = raw_input()
s2 = raw_input()
s2 = s2.split(" ")
f = 0
for a,b in enumerate(s2):
    if str(s2[int(b)-1]) == str(a+1):
        f = f+1
print str(f/2)

Submission status: Runtime Error

Solution code:
s1 = raw_input()
s2 = raw_input()
s2 = s2.split(" ")
f = 0
print s2
for a,b in enumerate(s2):
    if str(s2[int(b)-1]) == str(a+1):
        f = f+1
print f/2

Submission status: Runtime Error

Solution code:
s1 = raw_input()
s2 = raw_input()
s2 = s2.split(" ")
f = 0
for a,b in enumerate(s2):
    if s2[int(b)] == str(a):
        f++
print f

Submission status: Runtime Error

Solution code:
s1 = raw_input()
s2 = raw_input()
s2 = s2.split(" ")
f = 0
for a,b in enumerate(s2):
    if s2[b] == str(a):
        f++
print f

Submission status: Runtime Error

Solution code:
N = raw_input()
likes = raw_input()
pairs = 0
counter = 0
rabbit = 1
while counter < (int(N) * 2):
    if int(likes[((int(likes[counter]) -1 ) * 2)]) == rabbit:
        pairs += 1
        rabbit += 1
        counter += 2
    else:
        rabbit += 1
        counter += 2
pairs = pairs / 2
print pairs

Submission status: Runtime Error

Solution code:
n = input()
a = {}
for i in range(1,int(n)+1):
    a[i] = int(input())

result = 0
for i in range(1,int(n)+1):
    if a[a[i]] == i:
        result = result+1
result = int(result/2)
print(result)

Submission status: Runtime Error

Solution code:
num = int(input())
str = input()
Li = list(map(int,str.split(' ')))
CoupleNum = 0
for pointer in range(len(Li):
     ai = Li[pointer]
     if (ai > pointer+1):
         if (Li[ai-1]=pointer+1):
             CoupleNum++
print(CoupleNum)

Submission status: Runtime Error

Solution code:
n = input()
a = {}
for i in range(1,int(n)+1):
    a[i] = int(input())

result = 0
for i in range(1,int(n)+1):
    if a[a[i]] == i:
        result = result+1
print(result/2)

Submission status: Runtime Error

Solution code:
N = input()
a = raw_input().split()
ans = 0
for i in xrange(N):
    if a[ a[i] - 1 ] == i+1:
       ans += 1
print ans

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split())

pairs = []

for i in range(len(A)):
    if A[A[i] - 1] == i + 1:
        pairs.append(1)

print(len(pairs) // 2)

Submission status: Runtime Error

Solution code:
N = int(input())
A = map(int, input().split())

pairs = []

for i in A:
    if A[A[i] - 1] = i + 1:
        pairs.append(1)

print(len(pairs) // 2)

Submission status: Runtime Error

Solution code:
x_vec = map(int, raw_input().split())
ret = 0
for i in range(len(x_vec)):
    if x_vec[x_vec[i]] == i:
        ret += 1 
print ret / 2