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

Submission status: Accepted

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

cnt = 0
for idx in range(len(a)):
    lovers_love = a[a[idx]]
    if lovers_love == -1:
        continue
    if lovers_love == idx:
        cnt += 1
        a[a[idx]] = -1

print(cnt)

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 a[j]==i+1:
    ans+=1
print(ans//2)

Submission status: Accepted

Solution code:
#解説AC
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
ans = cnt // 2
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[a[i]-1]:
        cnt += 1
ans = cnt // 2
print(ans)

Submission status: Accepted

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

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()))
cnt = 0
for i in range(n):
    if a[a[i]-1] == i+1:
        cnt += 1
print(cnt >> 1)

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, a in enumerate(A):
    if A[a - 1] == i + 1:
        ans += 1
ans //= 2
print(ans)

Submission status: Accepted

Solution code:
n, *a = map(int, open(0).read().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
for itr, val in enumerate(A):
  if A[val - 1] == itr + 1:
    cnt += 1
    
print(cnt // 2)

Submission status: Accepted

Solution code:
if __name__ == "__main__":
    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()))

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

Submission status: Accepted

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

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


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 a[a[i]-1]==i+1:
        ans+=1
print(ans//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin,setrecursionlimit
setrecursionlimit(10**7)
n = int(stdin.readline().rstrip())
li = list(map(int,stdin.readline().rstrip().split()))
point = 1
for i,j in enumerate(li):
    if li[j-1] == i+1:
        point += 1
point //= 2
print(point)

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int,input().split()))
l = [set([]) for k in range(N+1)]
ans = 0
for k in range(N):
    if k+1 in l[a[k]]:
        ans += 1
    l[k+1].add(a[k])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

    edge = [set() for _ in range(N)]

    for i, a in enumerate(A):
        edge[i].add(a)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7

n = INT()
a = LIST()
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()]

from collections import defaultdict

flag = [True] * N

cnt = 0
for i in range(N):
    if flag[A[i]] and flag[i] and A[A[i]] == i:
        cnt += 1
        flag[A[i]] = False
        flag[i] = False

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

Submission status: Accepted

Solution code:
import sys

def solve():
    input = sys.stdin.readline
    N = int(input())
    pair = 0
    A = [int(a) for a in input().split()]
    for i, a in enumerate(A):
        if A[a-1] == i + 1: pair += 1
    print(pair // 2)

    return 0

if __name__ =="__main__":
    solve()

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:
def main():
    n = int(input())
    a = list(map(int, input().split()))
    f = [True]*n
    ans = 0
    for i in range(n):
        if f[i]:
            f[i] = False
            if a[a[i]-1]-1 == i:
                ans += 1
                f[a[i]-1] = False
    print(ans)

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):
    j = a[i] - 1
    if i < j and a[j] - 1 == i:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
a = input().split()
a = np.array(a).astype(int)
count = 0
for i,ai in enumerate(a):
    
    if a[ai-1] == i+1:
        count = count + 1
