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:
def main():
    N = int(input())
    A = [int(a) for a in input().split()]

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

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

    print(ans)

if __name__ == "__main__":
    main()

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

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

if __name__ == '__main__':
    main()

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()))
a.insert(0,0)
ans=0

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

print(ans//2)

Submission status: Accepted

Solution code:
import collections

n = int(input())
a = list(map(int, input().split()))
ans = 0

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

print(ans//2)

Submission status: Accepted

Solution code:
# n, m, l = map(int, input().split())

# list_n = list(map(int, input().split()))

# n = input()
# list = [input() for i in range(N)

# list = [[i for i in range(N)] for _ in range(M)]

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

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

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

ans=0

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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:

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

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

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


CODE_festival_2016_A()

Submission status: Accepted

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

    print(cnt//2)

if __name__ == '__main__':
    main()

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

print(ans//2)

Submission status: Accepted

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

print(count // 2)

Submission status: Accepted

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

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

print(count // 2)

Submission status: Accepted

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

print(int(count/2))

Submission status: Accepted

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

Submission status: Accepted

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

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

print(total // 2)

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

Submission status: Accepted

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

import sys
from typing import Any, Callable, Deque, Dict, List, Mapping, Optional, Sequence, Set, Tuple, TypeVar, Union
# import time
# import math, cmath
# import numpy as np
# import scipy.sparse.csgraph as cs            # csgraph_from_dense(ndarray, null_value=inf), bellman_ford(G, return_predecessors=True), dijkstra, floyd_warshall
# import random                                # random, uniform, randint, randrange, shuffle, sample
# import string                                # ascii_lowercase, ascii_uppercase, ascii_letters, digits, hexdigits
# import re                                    # re.compile(pattern) => ptn obj; p.search(s), p.match(s), p.finditer(s) => match obj; p.sub(after, s)
# from bisect import bisect_left, bisect_right # bisect_left(a, x, lo=0, hi=len(a)) returns i such that all(val<x for val in a[lo:i]) and all(val>-=x for val in a[i:hi]).
# from collections import deque                # deque class. deque(L): dq.append(x), dq.appendleft(x), dq.pop(), dq.popleft(), dq.rotate()
# from collections import defaultdict          # subclass of dict. defaultdict(facroty)
# from collections import Counter              # subclass of dict. Counter(iter): c.elements(), c.most_common(n), c.subtract(iter)
# from datetime import date, datetime          # date.today(), date(year,month,day) => date obj; datetime.now(), datetime(year,month,day,hour,second,microsecond) => datetime obj; subtraction => timedelta obj
# from datetime.datetime import strptime       # strptime('2019/01/01 10:05:20', '%Y/%m/%d/ %H:%M:%S') returns datetime obj
# from datetime import timedelta               # td.days, td.seconds, td.microseconds, td.total_seconds(). abs function is also available.
# from copy import copy, deepcopy              # use deepcopy to copy multi-dimentional matrix without reference
# from functools import reduce                 # reduce(f, iter[, init])
# from functools import lru_cache              # @lrucache ...arguments of functions should be able to be keys of dict (e.g. list is not allowed)
# from heapq import heapify, heappush, heappop # built-in list. heapify(L) changes list in-place to min-heap in O(n), heappush(heapL, x) and heappop(heapL) in O(lgn).
# from heapq import _heapify_max, _heappop_max, _siftdown_max
# from heapq import nlargest, nsmallest        # nlargest(n, iter[, key]) returns k-largest-list in O(n+klgn).
# from itertools import count, cycle, repeat   # count(start[,step]), cycle(iter), repeat(elm[,n])
# from itertools import groupby                # [(k, list(g)) for k, g in groupby('000112')] returns [('0',['0','0','0']), ('1',['1','1']), ('2',['2'])]
# from itertools import starmap                # starmap(pow, [[2,5], [3,2]]) returns [32, 9]
# from itertools import product, permutations  # product(iter, repeat=n), permutations(iter[,r])
# from itertools import combinations, combinations_with_replacement
# from itertools import accumulate             # accumulate(iter[, f])
# from operator import itemgetter              # itemgetter(1), itemgetter('key')
# from fractions import Fraction               # Fraction(a, b) => a / b ∈ Q. note: Fraction(0.1) do not returns Fraciton(1, 10). Fraction('0.1') returns Fraction(1, 10)



def main():
    Num = Union[int, float]
    mod = 1000000007                # 10^9+7
    inf = float('inf')              # sys.float_info.max = 1.79e+308
    # inf = 2 ** 63 - 1             # (for fast JIT compile in PyPy) 9.22e+18
    sys.setrecursionlimit(10**6)    # 1000 -> 1000000
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def isp():   return input().split()
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    # def _heappush_max(h, item): h.append(item); _siftdown_max(h, 0, len(h)-1)
    
    
    n = ii()
    L = lmi_0()

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

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

print(cnt // 2)

Submission status: Accepted

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

print(count)

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

Submission status: Accepted

Solution code:
from collections import defaultdict

def readInt():
	return int(input())
def readInts():
	return list(map(int, input().split()))
def readChar():
	return input()
def readChars():
	return input().split()

n = readInt()
a = readInts()
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:
## B - 仲良しうさぎ
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=[0]+list(map(int,input().split()))
ans=0
for i in range(1,N+1):
  B=A[i]
  if A[B]==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[i] == -1:
        pass
    elif a[a[i] - 1] - 1 == i:
        c += 1
        a[a[i] - 1] = -1
        a[i] = -1
    else:
        pass

print(c)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
for i in range(n): a[i] -= 1
ans = sum([i == a[a[i]] for i in range(n)])
print(ans//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(int(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:
def main():
    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)
        
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7

N = i()
a = l()
L = [[] for _ in range(N)]
ans = 0
for i in range(N):
    L[i].append(a[i])
    if i+1 in L[a[i]-1]:
        ans += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def resolve():
    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)
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()))
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()))
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 = [int(i) for i in input().split() ]

nk = [0]*N
count = 0
for k,v in enumerate(a):
  if k+1 != v :
    nk[k] = v
    if nk[v-1] == k+1 :
      count += 1

print(count)

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[i]<i:
        #既にチェック済み
        continue
    if i==A[A[i]]:
        ans+=1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees#, log2, log
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
from decimal import Decimal
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10**9 + 7
from decimal import *

N = INT()
a = LIST()

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,j) in zip(range(n),a):
  if i==a[j-1]-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):
	#rint(A[i],i+1)
	if A[A[i]-1] == i+1:
		cnt += 0.5


print(int(cnt))

Submission status: Accepted

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

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

Submission status: Accepted

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

counter = 0

for i, A_i in enumerate(A):
  if A[A[i]-1] == i + 1 :
    counter += 1
  
print(int(counter/2))

Submission status: Accepted

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

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
N = I()
A = IL()
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:
import sys
import math
def I():return int(sys.stdin.readline().replace("\n",""))
def I2():return map(int,sys.stdin.readline().replace("\n","").split())
def S():return str(sys.stdin.readline().replace("\n",""))
def L():return list(sys.stdin.readline().replace("\n",""))
def Intl():return [int(k) for k in sys.stdin.readline().replace("\n","").split()]
def Lx(k):return list(map(lambda x:int(x)*-k,sys.stdin.readline().replace("\n","").split()))

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

Submission status: Accepted

Solution code:
import sys
import collections

def swap(t):
    if t[1] < t[0] :
        return (t[1],t[0])
    else:
        return t

n = int(sys.stdin.readline().rstrip())
a = [int(x) for x in sys.stdin.readline().rstrip().split()]

zippeda = list(map(swap,list(zip(range(1,n+1),a))))
zippeda.sort()
c = collections.Counter(zippeda)
counter = 0
for a in c.most_common():
    if a[1] < 2:
        break
    else:
        counter = counter + 1

print(counter)

Submission status: Accepted

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

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

_, *A = map(int1, read().split())

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

print(ans // 2)

Submission status: Accepted

Solution code:
N = int(input())
a_list = [0]+[int(e) for e in input().split()]

ans = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
ans = 0

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

print(ans//2)

Submission status: Accepted

Solution code:
# For taking integer inputs.
import math


def inp():
    return(int(input()))


# For taking List inputs.
def inlist():
    return(list(map(int, input().split())))


# For taking string inputs. Actually it returns a List of Characters, instead of a string, which is easier to use in Python, because in Python, Strings are Immutable.
def instr():
    s = input()
    return(list(s[:len(s)]))


# For taking space seperated integer variable inputs.
def invr():
    return(map(int, input().split()))


n = inp()

a = inlist()
b = [0]
b += a
p = 0
for i in range(1, n+1):
    if b[b[i]] == i and b[i] > 0:
        p += 1


print(p//2)

Submission status: Accepted

Solution code:
input()
LOVE = [int(i) for i in input().split(' ')]
print(int(len([1 for i in range(len(LOVE)) if i + 1 == LOVE[LOVE[i] - 1]]) / 2))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
print(sum(i+1==a[a[i]-1] for i in range(n))//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]
  if l[x-1]-1 == i:
    count += 1

count = count//2
print(int(count))

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

Submission status: Accepted

Solution code:

def main():
    n = int(input())
    a_s = list(map(int,input().split()))
    dct = {}
    for i,value in enumerate(a_s):
        i = i+1
        dct[i] = value
    counter = 0
    for key in dct.keys():
        if dct[dct[key]] == key:
            counter += 1
    print(counter//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 i+1 == a[a[i]-1]:
    cnt+=1
print(cnt//2)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) - 1 for i in input().split()]
print(len([1 for i, a in enumerate(A) if i == A[a] and i < a]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int,input().split())
i=0
ans=0
while i<N:
    if i+1==A[A[i]-1]:
        ans+=1
    i+=1
print(ans//2)

Submission status: Accepted

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

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 = list(map(int,input().split()))

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

print(cnt//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    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
    ans //= 2

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

Submission status: Accepted

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

paired = [0] * n
c = 0
for i in range(n-1):
    if paired[i]:
        continue

    if A[A[i]] == i:
        c += 1
        paired[A[i]] = 1

print(c)

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/code-festival-2016-quala/tasks/codefestival_2016_qualA_b

n = int(input())
nums = [int(i) for i in input().split()]

ans = 0
d = set()
for i in range(n):
    a = i + 1
    b = nums[i]
    if (a, b) in d or (b, a) in d:
        ans += 1
    else:
        d.add((a, b))
        d.add((b, a))
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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