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: Time Limit Exceeded

Solution code:
n = int(raw_input())
s = raw_input()
aa = s.split(" ")
aaa = []
for i in range(n):
	aaa.append((i+1,int(aa[i])))

ctr = 0

for (a,b) in aaa:
	if (b,a) in aaa:
		aaa.remove((a,b))
		aaa.remove((b,a))
		ctr = ctr + 1
# ctr = ctr / 2
print(str(ctr))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(raw_input())
k = raw_input()
l = []
for i in range(0, len(k)):
	if i % 2 == 0:
		l.append(k[i])
d = {}
for c in range(0, len(l)):
	d[c+1] = l[c]
count = 0
for x,y in d.items():
	if (int(y), str(x)) not in d.items():
		continue
	else:
		count +=1
print count / 2

Submission status: Time Limit Exceeded

Solution code:
n = int(raw_input())
s = raw_input()
aa = s.split(" ")
aaa = []
for i in range(n):
	aaa.append((i+1,int(aa[i])))

ctr = 0

for (a,b) in aaa:
	if (b,a) in aaa:
		# aaa.remove((a,b))
		# aaa.remove((b,a))
		ctr = ctr + 1
ctr = ctr / 2
print(str(ctr))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N = int(raw_input())
k = raw_input()
l = []
for i in range(0, len(k)):
	if i % 2 == 0:
		l.append(k[i])
d = {}
for c in range(0, len(l)):
	d[c+1] = l[c]
count = 0
for x,y in d.items():
	if (int(y), str(x)) in d.items():
		count +=1
print count / 2

Submission status: Time Limit Exceeded

Solution code:
n=int(raw_input())
arr=map(int,raw_input().split(" "))
arr.insert(0,0)
marked=[0]*(n+1)

ctr=0
for i in range(1,n):
    if marked[i]:
        continue
    for j in range(i,n+1):
        if marked[j] | marked[i]:
            break
        if (arr[i]==j) & (arr[j]==i):
            ctr=ctr+1
            marked[i]=1
            marked[j]=1
            
print ctr

Submission status: Time Limit Exceeded

Solution code:
N = input()
L = input()
L = L.strip().split()
for e in range(len(L)):
    L[e] = int(L[e])-1

count = 0
searched = []
for e in L:
    if e in searched:
        continue
    if L[L[e]] == e:
        searched.append(e)
        searched.append(L[e])
        count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
N=raw_input()

a=raw_input().split(" ")

c=0

for i in xrange(len(a)):
    for j in xrange(i,len(a)):
        if a[i] == str(j+1) and a[j] == str(i+1):
            c+=1
            break

print c

Submission status: Time Limit Exceeded

Solution code:
N=raw_input()

a=raw_input().split(" ")

c=0

for i in xrange(len(a)):
    for j in xrange(len(a)):
        if a[i] == str(j+1) and a[j] == str(i+1):
            c+=1
            break

print str(c/2)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N=raw_input()

a=raw_input().split(" ")

c=0

for i in xrange(len(a)):
    for j in xrange(len(a)):
        if a[i] == str(j+1) and a[j] == str(i+1):
            c+=1

print str(c/2)

Submission status: Time Limit Exceeded

Solution code:
n=int(raw_input())
arr=map(int,raw_input().split(" "))
arr.insert(0,0)

ctr=0
for i in range(1,n):
    for j in range(i,n+1):
        if (arr[i]==j) & (arr[j]==i):
            ctr=ctr+1
print ctr

Submission status: Time Limit Exceeded

Solution code:
N=raw_input()

a=raw_input().split(" ")

a=[int(i) for i in a]

c=0

for i in range(len(a)):
    for j in range(len(a)):
        if a[i] == j+1 and a[j] == i+1:
            c+=1

print str(c/2)

Submission status: Time Limit Exceeded

Solution code:
N=input()
a=map(int,raw_input().split())
check=[]
ans=0
for i,a_i in enumerate(a):
    if i in check:
        continue
    else:
        if i+1==a[a_i-1]:
            ans+=1
            check.append(a_i-1)
print ans

Submission status: Time Limit Exceeded

Solution code:
size = int(input())

r_str = input().split()

rabs = []
for r in r_str:
    rabs.append(int(r) - 1)

visited = []
t = 0

for i in range(0, size):
    if rabs[rabs[i]] == i and (i not in visited):
        visited.append(i)
        visited.append(rabs[i])
        t += 1

print(t)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
As = list(map(int, input().split()))
As.insert(0,-1)
cnt = 0
for i in range(N+1):
    for j in range(i,N+1):
        if i >= j:
            continue
        if As[i] == j and As[j] == i:
            cnt += 1
print(cnt)

Submission status: Accepted

Solution code:
# n = int(input())
# a = list(map(int, input().split()))
# ans = [[i] for i in range(1, n+1)]
# print(ans)
# for i in range(n):
#     print(ans[i], a[i])
#     ans[i].apeend(a[i])
#     ans[a[i]-1].append(i)
# print(ans)
# これなんでダメだったかわからん

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

adlist = [[] for i in range(n)]  # 交流のあるやつ同士の隣接リスト
for i in range(n):  # 1から順に見ていく
    x = i+1
    y = b[i]
    adlist[x-1].append(y)
    adlist[y-1].append(x)
# print(adlist)
count = 0
for i in range(n):
    if adlist[i].count(b[i]) == 2:
        count += 1
print(count//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# B

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)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

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()))
ans = 0
for i in range(N):
    if i+1 == a[a[i]-1]:
        ans += 1
        continue
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())
A = list(map(int,input().split()))

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    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): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    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)

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
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)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


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

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(round(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())
L=list(map(int,input().split()))
count=0
for i in range(N):
	x=L[i]
#	print(L[i],L[L[i]-1])
	if L[x-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,a in enumerate(A):
    if i==A[a-1]-1: ans +=1
print(ans//2)

Submission status: Accepted

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

# print(a)

# print(n)
# print(a)
count = 0


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


print(int(count/2))

Submission status: Accepted

Solution code:
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))

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()))
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 = 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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

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().strip().split()))
count=0
for i in range(1,n+1):
    if a[a[i-1]-1]==i:
        count=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(1, (n + 1)):
    like1 = a[i - 1]
    like2 = a[like1 - 1]
    if i == like2:
        ans += 1
print(ans // 2)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

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
ans //= 2

print(ans)

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

Submission status: Accepted

Solution code:
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
    # print('dic', dic)
    for d in dic.keys():
        if dic[dic[d]] == d:
            ans += 1
    ans = int(ans/2)
    print(ans)
    return
resolve()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

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

Submission status: Accepted

Solution code:
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))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

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

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

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:
#!/usr/bin/env python3
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)

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

Submission status: Accepted

Solution code:
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)

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
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)