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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
like = [0] * n
for i in range(n):
    like[i] = a[i] - 1  # 0idx
ans = 0
for i in range(n):
    if like[like[i]] == i:
        ans += 1
print(ans // 2)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, 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
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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

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

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

Submission status: Accepted

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

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

print(ans//2)

Submission status: Accepted

Solution code:
# B - 仲良しうさぎ
# https://atcoder.jp/contests/code-festival-2016-quala/tasks/codefestival_2016_qualA_b

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

ans = 0
for itr, a in enumerate(A):
  if a - 1 > itr and A[a - 1] == itr + 1:
    ans += 1

print(ans)

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

if __name__ == "__main__":
  resolve()

Submission status: Accepted

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

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

print(point//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

Submission status: Accepted

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

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

ans //= 2
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
from collections import *

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

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt, ceil
from functools import lru_cache, reduce
INF = 1 << 60
MOD = 1000000007
sys.setrecursionlimit(10 ** 7)

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

def is_prime(n):
    if n == 1:
        return False
    for i in range(2,int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


# ワーシャルフロイド (任意の2頂点の対に対して最短経路を求める)
# 計算量n^3 (nは頂点の数)
def warshall_floyd(d, n):
    #d[i][j]: iからjへの最短距離
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = min(d[i][j],d[i][k] + d[k][j])
    return d


# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
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

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# 素数判定



# limit以下の素数を列挙
def eratosthenes(limit):
    A = [i for i in range(2, limit+1)]
    P = []

    while True:
        prime = min(A)
        
        if prime > sqrt(limit):
            break
            
        P.append(prime)
            
        i = 0
        while i < len(A):
            if A[i] % prime == 0:
                A.pop(i)
                continue
            i += 1
            
    for a in A:
        P.append(a)
            
    return P

# 同じものを含む順列
def permutation_with_duplicates(L):

    if L == []:
        return [[]]

    else:
        ret = []

        # set（集合）型で重複を削除、ソート
        S = sorted(set(L))

        for i in S:

            data = L[:]
            data.remove(i)

            for j in permutation_with_duplicates(data):
                ret.append([i] + j)

        return ret


# ここから書き始める
n = int(input())
a = list(map(int, input().split()))
ans = 0
for i in range(n):
    if a[a[i] - 1] == i + 1:
        ans +=  1
print(ans // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import itertools
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:
#!/usr/bin/env python
# coding: utf-8

# In[12]:


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


# In[13]:


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


# In[ ]:

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
readlines = sys.stdin.readlines
ns = lambda: readline().rstrip() # input string
ni = lambda: int(readline().rstrip()) # input int
nm = lambda: map(int, readline().split()) # input multiple int 
nl = lambda: list(map(int, readline().split())) # input multiple int to list

n = ni()
l = nl()
ans = 0

for i, usa in enumerate(l):
    if l[usa - 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()))

cnt = 0
chk = [False for i in range(n)]
for i in range(n):
	if chk[i]:
		continue
	chk[i] = True
	if i+1 == a[a[i]-1]:
		chk[a[i]-1] = True
		cnt += 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(int(ans/2))

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:

n = int(input())
a = list(map(int, input().split()))
flags = [False] * n
ans = 0
for i in range(n):
    if a[a[i] - 1] - 1 == i and flags[a[i] - 1] == False:
        ans += 1
        flags[a[a[i] - 1] - 1] = True

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0

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

print(ans//2)

Submission status: Accepted

Solution code:
import math
import copy

neko = 0
n = int(input())
a = [int(x) for x in input().split()]
for i in range(n):
    if i == a[a[i]-1]-1:
        neko += 1
print(int(neko/2))

Submission status: Accepted

Solution code:
from collections import Counter

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

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

def f(): 
    for i, a in enumerate(A, 1):
        if i < a:
            yield i, a
        else:
            yield a, i

c = Counter(f())
print(len([k for k, v in c.items() if v == 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] and i < a[i]:
        cnt += 1
print(cnt)

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

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
a = LI()
ans = 0
for i,x in enumerate(a,1):
    if x=='*':
        continue
    if a[x-1]==i:
        ans += 1
        a[x-1] = '*'
print(ans)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
a = LI()
count = 0
a_dict = {}
for i,x in enumerate(a,1):
    a_dict[i] = x
for k,v in a_dict.items():
    if k in [a_dict[v]]:
        count +=1
print(count//2)

Submission status: Accepted

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
N = I()
a = LI()
ans = 0
a_dict = {}
for i,x in enumerate(a,1):
    a_dict[i] = x
for k,v in a_dict.items():
    if k in [a_dict[v]]:
        ans +=1
print(ans//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
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 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 = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

d = defaultdict(list)
cnt = 0
for i, a in enumerate(A):
    if i+1 in d[a]:
        cnt += 1
    d[i+1].append(a)


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:
#print#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
sys.setrecursionlimit(10**9)
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))
#%%code
def resolve():
    N,=pin()
    A=[i-1 for i in pin()]
    cnt=0
    for j in range(N):
        if A[A[j]]==j:cnt+=1
    print(cnt//2)
    
#%%submit!
resolve()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(count // 2)

Submission status: Accepted

Solution code:
n = int(input())
a = [0]*(n+1)
a[1:] = map(int, input().split())
ans = set()
for i in a[1:]:
    if i==a[a[i]]:
        j, k = sorted([i, a[i]])
        ans |= {(j, k)}
print(len(ans))

Submission status: Accepted

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

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

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

print(ans//2)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import collections
import itertools as it
import math
#import numpy as np
 
#  = input()
n  = int(input())
#  = map(int, input().split())
a  = list(map(int, input().split()))
#  = [int(input()) for i in range(N)]
#
# c = collections.Counter()

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

Submission status: Accepted

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

main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, *a_l= map(int, open(0).read().split())
lovedBy = [[] for _ in range(n)]
for i, a in enumerate(a_l):
    lovedBy[a-1].append(i)
cnt = 0
for i, a in enumerate(a_l):
    if a-1 in lovedBy[i]:
        cnt += 1
print(cnt//2)

Submission status: Accepted

Solution code:
n = int(input())
a = tuple(map(lambda a:int(a)-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()))
c = 0
a = [0] + a
for i in range(n):
    if i == a[a[i]]:
        c += 1
else:
    print(c//2)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()

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

Submission status: Accepted

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

cnt = 0
index_list = list(range(0,n,1))

for i in (index_list):
    if a_list[a_list[i]-1] == i+1:
        cnt += 1
print(cnt//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):
    b=a[i]-1
    if(i==a[b]-1):
        ans+=1
print(ans//2)

Submission status: Accepted

Solution code:
import sys, heapq, bisect, math, fractions
from collections import deque

N = int(input())
A = list(map(int, sys.stdin.readline().rsplit()))

res = 0
for i, a in enumerate(A):
    if i == A[a - 1] - 1 and i < a - 1:
        res += 1

print(res)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    N=int(readline())
    a=list(map(lambda x:int(x)-1,readline().split()))

    flags=[False]*N
    res=0
    for i in range(N):
        j=a[i]
        if flags[j]==False and a[j]==i:
            flags[i]=True
            flags[j]=True
            res+=1

    print(res)
    
if __name__=="__main__":
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

n = int(input())
a = [int(x) for x in input().split()]
ans = set()
for i in range(n):
    j = a[i] - 1
    if (i not in ans) and (j not in ans) and a[j] == i + 1:
        ans.add(j)
print(len(ans))

Submission status: Accepted

Solution code:
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
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
#s=input().rstrip()
n=I()
lis=LI()
liked=[[] for i in range(n)]
for i in range(n):
    liked[lis[i]-1].append(i+1)
#print(liked)
count=0
for i in range(n):
    if lis[i] in liked[i]:
        count+=1
print(count//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
*A,=map(int, input().split())
A=[-1]+A
ans=0
for i in range(1,N+1):
    j=A[i]
    if j>i and A[j]==i: ans+=1
print(ans)

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())
    A = readInts()
    Pa = [0 for _ in range(n)]
    for i in range(n):
        Pa[i] = A[i] - 1
    cnt = 0
    for i in range(n):
        if i == Pa[Pa[i]]:
            cnt += 1
    print(cnt//2)
if __name__ == '__main__':
  main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right
# from math import gcd,ceil,floor,factorial
# from fractions import gcd
from functools import reduce
from pprint import pprint

INF = float("inf")

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

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    # lsはソート済みであること
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n = int(input())
a = list(myinput())

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
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

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

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

    print(ans)

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
    a[i] = ""
print(cnt)

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:
import sys
input = sys.stdin.readline
from collections import *

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

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

Submission status: Accepted

Solution code:
N = int(input())
a = list(map(int, input().split()))
pair = {}
ans = 0
for i in range(N):
    pair[i + 1] = a[i]

for key, value in pair.items():
    if pair[value] == key:
        ans += 1

print(ans // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
like=list(map(int,input().split()))
count=0

for i in range(N):
    like_1=like[i]
    #print("like_1")
    #print(like_1)
    if like[like_1-1]==i+1:
        #print(like[like_1-1])
        #print(i+1)
        count+=1
        #print("///")
        
print(count//2)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(cnt // 2)

Submission status: Accepted

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

G = [[] for _ in range(n + 1)]

for i in range(1, n + 1):
	G[a[i]].append(i)

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

Submission status: Accepted

Solution code:
#23:16

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

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

print(int(ans/2))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in 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:
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)