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 (Python):











n = int(input())
b = list(map(int, input().split()))

adlist = [[] for i in range(n)]  
for i in range(n):  
    x = i+1
    y = b[i]
    adlist[x-1].append(y)
    adlist[y-1].append(x)

count = 0
for i in range(n):
    if adlist[i].count(b[i]) == 2:
        count += 1
print(count//2)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
print(sum([i == A[A[i] - 1] - 1 for i in range(N)]) // 2)

Solution (Python):


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

if __name__ == "__main__":
    main()

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

count=0

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

Solution (Python):
N = int(input())
l = list(map(int, input().split()))

counter = 0

for i in range(N):
    if l[l[i]-1] == i+1:
        counter += 1
        l[i] = -99
print(counter)

Solution (Python):


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

pears = {}
for index,a in enumerate(A):
  pears[index+1] = a


cnt = 0
for key,value in pears.items():
  if key == pears.get(pears.get(key)):
    cnt += 1
print(cnt//2)

Solution (Python):
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)

Solution (Python):
N = int(input())
nums = list(map(int, input().split()))
answer = 0
for i, num in enumerate(nums, start=1):
  if i == nums[num-1] and i < num:
    answer += 1
print(answer)

Solution (Python):
N,*A = map(int,open(0).read().split())
cnt=0
for i,a in enumerate(A):
  if i+1 == A[a-1]:
    cnt+=1
print(cnt//2)

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

if __name__ == "__main__":
    solve()

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

Solution (Python):
n = int(input())
a = [int(s) for s in input().split()]

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

n = int(input())
a = [0] + list(map(int, input().split()))

ans = 0
for i, v in enumerate(a):
    if i == 0:
        continue
    if i == a[v]:
        ans += 1
print(ans // 2)

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

Solution (Python):
import sys
input = sys.stdin.readline

N=int(input())
a = tuple(map(int,input().split()))
sum = 0

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

print(sum)

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

ans=0
for i in range(N):
    A[i]-=1

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

print(ans//2)

Solution (Python):

def resolve():
    import sys
    input = sys.stdin.readline

    n = int(input().rstrip())
    a_list = [int(x) - 1 for x in input().rstrip().split(" ")]

    ans = 0
    likes = {i: set() for i in range(n)}
    for i, a in enumerate(a_list):
        if a in likes[i]:
            ans += 1
        likes[a].add(i)

    print(ans)

if __name__ == "__main__":
    resolve()

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

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

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

Solution (Python):
from collections import defaultdict

N=int(input())
*A,=map(int,input().split())

like=defaultdict(set)
ans=0
for i in range(N):
  if A[i] in like[i+1]:
    ans += 1
  like[A[i]].add(i+1)
  
print(ans)

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

cnt = 0

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

print(a.count(0) // 2)

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

ans = 0

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

print(ans//2)

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

cnt = 0

for i in range(N):
    if i+1 != a[i] and i+1 == a[a[i]-1]:
        a[a[i]-1] = 0
        a[i] = 0
    else:
        pass

print(a.count(0) // 2)

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

Solution (Python):
def main():
    n = int(input())
    a_list = list(map(int, input().split()))
    cnt = 0

    for i in range(n):
        if a_list[a_list[i] - 1] - 1 == i:
            cnt += 1

    print(cnt // 2)


if __name__ == "__main__":
    main()

Solution (Python):
import sys, math
from functools import lru_cache
from collections import defaultdict
from decimal import Decimal
sys.setrecursionlimit(10**9)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N = ii()
    a = list(mi())
    cnt = 0
    for i in range(N):
        if a[a[i]-1] == i+1:
            cnt += 1

    print(cnt//2)

if __name__ == '__main__':
    main()

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

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from collections import defaultdict
    N = int(readline())
    a = list(map(int, readline().split()))

    d = dict()

    for i, x in enumerate(a, 1):
        d[i] = x

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

    ans //= 2

    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
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)

Solution (Python):
N = int(input())
S = list(map(int,input().split()))

d = {}
cnt = 0
for i in range(N):
  j = S[i] - 1
  d[i] = j
  if j in d:
    if d[j] == i: 
      cnt += 1
  
print(cnt)

Solution (Python):
n = int(input())
l = [False] * n
inputList = list(map(int, input().split()))
pairs = 0
for i in range(n):
    if l[i]:
        continue

    x = inputList[i]-1 
    if inputList[x]-1 == i:
        pairs += 1
        l[x] = True
print(pairs)

Solution (Python):
import numpy as np
n = int(input())
a = list(map(int, input().split()))
a = np.array(a) - 1

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

print(ans // 2)

Solution (Python):
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(int(count / 2))

Solution (Python):
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(c//2)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
B = [set() for x in range(N)]

ans = 0
for i in range(N):
    j = A[i] - 1
    if j in B[i] or i in B[j]:
        ans += 1
    else:
        B[i].add(j)
        B[j].add(i)

print(ans)

Solution (Python):
N = int(input())
A = list(map(lambda x:int(x)-1,input().split()))
ans = 0
paired = []
flag = [0]*N
for i in range(N):
    if flag[i]:
        continue
    else:
        if A[A[i]] == i:
            ans += 1
            flag[A[i]] = 1
print(ans)

Solution (Python):
n,*L=map(int,open(0).read().split());print(sum(L[L[i]-1]==i+1for i in range(n))//2)

Solution (Python):
n,*L=map(int,open(0).read().split());print(sum(L[L[i]-1]==i+1 for i in range(n))//2)

Solution (Python):
n,*L=map(int,open(0).read().split())
print(sum(L[L[i]-1]==i+1 for i in range(n))//2)

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

Solution (Python):
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)

Solution (Python):
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
        continue
print(ans // 2)

Solution (Python):
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)

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

count = 0
for i, a in enumerate(A):
    
    if A[a-1] == i+1:
        count += 1
print(count//2)

Solution (Python):
import sys
import numpy as np
import numba
from numba import jit

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
def main(n, a):
  cnt = 0
  for i, j in enumerate(a):
    if i+1 == a[j-1]:
      cnt+=1
  return cnt//2

n = int(readline())
a = np.array(readline().split(), np.int64)
print(main(n,a))

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

Solution (Python):
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): 
    return list(map(int,input().split()))
def m(): 
    return map(int,input().split())
def onem(): 
    return int(input())
def s(x): 
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): 
    return " ".join(map(str,x))
def max2(x): 
    return max(map(max,x))
def In(x,a): 
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

def pow_k(x, n):
    ans = 1
    while n:
        if n % 2:
            ans *= x
        x *= x
        n >>= 1
    return ans

"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

n = onem()

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
ans = 0
for index,aa in enumerate(a):
  if index+1 == a[aa-1]:
    ans += 1
print(int(ans/2))

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

Solution (Python):

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
a=list(map(int1,input().split()))
ans=0
for i,x in enumerate(a):
    if i==a[x]:
        ans+=1
ans//=2
print(ans)

Solution (Python):
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)

Solution (Python):
N = int(input())
rabit = [ int(v) for v in input().split(" ") ]
skip = set()
num = 0
 
for i in range(N):
  n = rabit[i]
  if i in skip:
    continue
  if rabit[n-1] == i+1:
    skip.add(n-1)
    num += 1
 
print(num)

Solution (Python):
N = int(input())
rabit = input().split(" ")
skip = []
num = 0

for i in range(N):
  n = int(rabit[i])
  if n < i+1:
    continue
  if int(rabit[n-1]) == i+1:
    num += 1

print(num)

Solution (Python):
import sys
input = sys.stdin.readline



def main():
    n = int(input().strip())
    A = [None] + [int(i) for i in input().strip().split()]
    ans = 0
    for i in range(1, n + 1):
        if i == A[A[i]]:
            ans += 1

    print(ans // 2)

    return


if __name__ == "__main__":
    main()

Solution (Python):
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(round(ans / 2))

Solution (Python):
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(round(ans / 2))

Solution (Python):
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)

Solution (Python):
N=int(input())
L=list(map(int,input().split()))
count=0
for i in range(N):
	x=L[i]

	if L[x-1]==i+1:
		count+=1
print(count//2)

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

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]





count = 0


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


print(int(count/2))

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
N = int(input())
A_list = list(map(int, input().split()))

cnt = 0
for i, ai in enumerate(A_list):
    if i < ai:
        if (A_list[ai-1] - 1) == i:
            cnt += 1
print(cnt)

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

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

Solution (Python):
n = int(input())
l = [0] + list(map(int,input().split()))

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

Solution (Python):
n = int(input())
lis =[0] + list(map(int, input().split()))

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

print(cnt//2)

Solution (Python):
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)

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

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

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

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

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

G = {i:[] for i in range(1, N + 1)}

res = 0

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

    G[a].append(i)
    G[i].append(a)

print(res)

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

pair=[0]*n

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

print(cnt)

Solution (Python):
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
ans //= 2

print(ans)

Solution (Python):
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)

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

Solution (Python):
def resolve():
    N = int(input())
    a = list(map(int, input().split()))
    dic = dict()
    for i, an in enumerate(a):
        dic[i + 1] = an
    ans = 0
    
    for d in dic.keys():
        if dic[dic[d]] == d:
            ans += 1
    ans = int(ans/2)
    print(ans)
    return
resolve()

Solution (Python):
n,*a=map(int,open(s:=0).read().split())
i=1
for e in a:s+=a[e-1]==i;i+=1
print(s//2)

Solution (Python):
n, *a = map(int, open(0).read().split())
print(sum(a[b - 1] == e + 1 for e, b in enumerate(a)) // 2)

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

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

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
cnt = 0

for i in range(N):
    if a[i] == -1:
        pass
    else:
        if a[a[i]-1] == i+1:
            cnt += 1
            a[a[i]-1] = -1
print(cnt)

Solution (Python):
n = int(input())

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

li = set()

for i in range(n):
    a = tuple(sorted([i+1,L[i]]))
    li.add(a)
print(n-len(li))

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

ans = 0
for i in range(n):

    if a[i] > i+1 and i+1 == a[a[i]-1]:

        ans+=1

    i+=1

print(ans)

Solution (Python):
import collections
n = int(input())
a = list(map(int, input().split()))
d = {}
ans = 0
for i in range(n):
  d[i + 1] = a[i]
for i in range(n):
  if d[d[i + 1]] == i + 1:
    ans += 1
ans //= 2
print(ans)

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

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

Solution (Python):
def resolve():
    n = int(input())
    a = list(map(int, input().split()))

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

    print(cnt // 2)

resolve()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
cnt=0
for idx,i in enumerate(A):
    if idx==A[i-1]-1:
        cnt+=1
print(cnt//2)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
cnt=0
for idx,i in enumerate(A):
    a_idx=1+idx

    if idx==A[i-1]-1:
        cnt+=1
print(cnt//2)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))

ans = 0
cnt = 0

for a in A:
    cnt += 1
    if A[a-1] == cnt:
        ans += 1

print(ans//2)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
n = int(input())
a = list(map(lambda x:int(x)-1, input().split()))

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

Solution (Python):
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)

Solution (Python):
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
ans //= 2
print(ans)

Solution (Python):
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)

Solution (Python):

n, *a = map(int, open(0).read().split())
ans = 0
for i in range(n):
    if a[a[i] - 1] - 1 == i:
        ans += 1
print(ans // 2)

Solution (Python):
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)

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

d = {}
ans = 0
for num,i in enumerate(a,1):
  if i not in d: d[num] = i
  else:
    if d[i] == num: ans += 1
print(ans)

Solution (Python):
n = int(input())

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

res = 0

for i,v in enumerate(a,1):
    if i == a[v-1]:
        res += 1
print(res//2)

Solution (Python):
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)

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

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
B = [i for i in range(1,n+1)]
cnt = 0
for i in range(n):
    if B[i]==A[(A[i]-1)]:
        cnt+=1
        A[i]=0
print(cnt)

Solution (Python):
from collections import defaultdict

n = int(input())
alst = list(map(int, input().split()))
dd = defaultdict(int)

for i, num in enumerate(alst, start = 1):
    s = str(min(i, num)) + "-" + str(max(i, num))
    dd[s] += 1

ans = 0
for i, j in dd.items():
    if j == 2:
        ans += 1
print(ans)

Solution (Python):


from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    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)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

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

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

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

Solution (Python):
n = int(input())
la = [int(w)-1 for w in input().split()]

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

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]

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

print(ans // 2)

Solution (Python):
def main():
    N = int(input())
    A = [int(a) for a in input().split()]

    ans = 0
    pair = [False for i in range(N)]

    for i in range(N):
        if (A[A[i] - 1] - 1 == i) and (pair[A[i] - 1] == False):
            ans += 1
            pair[i] = True
            pair[A[i] - 1] = True

    print(ans)

if __name__ == "__main__":
    main()

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

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

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

d = {}
ans = 0
for i in range(n):
  if a[i] in d and d[a[i]] == i+1: ans += 1
  else: d[i+1] = a[i]
print(ans)

Solution (Python):
import sys

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

Solution (Python):
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)

Solution (Python):
N=int(input())
a=list(map(int,input().split()))
a.insert(0,0)
ans=0

for i in range(1,len(a)):
  if i==a[a[i]]:
    ans+=1

print(ans//2)

Solution (Python):
import collections

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)

Solution (Python):









import sys
input = sys.stdin.readline

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)

Solution (Python):
N=int(input())
A=["*"]+list(map(int,input().split()))

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

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

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

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

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A = [a - 1 for a in A]

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

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

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

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

Solution (Python):
N=int(input())
ai=list(map(int,input().split()))
ai.insert(0,0)

ans=0

for i in range(1,N+1):
    if ai[ai[i]]==i:
        ans+=1
    else:
        pass
print(ans//2)

Solution (Python):
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)

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

Solution (Python):
n = int(input())
a_l = list(map(int, input().split()))

ans = 0
for i,j in enumerate(a_l):
    if i+1 == a_l[j-1]:
        ans+= 1
print(int(ans/2))

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

Solution (Python):
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)

Solution (Python):

def CODE_festival_2016_A():
    import numpy as np
    N = int(input())
    a = np.array(list(map(int,input().split())))

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

    print(int(ans) if ans%2==1 else int(ans/2))


CODE_festival_2016_A()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    cnt = 0
    for i, x in enumerate(a):
        if a[x-1] == i+1:
            cnt += 1

    print(cnt//2)

if __name__ == '__main__':
    main()

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

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

print(ans//2)

Solution (Python):
N = int(input())
A = [int(a) - 1 for a in input().split()]
count = 0
for i in range(N):
    if i == A[A[i]]:
        count += 1

print(count // 2)

Solution (Python):
N = int(input())
A = [int(a) - 1 for a in input().split()]
liked = [set() for _ in range(N)]
for i in range(N):
    liked[A[i]].add(i)

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

print(count // 2)

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

print(int(count/2))

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
print(sum([a[x-1]==i+1 for i,x in enumerate(a)])//2)

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

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

total = 0
for i in range(N):
    if rabbit[rabbit[i]] == i:
        total += 1

print(total // 2)

Solution (Python):
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)

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

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

Solution (Python):


import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union































def main():
    Num = Union[int, float]
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    
    n = ii()
    L = lmi_0()

    cnt = 0
    for i in range(n):
        if L[L[i]] == i:
            cnt += 1
    assert cnt % 2 == 0
    print(cnt // 2)


if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
a = list(map(int, input().split()))
like = []
cnt = 0

for i in range(0, N):
    like.append([i+1, a[i]])

for j, k in like:
    if like[k-1][1] == j:
        cnt += 1

print(cnt // 2)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
count=0
for i in range (N):
    if A[i]<i+1:
        continue
    K=A[i]
    if A[K-1]==i+1:
        count+=1

print(count)