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
used=[]
for i in range(n):
    if a[a[i]-1]==i+1 and i not in used:
        ans+=1
        used.append(a[i]-1)
print(ans)

Submission status: Accepted

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

for i in range(n):
  if aa[i]<0:
    continue
  if aa[aa[i]-1]-1==i:
    aa[aa[i]-1] = aa[i] = -1

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

print(ans//2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

N = int(input())
A = np.array(list(map(int, input().split())), dtype=int) - 1
print((A[A] == np.arange(N)).sum() // 2)

Submission status: Accepted

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


if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

Submission status: Accepted

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

print(ans // 2)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = [i - 1 for i in list(map(int, input().rstrip('\n').split()))]
    cnt = 0
    for i in range(len(a)):
        if i == a[a[i]]:
            cnt += 1
    print(cnt // 2)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
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)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007


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


print(cnt // 2)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8

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

for index, num in enumerate(A, 1):
    if index == A[num - 1]:
        cnt += 1
print(cnt // 2)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
c=0
for i in range(N):
  if A[A[i]-1]==i+1:
    c+=1
print(c//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:
def slove():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip('\n'))
    a = list(map(int, input().rstrip('\n').split()))
    cnt = 0
    for i in range(n):
        if i + 1 == a[a[i]-1]:
            cnt += 1
    print(cnt // 2)


if __name__ == '__main__':
    slove()

Submission status: Accepted

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

ll = [sorted((idx+1,i)) for idx,i in enumerate(l)]
ll = set([tuple(i) for i in ll])

print(len(l) - len(ll))

Submission status: Accepted

Solution code:
def main():

    N = int(input())
    A = list(map(int, input().split()))
    visited = set()
    count = 0
    for i in range(N):
        if A[A[i]-1] == i+1:
            count += 1
    return count // 2


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque




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



if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

visited = set()
for i in range(n):
    if i == a[a[i]]:
        visited.add(i)
        visited.add(a[i])

print(len(visited) // 2)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans // 2)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#CODE FESTIVAL 2016 A

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n = I()
a = LI()
x = []
for i in range(n):
    if i+1 > a[i]:
        x.append((a[i],i+1))
    else:
        x.append((i+1,a[i]))
print(n-len(list(set(x))))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# code-festival-2016-qualaB - 仲良しうさぎ / Friendly Rabbits
# exhaustive search
def main():
    N = int(input())
    A = [0] + list(map(int, input().split()))
    ans = sum(i == A[j] for i, j in enumerate(A[1:], 1)) // 2
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# code-festival-2016-qualaB - 仲良しうさぎ / Friendly Rabbits
def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    ans, cand = 0, set()
    for i, j in enumerate(A, 1):
        i, j = min(i, j), max(i, j)
        if (i, j) in cand:
            ans += 1
        else:
            cand |= {(i, j)}
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# code-festival-2016-qualaB - 仲良しうさぎ / Friendly Rabbits
def main():
    N = int(input())
    A = tuple(map(int, input().split()))
    ans, cand = 0, set()
    for i, j in enumerate(A, 1):
        i, j = min(i, j), max(i, j)
        if (i, j) in cand:
            ans += 1
            cand.discard((i, j))
        else:
            cand |= {(i, j)}
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
#### import ####
import sys
sys.setrecursionlimit(10**7)
import math
from collections import defaultdict

#### 定数 ####
mod = 10**9 + 7

#### 読み込み ####
def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N):
  read_all = [tuple(map(int, input().split())) for _ in range(N)]
  return map(list,zip(*read_all))

#################

N = I()
a = III()

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

checked = [False] * N
ans = 0
for i in range(N):
    if not checked[i]:
        if a[a[i]-1]-1 == i:
            ans += 1
            checked[i] = True
            checked[a[i]-1] = True

print(ans)

Submission status: Accepted

Solution code:
input();*A,=map(int,input().split());print(sum(A[a-1]==i+1for i,a in enumerate(A))//2)

Submission status: Accepted

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

Submission status: Accepted

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

dict = {}
for i,a in enumerate(A):
    idx = a - 1
    t = (idx, i)
    if i <= idx:
        t = (i, idx)
    if t in dict:
        dict[t] += 1
    else:
        dict[t] = 1


ans = 0
for d in dict.values():
    if d == 2:
        ans += 1
print(ans)

Submission status: Accepted

Solution code:
###template###
import sys
def input(): return sys.stdin.readline().rstrip()
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt, ceil, floor
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
import heapq
INF = float("inf")
sys.setrecursionlimit(10**7)
# 4近傍（右, 下, 左, 上）
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
def inside(y: int, x: int, H: int, W: int) -> bool: return 0 <= y < H and 0 <= x < W
def mi(): return map(int, input().split())
def ii(): return int(input())
###template###

N = ii()
As = list(map(lambda x: int(x)-1, input().split())) #0-indexed
lovesWho = [-1]*N

ans = 0
for i, i_love in enumerate(As):
  if lovesWho[i_love] == i:
    ans += 1
  else:
    lovesWho[i] = i_love

print(ans)

Submission status: Accepted

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

g = [{} for _ in range(N)]
for i, a in enumerate(A):
	g[i][a - 1] = True

count = 0
for n in range(N):
	for r in g[n]:
		if n in g[r]:
			count += 1
print(count // 2)

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
x=0
for i in range(n):
    if i==a[a[i]-1]-1:x+=1
print(x//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] == 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()))
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()]
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()))
pair = set()
for i in range(N):
    if a[a[i]-1] == i+1:
        pair.add((min(a[a[i]-1], a[i]), max(a[a[i]-1], a[i])))
print(len(pair))

Submission status: Accepted

Solution code:
from collections import Counter

N = int(input())
*A, = map(int, input().split())
like = [(i, j) if i < j else (j, i) for i, j in zip(range(1, N+1), A)]
cnt = Counter(like)
ans = 0
for v in cnt.values():
    ans += v // 2
print(ans)

Submission status: Accepted

Solution code:
def solve(n, a):
    edge = {i: set() for i in range(n)}
    for i in range(n):
        edge[i].add(a[i]-1)
    num = 0
    for v, neighbors in edge.items():
        for u in neighbors:
            if (u in edge) and (v in edge[u]):
                num += 1
    return num // 2

n = int(input())
a = list(map(int, input().split()))
print(solve(n, a))

Submission status: Accepted

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

if __name__=="__main__": main()

Submission status: Accepted

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

for i in range(n):
    if i + 1 == a[a[i] - 1]:
        ans += 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 = [None] + [int(x) for x in input().split()]

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

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())
a = [int(x) for x in input().split()]
cnt = 0
for ind,i in enumerate(a):
    if(a[i-1]==ind+1):
        cnt+=1
print(cnt//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
B - 仲良しうさぎ / Friendly Rabbits
https://atcoder.jp/contests/code-festival-2016-quala/tasks/codefestival_2016_qualA_b

"""
import sys


def solve(N, rabbits):
    ans = 0
    for i, r in enumerate(rabbits[1:], start=1):
        if rabbits[r] == i:
            ans += 1
    return ans // 2


def main(args):
    N = int(input())
    rabbits = [-1] + [int(a) for a in input().split()]
    ans = solve(N, rabbits)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

c = 0
for i in range(1,N+1) :
    # iを基準に行って帰ってこれればOK
    if (i == li[li[i-1]-1]) :
        c += 1

print(c//2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(count // 2)

Submission status: Accepted

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

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()]
ans = 0
from collections import defaultdict
d = defaultdict(int)
for i, a in enumerate(A):
    t = tuple(sorted([i+1, a]))
    d[t] += 1
    if d[t] == 2:
        ans += 1
print(ans)

Submission status: Accepted

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

ans = 0

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

ans //= 2 
print(ans)

Submission status: Accepted

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

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

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

ans = 0

for i, a in enumerate(A):
    if a in ad[i]:
        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
for i,a in enumerate(A):
    if i == A[a - 1] - 1:
        cnt +=1
print(cnt // 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_s = [int(x) for x in input().split()]

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

print(ans)

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N=INT()
A=LIST()

like=[set() for i in range(N)]
cnt=0
for i in range(N):
    if i+1<A[i]:
        like[A[i]-1].add(i+1)
    else:
        if A[i] in like[i]:
            cnt+=1
print(cnt)

Submission status: Accepted

Solution code:
n=int(input())
ans=chk=0
d=set()
l=[int(i) for i in input().split()]
for i in range(n):
    a,b=min(i+1,l[i]),max(i+1,l[i])
    if (a,b) in d:
        ans+=1
    else:
        d.add((a,b))

print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

# print(n)
# print(a)

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

count = count//2
print(count)

Submission status: Accepted

Solution code:
import sys
MOD = 10 ** 9+7
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


N = ii()
a = lmi()
cnt = 0

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8-*-
if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))
    sum = 0
    for i in range(N):
        if i == a[a[i]-1]-1:
            sum += 1
    sum = sum // 2
    print(sum)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(p // 2)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input());a=list(map(int,input().split()));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 = [int(i)-1 for i in input().split()]

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

Submission status: Accepted

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

for i, a_i in enumerate(a, 1):
    if a_i > i and a[a_i-1] == i:
        cnt += 1

print(cnt)

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split(' ')))
like={(i+1):A[i] for i in range(N)}
print(int(len([1 for i in range(N) if (i+1)==like[like[i+1]]])/2))

Submission status: Accepted

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

ans = 0

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

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

Submission status: Accepted

Solution code:
N = int(input())
L = list(map(int,input().split()))
M = []
for i in range(N):
  if i+1 > L[i]:
    M.append((L[i],i+1))
  else:
    M.append((i+1,L[i]))
M = list(set(M))
print(len(L)-len(M))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

dic = {}
for i in range(N):
    dic[i] = A[i] - 1
   
res = 0
for i in range(N):
    if dic[A[i] - 1] == i:
        res += 1
        
        
print(res//2)