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: Wrong Answer

Solution code:
N = int(input())
a_1 = []
a_1=input().split()
a_2=[]
A = 0
a_2 = a_1


for n in a_1:
        #print(int(n))
        if int(n) == int(a_2[int(n)-1]):
                A = A + 1
                a_2[int(n)-1]=0
        else:
                pass
print(A)

Submission status: Wrong Answer

Solution code:
n = input()
s = input()
print(0)

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
j = 1
b = ''
while i < n:
    if (a[i] != j):
        b += str(a[i] + j)
    i += 1
    j += 1
l = len(set(b))
print(str(n - l))

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = raw_input().split(" ")
b = ['-2']
a = b + a
#print a
count = 0
for i in range(1,N+1):

    #print a.index(a[int(a[i])])
    if i == int(a[int(a[i])]) and a.index(a[int(a[i])]) == int(a[i]):
        count +=1
        a[i] = '0'
        a[int(a[i])] ='0'

print count

Submission status: Wrong Answer

Solution code:
n = int(input())
a = list(map(int, input().split()))
i = 0
j = 1
b = ''
while i < n:
    b += str(a[i] + j)
    i += 1
    j += 1
l = len(set(b))
print(str(n - l))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
_ = raw_input()
n_ls = map(int, raw_input().split())

count = 0
for i in range(len(n_ls)):
    other_idx = n_ls[i] - 1
    if i is not other_idx and i is n_ls[other_idx] - 1:
        n_ls[i] = False
        count += 1

# output
print count

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = raw_input()
a = a.replace(" ", "")
b = "0"
b +=a
a = b
count = 0
#print a
for i in range(1,N+1):
    if int(a[int(a[i])]) < N+1:

        #print a.index(a[i])
        if i == int(a[int(a[i])]) and a.index(a[int(a[i])]) == int(a[i]):
            print a.index(a[i])
            count += 1
            a = a[:i] + '0' + a[i + 1:]
            a = a[:int(a[i])] + '0' + a[int(a[i]) + 1:]

print count

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
_ = raw_input()
n_ls = map(int, raw_input().split())

count = 0
for i in range(len(n_ls)):
    if n_ls[i] and i + 1 is not n_ls[i] and i + 1 is n_ls[n_ls[i] - 1]:
        n_ls[n_ls[i] - 1] = False
        count += 1

# output
print count

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = raw_input()
a = a.replace(" ", "")
b = "0"
b +=a
a = b
count = 0
#print a
for i in range(1,N+1):
    if int(a[int(a[i])]) < N+1:
        if i == int(a[int(a[i])]):
            count += 1
            a = a[:i] + '0' + a[i + 1:]
            a = a[:int(a[i])] + '0' + a[int(a[i]) + 1:]

print count

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
_ = raw_input()
n_ls = map(int, raw_input().split())

count = 0
for i in range(len(n_ls)):
    if n_ls[i] and i + 1 is n_ls[n_ls[i] - 1]:
        n_ls[n_ls[i] - 1] = False
        count += 1

# output
print count

Submission status: Wrong Answer

Solution code:
N = raw_input()
likes = raw_input()
pairs = 0
counter = 0
rabbit = 1
while counter < (int(N) * 2):
    if likes[counter] == " ":
        counter += 1
    elif int(likes[((int(likes[counter]) -1 ) * 2)]) == rabbit:
        pairs += 1
        rabbit += 1
        counter += 2
    else:
        rabbit += 1
        counter += 2
pairs = pairs / 2
print pairs

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = raw_input()
a = a.replace(" ", "")
b = "0"
b +=a
a=b
count = 0
#print a
for i in range(1,N+1):
    if int(a[int(a[i])]) < N+1:
        if i == int(a[int(a[i])]):
            count += 1

print count/2

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

friends = 0
for i in range(len(a)):
    if a[i] == -1:
        continue
    if a[a[i] - 1] == (i+1):
        friends += 1
        a[i] = -1
        a[a[i] - 1] = -1

print(int(friends))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
_ = raw_input()
n_ls = map(int, raw_input().split())

count = 0
for i in range(len(n_ls)):
    if i + 1 is n_ls[n_ls[i] - 1]:
        count += 1

# output
print count / 2

Submission status: Wrong Answer

Solution code:
n = int(input())
a = set(list(map(int, input(). split())))
if len(a) % 2 == 0:
    print(int(len(a) / 2))
else:
    if len(a) == 3:
        print(0)
    else:
        print(int(len(a) / 2))

