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: Accepted

Solution code:
n,*a=map(int,open(0).read().split());print(sum(a[~-a[i]]==-~i for i in range(n))>>1)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split());print(sum(a[a[i]-1]==i+1for i in range(n))>>1)

Submission status: Accepted

Solution code:
n=int(input());s=input().split();print(n-len(set(tuple(sorted([i+1,int(s[i])]))for i in range(n))))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
As = [A-1 for A in map(int, input().split())]

ans = 0
for i, A in enumerate(As):
    if As[A] == i:
        ans += 1
print(ans // 2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = [int(m) for m in input().split()]
pair = []
for i in range(n):
  pair.append([i + 1, a[i]])

for i in range(n):
  pair[i].sort()
tuplepair = []
for i in range(n):
  tuplepair.append((pair[i][0], pair[i][1]))
print(n - len(set(tuplepair)))

Submission status: Accepted

Solution code:
N = int(input())
A = [0] + list(map(int, input().split()))
res = 0
for i in range(1, N):
    if i == A[A[i]]:
        res += 1
        A[A[i]] = 0
print(res)

Submission status: Accepted

Solution code:
n = int(input())
a = list(int(i) for i in input().split())
ans = 0
for i in range(n):
  if i == a[(a[i])-1]-1:
    ans += 1

print(ans//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


ans=0

for i in range(n):
    if i+1==a[a[i]-1]:
        ans=ans+1

print(ans//2)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(i)-1 for i in input().split()]
ans2=0
for i in range(N):
    if a[a[i]]==i:
        ans2+=1
ans=ans2//2
print(ans)

Submission status: Accepted

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

cnt=0
for j in range(N):
    if j+1==A[A[j]-1]:
        cnt+=1
        
print(cnt//2)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(N):
    if i+1 == a[a[i]-1]:
        ans += 1
print(ans//2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) - 1 for _ in input().split()]
ans = 0
for i in range(N):
    if i < A[i] and A[A[i]] == i:
        ans += 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
for i in range(N):
  if A[A[i]-1] == i+1:
    cnt += 1
    
print(cnt // 2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(N):
    j = A[i] - 1
    if i < j and A[j] - 1 == i:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
input();*a,=map(int,input().split());print(sum(a[a[i]-1]==i+1for i in range(len(a)))>>1)

Submission status: Accepted

Solution code:
input();a=list(map(int,input().split()));print(sum([a[i]>a[a[i]-1]==i+1for i in range(len(a))]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=[int(i)-1 for i in input().split()]
ans=0
for i in range(N):
  if a[a[i]]==i:
    ans+=1
print(ans//2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin

s_in = lambda: stdin.readline()[:-1] # s = s_in()
d_in = lambda: int(stdin.readline()) # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split())) # List = ds_in()


N = d_in()
a_list = ds_in()

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

print(ans // 2)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
checklist = []
for i in range(N):
    cl = [i, a[i] - 1]
    cl.sort()
    checklist.append(cl)
cnt = 0
checklist.sort()
for i in range(1, N):
    if checklist[i] == checklist[i - 1]:
        cnt += 1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
d=set()
v=0
for i,j in enumerate(map(int,input().split()),start=1):
  t=tuple(sorted([i,j]))
  if t in d:
    v+=1
  else:
    d.add(t)
print(v)

Submission status: Accepted

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_s(): return list(input().split())

N = int(input())
aa = inpl()

ans = 0
for i in range(N):
	if i == aa[aa[i]-1]-1:
		ans += 1

print(ans//2)

Submission status: Accepted

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

res = 0

for i in range(n):
    if a[a[i]-1]-1 == i:res +=1

print(int(res/2))

Submission status: Accepted

Solution code:
n = int(input())
an = input().split()
a = []
ans = 0
for i in range(0, n):
    temp = int(an[i])-1
    a.append(temp)
for i in range(0, n):
    if(i == a[a[i]]):
        ans += 1
print(ans//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(lambda x: int(x)-1, input().split()))

ans = 0
for i in range(N):
    if A[A[i]] == i:
        ans += 1
print(ans // 2)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

for i in range(len(a)):
    if a[a[i] - 1] == i + 1:
        count += 1

print(int(count/2))

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
c = 0
for i in range(N):
	if a[a[i] - 1] == i + 1:
		c += 1

print(int(c / 2))

Submission status: Accepted

Solution code:
N = int(input())
jk =0
le = list(map(int,input().split()))

le = [0] + le
for i in range(N):
    if le[le[i]] == i:
        jk += 1

print(jk // 2)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
co = 0
for i, a in enumerate(A):
    #print(i+1,a,A[i],A[a-1])
    if i+1 == A[a-1]:
        co = co + 1
else:
    print(co//2)

Submission status: Accepted

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

result = 0
for i, v in enumerate(a, 1):
    if a[v - 1] == i:
        a[i - 1] = -1
        result += 1
print(result)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


if __name__ == '__main__':
    n = int(input())
    a = list(map(lambda x: int(x) - 1, input().split()))
    count = 0

    # See:
    # http://code-festival-2016-quala.contest.atcoder.jp/data/other/code-festival-2016-quala/editorial.pdf
    for i in range(len(a)):
        if i == a[a[i]]:
            count += 1

    print(count // 2)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


if __name__ == '__main__':
    n = int(input())
    a = list(map(int, input().split()))
    count = 0

    for i in range(len(a)):
        if i == a[a[i] - 1] - 1:
            count += 1

    print(count // 2)

Submission status: Accepted

Solution code:
n=int(input());*a,=map(int,input().split());print(sum(a[~-a[i]]==-~i for i in range(n))>>1)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split()))
ans = 0
for i in range(n):
  if l[l[i]-1] == i+1:
    ans += 1

print(ans // 2)

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    
    pair = 0
    for i in range(n):
        if a[a[i]-1] == i + 1 and a[a[i]-1] <= a[i]:
            pair += 1
            
    print(pair)
    
    
if __name__ == "__main__":
    main()

Submission status: Accepted

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

for k in range(N):
    if A[A[k]-1] == k+1:
        ans += 1

print(ans//2)

Submission status: Accepted

Solution code:
def getinputdata():
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()
            if(data != ""):
                
                array_result.append(data.split(" "))

                flg = 1

            else:

                flg = 0
    finally:
        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])

cnt=0
for i in range(1,n+1):
     
     val=int(arr_data[1][i-1])
     
     if i==int(arr_data[1][val-1]):
         
         cnt+=1
         
print(cnt//2)

Submission status: Accepted

Solution code:
n=int(input());*a,=map(int,input().split())
print(sum([a[~-a[i]]==-~i for i in range(n)])>>1)

Submission status: Accepted

Solution code:
s=0;n=int(input());*a,=map(int,input().split())
for i in range(~-n):s+=(-~i<a[i])*(a[~-a[i]]==-~i)
print(s)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i)-1 for i in input().split()]

ans = 0
for i,a in enumerate(A):
  if i == A[a] and i < a:
    ans += 1
print(ans)

Submission status: Accepted

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]-1]:
    count += 1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(N):
    if a[i] == 0:
        continue
    if a[a[i]-1]-1 == i:
        ans += 1
        a[a[i]-1], a[i] = 0, 0
        
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(n):
    if a[a[i]-1] == i+1:
        ans += 1
print(ans//2)

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int,input().split()))[:N]

ans = 0
for i in range(N):
    if A[A[i]-1] == i+1: ans+= 1
        
print(ans//2)

Submission status: Accepted

Solution code:
N=int(input())
a=[int(i)-1 for i in input().split()]
K=0
for i in range(N):
  if a[a[i]]==i:
    K+=1
print(K//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


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

Submission status: Accepted

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

    d = {}
    ans = 0
    for i, a in enumerate(A, 1):
        x, y = min(i, a), max(i, a)
        d[(x, y)] = d.get((x, y), 0) + 1
        if d[(x, y)] == 2:
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
N = int(input())
a = list(map(int, input().split()))

print(sum([a[a[i] - 1] - 1 == i for i in range(N)]) // 2)

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    ans = 0
    searched = []
    for i, like in enumerate(A):
        if i + 1 == A[like-1]:
            ans += 1
    print(ans//2)

solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-



def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    A = tuple(a-1 for a in A) # 0-based

    ans = 0
    for i in range(N):
        b = A[i]
        if A[b] == i:
            ans += 1

    ans //= 2
    print(ans)

if __name__ == "__main__": main()

Submission status: Accepted

Solution code:
N = int(input())
like_list = [int(i) for i in input().split()]

num = 0
for i in range(N):
    if like_list[like_list[i] - 1] == i + 1:
        num += 1

print(int(num/2))

Submission status: Accepted

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

@author: taichiNakamura
"""

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

a = list(map(int, a))

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

print(favorite//2)

Submission status: Accepted

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

ans = 0

for i in range(N):
    if a[a[i]-1] == i+1:
        ans += 1

print(int(ans/2))

Submission status: Accepted

Solution code:
N = int(input())
src = list(map(int, input().split()))
ans = 0
for i in range(N):
    j = src[i] - 1
    if src[j] - 1 == i:
        ans += 1
print(ans // 2)

Submission status: Accepted

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

print (count)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(lambda x:int(x)-1, input().split()))


cnt = 0
for i,a in enumerate(A):
  if A[a] == i:
    cnt += 1

print(cnt//2)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
 
count = 0
 
for i, target in enumerate(a):
	if a[target - 1] == i + 1:
		count += 1
 
print(count // 2)

Submission status: Accepted

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

ans = 0
for i in range(N):
    if a[a[i]-1]-1 == i:
        ans += 1
        
print(ans//2)

Submission status: Accepted

Solution code:
N = int(input())

a = list(map(int,input().split()))


ans = 0
for i in range(N):
    if a[a[i]-1]-1 == i:
        ans += 1
print(ans//2)

Submission status: Accepted

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

count = 0
for i in range(N):
    if a[a[i]-1]-1 == i:
        count += 1
print(int(count/2))

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
N = int(input())
a = list(map(int, input().split()))
b = [ (tmp+1,a[tmp]) for tmp in range(len(a)) if a[a[tmp]-1] == tmp+1 ]
print(len(list(set([ tuple(sorted(tmp)) for tmp in b ]))))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(n):
	if(rab[rab[i]-1] == i+1):
		ans += 1
		rab[i] = -1
print ans

Submission status: Accepted

Solution code:
s = int(input())
a=[int(i) for i in input().split()]
f=0
for i in range(s):
 if i==a[a[i]-1]-1:
     f+=1
print(int(f/2))

Submission status: Accepted

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

count = 0
for i in range(0, n):
	if a[a[i]-1]-1 == i:
		count += 1

print(int(count/2))

Submission status: Accepted

Solution code:
N = int(raw_input())
digits_str = raw_input()
likes = map(int, digits_str.split(' '))
tags = [0]*N
for i in range(N):
    if likes[likes[i]-1] == i+1:
        tags[i] = 1
print sum(tags) / 2

Submission status: Accepted

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: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline()
# print = lambda: sys.stdout.writeline()
def main():
    n = int(input())
    L = [int(x)-1 for x in input().split()]

    cnt = 0
    for i in range(n):
        if L[L[i]] == i:
            cnt += 1

    print(cnt//2)

if __name__ == '__main__':
    main()

Submission status: Accepted

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+1 == d[d[x+1]]):
        sum += 1
 
print(sum//2)

Submission status: Accepted

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.
    >>> target_list = [2, 3, 1]
    >>> search_lovers(target_list)
    0
    >>> target_list = [2, 1, 4, 3]
    >>> search_lovers(target_list)
    2
    >>> target_list = [5, 5, 5, 5, 1]
    >>> search_lovers(target_list)
    1
    """
    target_list = [int(i) - 1 for i in target_list]
    lovers = []
    ans = 0
    for i, n in enumerate(target_list):
        if target_list[n] == i:
            ans = ans + 1

    print(int(ans/2))


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

    target = input_two_line()

    search_lovers(target)

Submission status: Accepted

Solution code:
from sys import stdin
s = set()
n = int(stdin.readline())
a = map(int,stdin.readline().split())
ans = 0
for i in xrange(1,n+1):
 x = a[i-1]
 y = i
 if (y,x) in s:
  s.remove((y,x))
  ans+=1
 else:
  s.add((x,y))
print ans

Submission status: Accepted

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

Submission status: Accepted

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 v in b and b[v] == i+1:
            ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = input()
rabits = map(int,raw_input().split())
_rabits = [r - 1 for r in rabits]
count = 0
for i in range(N):
    j = _rabits[i]
    if _rabits[j] == i and i < j:
        count+=1
        #print('(%d,%d)' % (rabits[i],rabits[j]))
print count

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    N = int(input())
    a = [int(x) for x in input().split()]
    num_len = len(a)
    ans = 0
    for i in range(num_len):
        if a[a[i]-1] == i+1:
            ans += 1
    print(int(ans / 2))

# if __name__ == '__main__':
#     N = int(input())
#     a = [int(x) for x in input().split()]
#     num_len = len(a)
#     ans = 0
#     for i in range(1, num_len+1):
#         for j in range(1, num_len+1):
#             if i == a[j-1] and j == a[i-1]:
#                 ans += 1
#     print(int(ans / 2))

Submission status: Accepted

Solution code:
n = int(raw_input())
a = map(int, raw_input().split(' '))
a = map(lambda i: i - 1, a)

res = 0
for i, v in enumerate(a):
    if a[v] == i:
        res += 1

print res / 2

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = input().split(' ')
t = 0
for o in range(n):
    i = o + 1
    b = int(a[o]) - i
    if i == int(a[o + b]):
      t += 1
print(int(t / 2))

Submission status: Accepted

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

print(ans//2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

count = 0
for idx,val in enumerate(A):
    if A[val-1] == idx+1:
        count += 1

print(count//2)

Submission status: Accepted

Solution code:
from collections import defaultdict

mat = defaultdict(lambda: defaultdict(int))
mat[0][1] = 0

ans = 0
_ = input()

for i, a in enumerate(map(lambda x: int(x), input().split())):
    mat[a-1][i] += 1
    mat[i][a-1] += 1

for i, vec in mat.items():
    for j, val in vec.items():
        ans += val // 2

print(ans // 2)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf = 10**10
mod = 10**9 + 7


def f():
    n = int(input())
    a = list(map(int, input().split()))
    c = 0
    for _ in range(n):
        if a[a[_]-1] == _ + 1:
            c += 1
    return c // 2

print(f())

Submission status: Accepted

Solution code:
n=int(raw_input())
N=map(int,raw_input().split())
count=0
for i in range(n):
	if i==N[N[i]-1]-1:
		count+=1
print count/2

Submission status: Accepted

Solution code:
n = int(input())
L = [int(x)-1 for x in input().split()]
# for i , j in enumerate(L):
#     print(i,j)
cnt = 0
for i in range(n):
    if L[L[i]] == i:
        cnt += 1
print(cnt//2)

Submission status: Accepted

Solution code:
n = int(raw_input())
s = raw_input()
aa = s.split(" ")
aa = [int(x)-1 for x in aa]

ctr = 0

for i in range(n):
	if i == aa[aa[i]]:
		ctr = ctr + 1

ctr = ctr / 2

print(str(ctr))

Submission status: Accepted

Solution code:
n = int(input())
l = [int(x)-1 for x in input().split()]

cnt = 0
for i in range(n):
    if l[l[i]] == i: cnt += 1

print(cnt // 2)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
j = 1
b = list(range(2))
c = []
while i < n:
    b[0] = a[i]
    b[1] = j
    b.sort()
    c.append(''.join(str(b)))
    i += 1
    j += 1
l = len(set(c))
print(str(n - l))

Submission status: Accepted

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

for i in range(n):
    try:
        if a[a[i] - 1] - 1 == i:
            cnt += 1
    except:
        pass

print(cnt // 2)

Submission status: Accepted

Solution code:
s1 = input()
s2 = 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 (int(f/2))

Submission status: Accepted

Solution code:
N = int(input())
A = list(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: Accepted

Solution code:
N = int(input())
A = list(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)

n_pair = 0

for i in range(len(A)):
    if A[i] is not None:
        if A[A[i] - 1] == i + 1:
            n_pair += 1
            A[A[i] - 1] = None

print(n_pair)

Submission status: Accepted

Solution code:
n = input()
a = map(int, raw_input().split())
count = 0
for i, v in enumerate(a):
    if a[a[i] - 1] == i + 1:
        if a[i] == 0:
            continue
        count = count + 1
        a[i] = 0
        
print count

Submission status: Accepted

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-1])-1]) == i:
        result = result+1
result = int(result/2)
print(result)

Submission status: Accepted

Solution code:
N = int(input())
aa = input()
b = []
b = aa.split(" ")
output = 0
a = []
flag = []
for i in range(len(b)):
    a.append(int(b[i]))
    flag.append(False)

for i in range(len(a)):
    if (i + 1 == a[a[i] - 1]):
        output = output + 1
        

print(output//2)

Submission status: Accepted

Solution code:
N = input();
a = raw_input().split();
a.insert(0, 0);

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    dic = {}
    l = raw_input().split()
    for i in range(n):
        if i+1 > int(l[i]) :
            if(dic.get("{}{}".format(i+1,l[i]))):
                dic["{}{}".format(i+1,l[i])] += 1
            else:
                dic["{}{}".format(i+1,l[i])] = 1
        else:
            if(dic.get("{}{}".format(l[i],i+1))):
                dic["{}{}".format(l[i]),i+1] += 1
            else:
                dic["{}{}".format(l[i],i+1)] = 1
    c = 0
    for k,v in dic.items():
        if v >= 2:
            c += 1
    print(c)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

pair = 0
for index, item in enumerate(a):
    if a[item - 1] == index + 1:
        pair += 1
print(int(pair / 2))

Submission status: Accepted

Solution code:
a=input()
b=map(int,raw_input().split())
tmp=0
cnt=0
for i in range(0,a):
    tmp=b[i]-1
    if tmp<b[tmp]:
        if b[tmp]==i+1:
            cnt+=1

print cnt

Submission status: Accepted

Solution code:
N = int(input())
a = [int(i) for i in input().split()]

friends = 0
for i in range(0,len(a)):
    if a[i] == -1 or a[i] == -2:
        continue
    if a[a[i] - 1] == (i+1) :
        friends += 1
        a[ (a[i] - 1) ] = -1
        a[i] = -1
print(int(friends))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-                                                         
def main():
  # input
  N = int(raw_input())
  s = raw_input()
  a = map(int, s.split())
  count = 0 
  for i in range(0, N): 
    if a[a[i]-1]-1 == i:
      count = count + 1 

  # output
  print count/2

main()

Submission status: Accepted

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

pairs = 0
for i in range(N):
    x = a[i]
    if a[x - 1] - 1 == i:
        pairs += 1
print(pairs // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
likes = list(enumerate(input().split(' '), start=1))
rev = list(likes)
rev.reverse()
pairs = 0
for n, like in rev:
	if int(like) < n and likes[int(like) - 1][1] == str(n):
			pairs += 1
print(str(pairs))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int()
N = input()
l = map(int, raw_input().split())
count=int()

var = int()
indexerror = int()
for var in range(0, N):
    try:
        k = int()
        k=l[var]
        k-=1
        if k >= (var+1):
            if l[k] == var+1:
                count+=1
    except IndexError:
        indexerror+=1

print count