print(count//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int,input().split()))
l=[]
buf=[]
for i in range(N):
    buf=[a[i],i+1]
    buf.sort()
    l.append(buf[0]*N+buf[1])
print(len(l)-len(set(l)))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(cnt//2)

Submission status: Accepted

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

for i in range(N):
    if alist[alist[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 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(1,n+1):
    if a[i-1]>i and a[a[i-1]-1]==i:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
    t = a[i]-1
    if a[t] - 1 == i:
        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(ans//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
dat = list(map(int, input().split()))
d = dict()
res = 0
for i in range(n):
    d[i+1] = dat[i]
    if d.get(dat[i], -1) == (i + 1):
        res += 1
print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 数えていく おしまい
n = int(input())
used = [False for _ in range(n)]
arr = list(map(int, input().split()))
ans = 0
for i, a in enumerate(arr):
    if i + 1 == arr[a - 1]:
        if used[i] == False:
            ans += 1
            used[i] = True
            used[a - 1] = True
print(ans)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
A = np.array(tuple(map(int, input().split())))
print(np.count_nonzero(A[A - 1] == np.arange(N) + 1) // 2)

Submission status: Accepted

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

n = int(input())
a = [int(x) for x in input().split()]
c = 0
for i, ax in enumerate(a):
    if a[ax - 1] == 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):
    # print('--', i, A[i])
    if i+1 == A[A[i]-1]:
        cnt += 1

ans = int(cnt//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())
a = list(map(int, input().split()))
cnt = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print([A[i] - 1 > i and A[A[i] - 1] - 1 == i for i in range(N)].count(1))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import fractions
import bisect
import collections
import itertools
import heapq
import string
import sys
import copy
from collections import deque
sys.setrecursionlimit(10**7)
def gcd(a,b):return fractions.gcd(a,b) #最大公約数
def lcm(a,b):return (a*b) // fractions.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def isn(): return input().split() #文字列読み込み
def imn(): return map(int, input().split()) #整数map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def perm_count(n, r): return math.factorial(n) // math.factorial(n-r) # 順列の総数
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def comb_count(n, r): return math.factorial(n) // (math.factorial(n-r) * math.factorial(r)) #組み合わせの総数
def two_distance(a, b, c, d): return ((c-a)**2 + (d-b)**2)**.5 # 2点間の距離

N = iin()
a = list(map(lambda x:int(x)-1, input().split()))

cnt = 0
for i in range(N):
    if i == a[a[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:
p = int(input())
ans = 0
*A,= map(int,input().split())
for j,i in enumerate(A):
  if A[A[j]-1]==j+1:
    ans+=1
print(ans//2)

Submission status: Accepted

Solution code:
from sys import stdin

n = int(stdin.readline().rstrip())
li = [1]+list(map(int,stdin.readline().rstrip().split()))
point = 0
for i in range(1,n+1):
    if li[li[i]] == i:
        point += 1
print(point//2)

Submission status: Accepted

Solution code:
def main():
    N = int(input())
    A = [int(x)-1 for x in input().split()]
    ans = 0
    for i in range(N):
        if i==A[A[i]]:
            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 index, x in enumerate(a):
    # print (x)
    if index + 1 == a[x - 1]:
        count += 1

print (count // 2)

Submission status: Accepted

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

cnt = 0

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

print(int(cnt * 0.5))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
cnt=[i for i in range(n) if a[a[i]-1]-1==i]
print(len(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]-1==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 i+1 == A[A[i]-1]: ans += 1
print(ans//2)

Submission status: Accepted

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

ans = 0
for idx,a in enumerate(al):
   if al[a-1]-1 == idx:
      ans += 1
print(ans//2)

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 = ans + 1
		#print(i,l[i],l[l[i]-1],i+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):
  j = a[i]-1
  if a[j] == i+1:
    ans+=1
print(ans//2)

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
seen = [0] * n
for i, ai in enumerate(A):
    if seen[i]:
        continue
    seen[i] = 1
    if A[ai] == i:
        ans += 1
        seen[ai] = 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 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()))

count=0
for i in range(N):
    #print(A[i],A[A[i]-1])
    if i+1==A[A[i]-1]:
        count+=1
        A[i]=-1
print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict
n=int(input())
a=[int(i) for i in input().split()]

dic = defaultdict(set)
ans=0
for i in range(n):
    dic[a[i]].add(i+1)
    if a[i] in dic[i+1]:
        ans+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = [int(x) - 1 for x in input().split()]

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

print(cnt//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:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().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
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32
from bisect import bisect_left, bisect_right

def solve(N: int, a: "List[int]"):
    
    ans = 0
    for i in range(N):
        if a[a[i]-1] == i+1:
            ans += 1

    print(ans//2)


    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32
from bisect import bisect_left, bisect_right

def solve(N: int, a: "List[int]"):
    # x = [[i+1, a[i]] for i in range(N)]

    # x = sorted(x, key=lambda x:x[1])
    # x1 = [i for i,j in x]
    # x2 = [j for i,j in x]

    # ans = 0
    # for i in range(N):
    #     p = bisect_left(x2, x1[i])
    #     if not (0 <= p < N):
    #         continue
    #     if x1[i] == x2[p] and x2[i] == x1[p]:
    #         ans += 1

    # print(ans//2)

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

    print(ans//2)


    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ret = 0
for i in range(N) :
    if i == a[a[i] - 1] - 1 :
        ret += 1
print(ret // 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+1==A[a-1]:
    ans+=1
print(ans//2)

Submission status: Accepted

Solution code:
import sys

stdin = sys.stdin
 
ri = lambda: int(rs())
rl = lambda: list(map(int, stdin.readline().split()))
rs = lambda: stdin.readline().rstrip()  # ignore trailing spaces

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

print(answer//2)

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[i] > i and a[a[i]] == i:
        ans += 1

print (ans)

Submission status: Accepted

Solution code:
import collections
import sys

readline = sys.stdin.buffer.readline


def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    B = list(range(1, N + 1))

    C = collections.defaultdict(int)
    for a, b, in zip(A, B):
        a, b = min(a, b), max(a, b)
        C[(a, b)] += 1

    D = collections.Counter(C.values())
    print(D[2])


main()

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:
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:
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(_) for _ in stdin.readline().rstrip().split()]
A = {frozenset([i+1, a]) for i, a in enumerate(A)}
print(N-len(A))

Submission status: Accepted

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

G = [[] for _ in range(N)]

for i in range(N):
    G[A[i]-1].append(i)

ans = 0

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

print(int(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

print(ans // 2)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
# ペアの数だけsetで消されるからn-len(s)で差を取るとペアの数がとってこれる
# sortedとset({}で表される)がキーになっている
s = {tuple(sorted([a[i],i+1]))for i in range(n)}
print(n-len(s))

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
s = { tuple(sorted([A[i], i+1])) for i in range(n) }
print(n-len(s))

Submission status: Accepted

Solution code:
# 2019-11-26 17:11:55(JST)
import sys
def main():
    n, *a = map(int, sys.stdin.read().split())
    a = [None] + a

    cnt = 0
    for i in range(1, n):
        j = a[i]
        if j > i and a[j] == i:
            cnt += 1
    
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
*a,=map(int,open(0).read().split());print(sum(a[a[i]]==i for i in range(len(a)))//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
N=int(input())
table=collections.defaultdict(int)
a=list(map(int,input().split()))
for i,x in enumerate(a):
  table[i+1]=x
ans=0
for x in table:
  b=table[x]
  if table[b]==x:
    ans+=1
    table[x]=0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    cnt = 0
    for i in range(n):
        if i + 1 == a[a[i]-1]:
            cnt += 1
    print(cnt // 2)


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N=int(input())
a=[N+1]
a.extend(list(map(int,input().split())))

ans=sum([a[a[i]]==i for i in range(1,N+1)])//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 i+1==a[j-1]:
        cnt+=1
print(cnt//2)

Submission status: Accepted

Solution code:
from operator import mul
from functools import reduce
from collections import Counter
# from itertools import combinations as comb
# from itertools import permutations as perm
# from copy import copy
# 配列二分法アルゴリズム
# https://docs.python.jp/3/library/bisect.html
# import bisect
# 桁数指定
# print('{:.3f}'.format(X))
# from collections import defaultdict
# dic = defaultdict(lambda: ...)
# 値で辞書をソート
# sorted(dic.items(), key=lambda x:x[1])
# ヒープキュー
# https://docs.python.org/ja/3/library/heapq.html
# import heapq
# 正規表現(regular expression)のためのモジュール
# import re

import sys

sys.setrecursionlimit(2000)


def inpl():
    return list(map(int, input().split()))


# 重複組み合わせは nHr = (n + r - 1)Cn
def cmb(n, r):
    # combination
    if n < r:
        return 0
    r = min(n - r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


def gcd(a, b):
    # greatest common divisor
    while b > 0:
        a, b = b, a % b

    return a


def lcm(a, b):
    # least common multiple
    return a * b // gcd(a, b)


N = int(input())
A = inpl()
ans = 0
for i, a in enumerate(A):
    if A[a - 1] == i + 1:
        ans += 1

print(ans // 2)

Submission status: Accepted

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

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

print(cnt//2)

Submission status: Accepted

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

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

print(answer // 2)

Submission status: Accepted

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

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):
    if a[a[i]]==i:
        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(res // 2)

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools
sys.setrecursionlimit(10**8)
mod = 10**9+7
mod2 = 998244353
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

n = inp()
a = inpl()
b = [False] * n
res = 0
for i in range(n):
    if b[i]:
        continue
    if a[a[i]-1] == i+1:
        res += 1
        b[a[i]-1] = True
print(res)

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+=0.5
print(int(ans))

Submission status: Accepted

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

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

print(answer//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:
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
L = []
for i, a in enumerate(A, 1):
    L.append(tuple(sorted([i, a])))
cnt = Counter(L)
print(list(cnt.values()).count(2))

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = {i+1: v for i, v in enumerate(a)}
cnt = 0
for k, v in a.items():
    if k == a[v]:
        cnt += 1
print(cnt//2)

Submission status: Accepted

Solution code:
from collections import defaultdict

n = int(input())
a = list(map(int, input().split()))
a = {i+1: v for i, v in enumerate(a)}
cnt = 0
for k, v in a.items():
    if k == a[v]:
        cnt += 1
print(cnt//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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 += 0.5
print(int(ans))