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:
import sys
## io ##
def IS(): return sys.stdin.readline().rstrip()
def II(): return int(IS())
def MII(): return list(map(int, IS().split()))
def MIIZ(): return list(map(lambda x: x-1, MII()))
## dp ##
def DD2(d1,d2,init=0): return [[init]*d2 for _ in range(d1)]
def DD3(d1,d2,d3,init=0): return [DD2(d2,d3,init) for _ in range(d1)]
## math ##
def to_bin(x: int) -> str: return format(x, 'b') # rev => int(res, 2)
def to_oct(x: int) -> str: return format(x, 'o') # rev => int(res, 8)
def to_hex(x: int) -> str: return format(x, 'x') # rev => int(res, 16)
MOD=10**9+7
def divc(x,y) -> int: return -(-x//y)
def divf(x,y) -> int: return x//y
def gcd(x,y):
    while y: x,y = y,x%y
    return x
def lcm(x,y): return x*y//gcd(x,y)
def enumerate_divs(n):
    """Return a tuple list of divisor of n"""
    return [(i,n//i) for i in range(1,int(n**0.5)+1) if n%i==0]
def get_primes(MAX_NUM=10**3):
    """Return a list of prime numbers n or less"""
    is_prime = [True]*(MAX_NUM+1)
    is_prime[0] = is_prime[1] = False
    for i in range(2, int(MAX_NUM**0.5)+1):
        if not is_prime[i]: continue
        for j in range(i*2, MAX_NUM+1, i):
            is_prime[j] = False
    return [i for i in range(MAX_NUM+1) if is_prime[i]]
## libs ##
from itertools import accumulate as acc, combinations as combi, product, combinations_with_replacement as combi_dup
from collections import deque, Counter
from heapq import heapify, heappop, heappush
from bisect import bisect_left
#======================================================#

def main():
    n = II()
    aa = MII()
    l = set()
    cnt = 0
    for i, a in enumerate(aa):
        if (i+1,a) in l:
            cnt += 1
        else:
            l.add((i+1,a))
            l.add((a,i+1))
    print(cnt)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
from collections import defaultdict
d = defaultdict(set)
ans = 0
for i, e in enumerate(a, start=1):
  d[i].add(e)
  if i in d[e]:
    ans += 1
print(ans)

Submission status: Accepted

Solution code:

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

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

Submission status: Accepted

Solution code:
import sys
rs = lambda: sys.stdin.readline().rstrip()
ri = lambda: int(rs())
rs_ = lambda: [_ for _ in rs().split()]
ri_ = lambda: [int(_) for _ in rs().split()]

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

Submission status: Accepted

Solution code:
# CODE FESTIVAL 2016 予選 A: B – 仲良しうさぎ / Friendly Rabbits
N = int(input()) - 1
a = [int(s) - 1 for s in input().split()]

pairs = 0

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

print(pairs)

Submission status: Accepted

Solution code:
import sys
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = list(map(int, input().split()))
to = [0 for _ in range(n + 1)]

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    A = list(map(int, readline().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:
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

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

def readInts():
  return list(map(int,input().split()))
def main():
    n = int(input())
    RA = readInts()
    ans = 0
    for i in range(n):
        if RA[RA[i]-1] == i+1:
            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, ai in enumerate(A):
    if A[ai-1] == i+1:
        ans += 1
        
print(ans // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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


def resolve():
    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)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

counter = 0

for i,j in a.items():
    if i == a[j]:
        #print(i,j)
        counter += 1

print(counter//2)

Submission status: Accepted

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

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

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()))
num = set()
ans = 0
for i in range(N):
    # print(a[a[i]-1], a[i])
    if a[a[i]-1] == i+1:
        ans += 1

print(ans//2)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, *a = map(int, read().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:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby, product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

    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)

if __name__ == '__main__':
    main()

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

Submission status: Accepted

Solution code:
# from math import factorial,sqrt,ceil,gcd
# from itertools import permutations as permus
# from collections import deque,Counter
# import re
# from functools import lru_cache # 簡単メモ化 @lru_cache(maxsize=1000)
# from decimal import Decimal, getcontext
# # getcontext().prec = 1000
# # eps = Decimal(10) ** (-100)

import numpy as np
# import networkx as nx
# from scipy.sparse.csgraph import shortest_path, dijkstra, floyd_warshall, bellman_ford, johnson
# from scipy.sparse import csr_matrix
# from scipy.special import comb

# slist = "abcdefghijklmnopqrstuvwxyz"
N = int(input())
arrA = np.array(input().split(),dtype=np.int64)
arrA = arrA -1

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

print(ans//2)
# print(*ans)   # unpackして出力。間にスペースが入る
# for row in board:
#     print(*row,sep="")    #unpackして間にスペース入れずに出力する
# print("{:.10f}".format(ans))
# print("{:0=10d}".format(ans))

Submission status: Accepted

Solution code:
def myAnswer(N:int,A:list) -> int:
   counter = 0
   for i in range(N):
      if(A[A[i]-1] == i + 1):
         counter += 1
   return counter//2


def modelAnswer():
   tmp=1
def main():
   N = int(input())
   A = list(map(int,input().split()))
   print(myAnswer(N,A[:]))
if __name__ == '__main__':
   main()

Submission status: Accepted

Solution code:
def ii():return int(input())
def iim():return map(int,input().split())
def iil():return list(map(int,input().split()))
def ism():return map(str,input().split())
def isl():return list(map(str,input().split()))

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

Submission status: Accepted

Solution code:
*a, = map(int, open(0).read().split())
c = 0
for i, j in enumerate(a):
  c += (a[j:j+1] == [i])*(i > 0)
print(c // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().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)

Submission status: Accepted

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

like = {}
ans = 0

for i, r in enumerate(a):
    like[i+1] = r
    if r in like and like[r] == i+1:
        ans += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = list(map(int, input().split()))
checked = [False] * n
ans = 0
for i, a in enumerate(A):
    if checked[i]:
        continue
    if A[a - 1] - 1 == i:
        ans += 1
        checked[i] = True
        checked[a - 1] = True
print(ans)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.buffer.readline().split()]
def I(): return int(sys.stdin.buffer.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    for i in range(n):
        a[i] -= 1
    ans = 0
    for i in range(n):
        ans += (a[a[i]] == i)&(a[i] > i)
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(cnt//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import collections
import sys
import numpy as np
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
MOD = 10**9+7
import itertools
import math

n = int(input())
a = list(map(int,input().split()))
pair = []
for i in range(len(a)):
    koho = [i,a[i]-1]
    koho.sort()
    pair.append(koho)
pair.sort()
cnt = 0
for i in range(len(pair)-1):
    if pair[i] == pair[i+1]:
        cnt+=1
print(cnt)

Submission status: Accepted

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

print(ans // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=[0]*n
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()))
res = 0
for i, val in enumerate(a):
  if a[val - 1] == i + 1:
    res += 1
print(res//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
print(ans//2)

Submission status: Accepted

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

print(count // 2)

Submission status: Accepted

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

count = 0
for i in range(N):
    # print(a[a[i]-1])
    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()))

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
A = [int(x) for x in input().split()]
dict = {}
res = 0
for i, a in enumerate(A, 1):
  dict[i] = a
for i in range(1, n+1):
  if dict[i] in dict.keys() and dict[dict[i]] == i:
    res += 1
print(res // 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:
N = int(input())
a = list(map(lambda s: int(s) - 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())
ans=0
table=[-1]*n
for i,j in enumerate(list(map(int,input().split()))):
    if table[j-1]==-1:table[i]=j-1
    elif table[j-1]==i:ans+=1
print(ans)

Submission status: Accepted

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

for i, j in enumerate(a):
    ai.append([i+1,j])
    ai[i].sort()

print(len(ai) - len(list(map(list,set(map(tuple, ai))))))

Submission status: Accepted

Solution code:
n=int(input())
a=[int(x) for x in input().split()]
b=[]
for i in range(n):
    if a[i]==i+1:continue
    if a[a[i]-1]==i+1:
        t=[a[i]-1,i]
        b.append(sorted(t))
# print(b)
print(len(b)//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
a = [int(i) for i in input().split()]

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

Submission status: Accepted

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

b = []
c = []
for i in range(n):
	b.append((i, a[i]))
	c.append((a[i], i))

c = set(c)
b = set(b)

ans = len(b & c)
print(ans//2)

Submission status: Accepted

Solution code:
def resolve():

    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)



resolve()

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n = int(input())
A = list(map(int,input().split()))
for i in range(n): A[i] -= 1
for i in range(n):
  if not A[i] > i:continue
  else:
    if A[A[i]] == i:ans += 1
print(ans)

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

    print(ans//2)

Submission status: Accepted

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

Submission status: Accepted

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

B=[0]*N

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

print(sum(B)//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:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
res = 0
for x in range(N):
  y = a[x]
  if x + 1 == a[y - 1]: res += 1
print(res // 2)

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, acos, atan, asin, log, log10
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 debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(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()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 0, 1, -1, 1, -1, -1, 1]
dy = [1, -1, 0, 0, 1, -1, 1, -1]
debugmode = False

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

Submission status: Accepted

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

Submission status: Accepted

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

print(int(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 a[a[i]-1] == (i+1):
        cnt+=1
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import Counter
n = int(input())
A = list(map(int, input().split()))
p = []
for i, a in enumerate(A):
    idx = i+1
    if a>idx:
        a,idx = idx, a
    p.append((a, idx))
c = Counter(p)
ans = 0
for k,v in c.items():
    if v==2:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i)-1 for i in input().split(" ")]
count = 0
for i in range(N):
    # 相手が自分を好いているか確認
    if i==A[A[i]]:
        count+=1
# 各ペア1回ずつ多く数えているので半分に
pair = count//2
print(pair)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

a = list(map(int, input().split()))
a = np.array(a)
a = a - 1

cnt = 0

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

print(cnt//2)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    a = LI()
    ans = 0
    for i in range(n):
        ans += a[a[i] - 1] - 1 == i
    print(ans//2)
    return


#main
if __name__ == '__main__':
    solve()

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(lambda x: int(x) - 1, input().split()))
checked = [False] * N
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
a = [int(x)-1 for x 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=[0]+list(map(int,input().split()))
cnt=0

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

Submission status: Accepted

Solution code:
def main():
    rabbits = int(input())
    like = list(map(int, input().split()))
    answer = 0
    for i in range(rabbits):
        if like[like[i] - 1] == i + 1:
            answer += 1
    print(answer // 2)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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

cnt //= 2
print(cnt)

Submission status: Accepted

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

st = set()
cnt = 0
for i, e in enumerate(a, 1):
    pair = min(i, e), max(i, e)
    if pair in st:
        cnt += 1
    else:
        st.add(pair)

print(cnt)

Submission status: Accepted

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