Submission status: Wrong Answer

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

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

print(friends)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = raw_input()
a = a.replace(" ", "")
b = "0"
b +=a
a=b
count = 0
#print a
for i in range(1,N+1):
    #print i
    #print a[int(a[i])]
    if i == int(a[int(a[i])]):
        count += 1

print count/2

Submission status: Wrong Answer

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

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

print(friends)

Submission status: Wrong Answer

Solution code:
n=int(raw_input())
arr=map(int,raw_input().split(" "))
arr.insert(0,0)

ctr=0
for i in range(1,n):
    for j in range(i,n+1):
        if (arr[i]==j) & (arr[j]==i):
            ctr=ctr+1

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = map(int, raw_input().split())
m = {}
ret  = 0
for idx, val in enumerate(a):
  m[val] = idx
for idx, val in enumerate(a):
  if a[val-1] == idx+1 and val < idx:
    ret += 1
print ret

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys 

N = int(sys.stdin.readline())
rabbits = map(int, sys.stdin.readline().split())
print rabbits

i = 0 
pair = 0 
while i< len(rabbits):
        if rabbits[rabbits[i]-1] == i + 1:
                pair += 1
        i += 1 

print pair/2

Submission status: Wrong Answer

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

friends = 0
for i in range(0,len(a)):
    if a[i] == -1:
        continue
    if a[a[i]-1] == i+1:
        friends += 1
        a[i-1] = -1
        a[a[i-1]] = -1

print(friends)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
x_vec = map(int, raw_input().split())
ret = 0
for i in range(len(x_vec)):
    if x_vec[x_vec[i]] == i:
        ret += 1 
print ret / 2

Submission status: Wrong Answer

Solution code:
N = input()
love = map(int, raw_input().split())
count = 0
for i in range(N):
    love[love[i]] == i
    count = count + 1
print count

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = map(int, raw_input().split())
ret  = 0
for idx, val in enumerate(a):
  if a[idx] == idx+1:
    ret += 1
print ret

Submission status: Wrong Answer

Solution code:

N = int(input())
a = list(map(int,input().split()))
print(a)
ans=0
for i, v in enumerate(a):
    if v != None:
        if a[v-1] == i+1:
            ans+=1
            a[v-1] = None
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(raw_input())
a = map(int, raw_input().split())
ret  = 0
for idx, val in enumerate(a):
  if a[idx] == idx:
    ret += 1
print ret

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
s = list(map(int ,input().split()))
con = 0
for i in range(N):
  for j in range(i + 1 , N):
    if s[i] == j + 1 and s[j] == i + 1:
      con += 1
print(con)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
l = [sorted([i+1, j]) for i, j in enumerate(map(int, input().split()))]

def f(l):
	ul = []
	return [x for x in l if x not in ul and not ul.append(x)]

print(len(l) - len(f(l)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

d = [[False]*N for _ in range(N)]
cnt = 0
for i in range(N):
  j = S[i] - 1
  d[i][j] = True
  if d[j][i]:
    cnt += 1
  
print(cnt)

Submission status: Time Limit Exceeded

Solution code:

count = 0
N = int(input())
A = list(map(int, input().split()))
B =[x for x in range(1,N+1)]
C = list(zip(A, B))
D = [sorted(x) for x in C]
for i in range(N):
  if D.count(D[i]) == 2:
    count += 1
    
print(int(count/2))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
rabit = input().split(" ")
skip = []
num = 0

for i in range(N):
  n = int(rabit[i])
  if i in skip:
    continue
  if int(rabit[n-1]) == i+1:
    skip.append(n-1)
    num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
rabit = [ int(v) for v in input().split(" ") ]
skip = []
num = 0

for i in range(N):
  n = rabit[i]
  if i in skip:
    continue
  if rabit[n-1] == i+1:
    skip.append(n-1)
    num += 1

print(num)

Submission status: Time Limit Exceeded

Solution code:
from itertools import*
n, *a = map(int, open(0).read().split())
print(sum(a[i] == j + 1 and a[j] == i + 1 for i, j in combinations(range(n), 2)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int, input().split()))
list1 = []
for i in range(n) :
    list1.append({a[i],i+1})
length1 = len(list1)
list2 = []
for num in list1 :
    if not num in list2 :
        list2.append(num)
length2 = len(list2)
print(length1-length2)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
import collections

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

for i in range(n):
    if {i+1, a[i]} in l:
        ans += 1
    else:
        l.append({i+1, a[i]})

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter

n = int(input())
a = list(map(int, input().split()))
pair = []
cp = [None]
for i, tgt in enumerate(a):
    cp.clear()
    if a.index(tgt) == i:
        cp = [str(a[i]), str(i + 1)]
        cp.sort()
        cpj = "".join(cp)
        pair.append(cpj)

pcnt = Counter(pair)
pcv = list(pcnt.values())
print(pcv.count(2))

Submission status: Time Limit Exceeded

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

c = 0
for i in range(N-1):
  for j in range(i+1,N):
    if a[i] == j+1 and a[j] == i+1:
      c += 1
print(c)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(N-1):
  for j in range(i+1,N):
    if a[i] == j+1 and a[j] == i+1:
      c += 1
print(c)

Submission status: Time Limit Exceeded

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

p = []
d = [0]*n
for i in range(n):
  tmp = []
  for j in range(i+1,n):
    if i+1 == a[j] and a[i] == j+1 and d[i] == 0 and d[j] == 0:
      tmp.append(a[i])
      tmp.append(a[j])
      p.append(tmp)
      d[i] = 1
      d[j] = 1
print(len(p))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    for j in range(i+1, n):
        if A[i] == j and A[j] == i:
            paired[j] = 1
            paired[i] = 1
            c += 1
            break
print(c)

Submission status: Time Limit Exceeded

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

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

print(c)

Submission status: Time Limit Exceeded

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

for i in range(n):
    if i+1 in a:
        if a[i] == a.index(i+1) + 1:
            count += 1

print(int(count/2))

Submission status: Time Limit Exceeded

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

li = [None]*N

ans = 0

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

print(ans)

Submission status: Time Limit Exceeded

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

li = [None]*N

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

ans = 0

for l in li:
    if l[::-1] in li:
        ans += 1

print(ans//2)

Submission status: Time Limit Exceeded

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

li = []

for i, a in enumerate(A):
    li.append([i+1, a])

ans = 0

for l in li:
    if l[::-1] in li:
        ans += 1

print(ans//2)

Submission status: Time Limit Exceeded

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

l = [[0]*N for i in range(N)]

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

ans = 0
for i in range(N):
    for j in range(i+1, N):
        if l[i][j] == 1 and l[j][i] == 1:
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    N=int(input())
    a=[int(_) for _ in input().split()]
    pair = []
    ans = 0
    for i in range(N):
        fst = a[a[i]-1]
        snd = a[fst-1]
        pair.append((fst,snd))
    pair=list(set(pair))
    for i in range(len(pair)-1):
        for j in range(i+1,len(pair)):
            if pair[i][0]==pair[j][1] and pair[i][1]==pair[j][0]:
                ans += 1
    print(ans)

main()

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

like = []
cnt = 0
for i in range(N):
    s = set([A[i], i+1])
    if s in like:
        cnt+=1
    else:
        like.append(s)
print(cnt)

Submission status: Time Limit Exceeded

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

friends = []

for i in range(N):
    if a[a[i]] == i and ([i, a[i]] not in friends and [a[i], i] not in friends):
        friends.append([i, a[i]])

print(len(friends))

Submission status: Time Limit Exceeded

Solution code:
import sys

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


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

    res = 0
    FF = []
    for i in range(n):
        if A[A[i] - 1] == i + 1 and A[i] not in set(FF):
            res += 1
            FF.append(A[A[i] - 1])
            FF.append(i + 1)

    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
import sys

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


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

    res = 0
    FF = []
    for i in range(n):
        if A[A[i] - 1] == i + 1 and A[i] not in FF:
            res += 1
            FF.append(A[A[i] - 1])
            FF.append(i + 1)
    
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
ans = 0
ls = []
for i in range(N):
  if A[A[i]-1] == i+1:
    k = [A[i]-1,i]
    k.sort()
    if k not in ls:
      ls.append(k)
print(len(ls))

Submission status: Time Limit Exceeded

Solution code:
import itertools

n=int(input())
a=list(map(int,input().split()))
l=list(range(1,n+1))
ans=(list([a[x-1],a[y-1]] for x,y in (itertools.combinations(l, 2)) if x<y and a[x-1]!=x and a[y-1]!=y and a[x-1]==y and a[y-1]==x))
print (len(ans))

Submission status: Time Limit Exceeded

Solution code:
import itertools

n=int(input())
a=list(map(int,input().split()))
l=list(range(1,n+1))
tmpList= (list([x,y] for x,y in (itertools.combinations(l, 2)) if x<y))
ans=0
for item in tmpList:
    if a[item[0]-1]!=item[0] and a[item[1]-1]!=item[1] and a[item[1]-1]==item[0] and a[item[0]-1]==item[1]:
        ans+=1
print (ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

n=int(input())
a=list(map(int,input().split()))
l=[]
for i in range(1,n+1):
    l.append(i)
tmpList= (list([x,y] for x,y in (itertools.combinations(l, 2)) if x<y))
ans=0
for item in tmpList:
    if a[item[0]-1]!=item[0] and a[item[1]-1]!=item[1] and a[item[1]-1]==item[0] and a[item[0]-1]==item[1]:
        ans+=1
print (ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
ai = []
 
for i, j in enumerate(a):
    ai.append([i+1,j])
    ai[i].sort()
 
c = 0
for i in range(len(ai)):
    c += ai.count(ai[i])
 
print(c // 4)

Submission status: Time Limit Exceeded

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

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

c = 0
for i in range(len(ai)):
    c += ai.count(ai[i])

print(c // 4)

Submission status: Time Limit Exceeded

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

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

c = 0
for i in range(len(ai)):
    for j in range(len(ai)):
        if i < j and ai[i] == ai[j]:
            c += 1

print(c)

Submission status: Time Limit Exceeded

Solution code:
def resolve():

    N=int(input())
    A=list(map(int,input().split()))
    ls=[0]*N
    for i in range(N):
        ls[i]=(i,A[i]-1)
    cnt=0
    for (s,t) in ls:
        if (t,s) in ls:
            cnt+=1
    print(cnt//2)



resolve()

Submission status: Time Limit Exceeded

Solution code:

def resolve():

    N=int(input())
    A=list(map(int,input().split()))
    ls=[]
    for i in range(N):
        ls.append((i,A[i]-1))
    cnt=0
    for (s,t) in ls:
        if (t,s) in ls:
            cnt+=1
    print(cnt//2)



resolve()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
arr = [[False for i in range(N)] for j in range(N)]
cnt = 0
for ind, val in enumerate(A):
  arr[ind][val-1] = True
  if arr[val-1][ind] == True:
    cnt += 1

print(cnt)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def likes(i,j,ai,aj):
    if(i == aj and j == ai):
        return True
    else:
        return False

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

Submission status: Time Limit Exceeded

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

n = int(input())
a = [int(x) for x in input().split()]
c = 0
b = [{i + 1, x} for i, x in enumerate(a)]
for i, e in enumerate(sorted(b)):
    if b.count(e) == 2:
        c += 1
print(c // 2)

Submission status: Time Limit Exceeded

Solution code:
p = int(input())
A = list(map(int,input().split()))
co = 0
cont = 0
for i in A:
  cont += 1
  if A.count(cont) != 0 and i == A.index(cont)+1:
    A.pop(A.index(cont))
    co += 1
print(co)

Submission status: Time Limit Exceeded

Solution code:
p = int(input())
A = list(map(int,input().split()))
a = list(range(1,p+1))
count = 0
for j,i in zip(a,A):
  if A.count(j) != 0 and i == a[A.index(j)]:
    A.pop(A.index(j))
    count += 1
print(count)

Submission status: Time Limit Exceeded

Solution code:
p = int(input())
A = list(map(int,input().split()))
a = list(range(1,p+1))
count = 0
for j,i in zip(a,A):
  if A.count(j) != 0 and i == a[A.index(j)]:
    count += 1
print(count//2)

Submission status: Time Limit Exceeded

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

c = 0
for i in range(N):
    for j in range(N):
        if i < j:
            if A[i] == j+1 and A[j] == i+1:
                c += 1
print(c)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

    C = []
    ans = 0
    for a, b, in zip(A, B):
        a, b = min(a, b), max(a, b)
        if [a, b] in C:
            ans += 1
        else:
            C.append([a, b])

    print(ans)


main()

Submission status: Time Limit Exceeded

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

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

print(answer)

Submission status: Time Limit Exceeded

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

res = 0
pair = []
for i in range(N):
    if A[A[i] - 1] - 1 == i and sorted([i, A[i] - 1]) not in pair:
        res += 1
        pair.append([i, A[i] - 1])

print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a = list(map(int,input().split()))
check=[]
ans=0
for i in range(n):
    if min(i+1,a[i])+max(i+1,a[i])*1000000 not in check:
        check.append(min(i+1,a[i])+max(i+1,a[i])*1000000)
    else:
        ans+=1
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
rabbit = list(map(int, input().split()))
cnt = 0
for i in range(n-1):
  for j in range(i+1,n):
#    print("i",i,"j",j)
#    print("rabbit[i]",rabbit[i],"rabbit[j]",rabbit[j])
    if rabbit[i] == j+1 and rabbit[j]==i+1:
      cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

# タプルを作って裏返したペアが存在するかどうか
tupple = []
for i in range(N) :
    s = str(i+1) + ' ' + str(li[i])
    tupple.append(s)

c = 0
for tup in tupple :
    t = tup.split()
    needle = t[1] + ' ' + t[0]
    if needle in tupple :
        c += 1

print(c//2)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count_j = 0
count_ans = 0

num_ban = []
ban = 0

for i in range(N):
    for k in num_ban:
        if k == i + 1:
            ban = 1
            break
    if ban == 1:
        ban = 0
        continue
    for j in A:
        count_j += 1
        if count_j != i + 1 and j == i + 1:
            if A[i] == count_j:
                count_ans += 1
                num_ban.append(j)
                num_ban.append(count_j)
    else:
        count_j = 0
else:
    print(count_ans)

Submission status: Time Limit Exceeded

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

count_j = 0
count_ans = 0

front_li = []
back_li = []

for i in range(N):
    for j in A:
        count_j += 1
        if count_j != i + 1 and j == i + 1:
            if A[i] == count_j:
                count_ans += 1
    else:
        count_j = 0
else:
    print(int(count_ans / 2))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    N = int(input())
    a = [int(x) for x in input().split()]
    num_len = len(a)
    ans = 0
    for i in range(1, num_len+1):
        for j in range(1, num_len+1):
            if i == a[j-1] and j == a[i-1]:
                ans += 1
    print(int(ans / 2))

Submission status: Time Limit Exceeded

Solution code:
x = input()
y = input().split(" ")

pair = 0

l = []

for i in range(len(y)):
    if [y[i], y[int(y[i])-1]] not in l:
        l.append([y[i], y[int(y[i])-1]])


for i in l:
    a, b = i
    if [b, a] in l:
        l.remove([b, a])
        pair += 1
    
print(pair)

Submission status: Time Limit Exceeded

Solution code:
x = input()
y = input().split(" ")

pair = 0

l = []

for i in range(len(y)):
    if [y[i], y[int(y[i])-1]] not in l:
        l.append([y[i], y[int(y[i])-1]])

for i in l:
    a, b = i
    if [b, a] in l:
        pair += 1
    
print(pair//2)

Submission status: Time Limit Exceeded

Solution code:
x = input()
y = input().split(" ")

pair = 0

l = []

for i in range(len(y)):
    if [int(y[i]), int(y[int(y[i])-1])] not in l:
        l.append([int(y[i]), int(y[int(y[i])-1])])

for i in l:
    a, b = i
    if [b, a] in l:
        pair += 1
    
print(pair//2)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
l = input().split(' ')
t_l =[]
for i in range(0,n):
   t = i + int(l[i])
   t_l.append(t)
a = 0
for i in range(n *2 -1):
    if t_l.count(i) == 2:
        a += 1
print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
line = input().split(' ')
t_list =[]
for i in range(0,n):
   t = i + int(line[i])
   t_list.append(t)
a = 0
for i in range(n *2 -1):
    if t_list.count(i) == 2:
        a += 1
print(a)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
line = input().split(' ')
total_list =[]
for i in range(0,n):
   total = i + int(line[i])
   total_list.append(total)
answer = 0
for i in range(n *2 -1):
    if total_list.count(i) == 2:
        answer += 1
    else:
        answer += 0
print(answer)

Submission status: Time Limit Exceeded

Solution code:
# cook your code here
a = int(raw_input())
ss = raw_input().split()
lst = []
c = 0

i = 0
for s in ss:
    if s not in lst:
        if int(ss[int(ss[i])-1]) == i+1:
            c=c+1
            lst.append(s)
            lst.append(ss[int(ss[i])-1])
    i=i+1
    

    

print c

Submission status: Time Limit Exceeded

Solution code:
rabbit_number = input()
like = raw_input()
couple = 0
for i in range(rabbit_number):
	if i+1 == int(like.split(" ")[int(like.split(" ")[i])-1]):
		couple += 1
print couple/2