Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
lst=[None]*N
for i in range(N):
    lst[i]=''.join(sorted(input()))
count=0
for i in range(N-1):
    for j in range(i+1, N):
        if lst[i]==lst[j]:
            count+=1
print(count)

Fixed submission:
N=int(input())
lst=[None]*N
for i in range(N):
    lst[i]=''.join(sorted(input()))
count=0

import collections
dict=collections.Counter(lst)

for k, v in dict.items():
    count+=v*(v-1)//2

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
lst=[None]*N
for i in range(N):
    lst[i]=''.join(sorted(input()))
count=0
for el in set(lst):
    num=lst.count(el)
    count+=num*(num-1)//2
print(count)

Fixed submission:
N=int(input())
lst=[None]*N
for i in range(N):
    lst[i]=''.join(sorted(input()))
count=0

import collections
dict=collections.Counter(lst)

for k, v in dict.items():
    count+=v*(v-1)//2

print(count)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
lst=[None]*N
for i in range(N):
    lst[i]=''.join(sorted(input()))
count=0
for el in set(lst):
    num=lst.count(el)
    count+=num*(num-1)//2
print(count)

Fixed submission:
N=int(input())
lst=[None]*N
for i in range(N):
    lst[i]=''.join(sorted(input()))
count=0

import collections
dict=collections.Counter(lst)

for k, v in dict.items():
    count+=v*(v-1)//2

print(count)
Buggy python submission (verdict: Runtime Error):
S=input()
N=len(S)
children=[1]*N
def next(children):
    new_children=[0]*N
    for i in range(N):
        if S[i]=='L':
            new_children[i-1]+=children[i]
        else:
            new_children[i+1]+=children[i]
    return new_children

change=[]
count=0
while True:

    #print(children)
    if children in change:
        start=change.index(children)
        break
    change.append(children)

    children=next(children)
    count += 1
#print(start, count)
if count==start:
    R=(10**100-4)%(count-start)
    print(' '.join(change[4+R]))
else:
    print(' '.join(change[start]))


Fixed submission:
S=input()

N=len(S)

lst=[0]*N
R_count=0
for i in range(N-1):
    if S[i]=='R' and S[i+1]=='R':
        R_count+=1
    if S[i]=='R' and S[i+1]=='L':
        R_count += 1
        lst[i]=R_count
        R_count=0
L_count=0
for i in range(N-1, 0, -1):
    if S[i]=='L' and S[i-1]=='L':
        L_count+=1
    if S[i]=='L' and S[i-1]=='R':
        L_count += 1
        lst[i]=L_count
        L_count=0
#print(lst)
i=0
while i < N:
    if lst[i] and lst[i+1]:
        mid=(lst[i]+lst[i+1])//2
        if (lst[i]-lst[i+1])%2==0:
            lst[i], lst[i+1] = mid, mid
        elif lst[i]>lst[i+1]:
            if lst[i]%2==1:
                lst[i], lst[i+1]= mid+1, mid
            else:
                lst[i], lst[i+1] =mid, mid+1
        elif lst[i]<lst[i+1]:
            if lst[i+1]%2==1:
                lst[i], lst[i + 1] = mid, mid + 1
            else:
                lst[i], lst[i + 1] = mid + 1, mid
        i+=1
    i+=1
print(' '.join(list(map(str, lst))))
Buggy python submission (verdict: Runtime Error):
import math
A, B, C, D = map(int, input().split())

l=C*D//math.gcd(C,D)
underB = B- (B//C+B//D-B//l)
underA = (A-1)- ((A-1)//C+(A-1)//D-(A-1)//l)
#print(underB, underA)
print(underB-underA)

Fixed submission:
import fractions
A, B, C, D = map(int, input().split())

l=C*D//fractions.gcd(C,D)
underB = B- (B//C+B//D-B//l)
underA = (A-1)- ((A-1)//C+(A-1)//D-(A-1)//l)
#print(underB, underA)
print(underB-underA)

Buggy python submission (verdict: Runtime Error):
A, B, C, D = map(int, input().split())
import math
l=C*D//math.gcd(C,D)
underB = B- (B//C+B//D-B//l)
underA = (A-1)- ((A-1)//C+(A-1)//D-(A-1)//l)
#print(underB, underA)
print(underB-underA)

Fixed submission:
import fractions
A, B, C, D = map(int, input().split())

l=C*D//fractions.gcd(C,D)
underB = B- (B//C+B//D-B//l)
underA = (A-1)- ((A-1)//C+(A-1)//D-(A-1)//l)
#print(underB, underA)
print(underB-underA)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
AB=[]
for _ in range(N):
    AB.append(tuple(map(int, input().split())))
#print(N, AB)

AB=sorted(AB, key=lambda val:val[1])
task=[]
for ab in AB:
    for _ in range(ab[0]):
        task.append(ab[1])
#print(AB)
#print(task)

for i in range(len(task)):
    if i+1>task[i]:
        print('No')
        break
else:
    print('Yes')

Fixed submission:
N = int(input())
AB=[]
for _ in range(N):
    AB.append(list(map(int, input().split())))
#print(N, AB)

AB=sorted(AB, key=lambda val:val[1])

if AB[0][0]>AB[0][1]:
    print('No')
else:
    for i in range(N-1):
        AB[i+1][0]+=AB[i][0]
        if AB[i+1][0]>AB[i+1][1]:
            print('No')
            break
    else:
        print('Yes')
Buggy python submission (verdict: Runtime Error):
N, X = map(int, input().split())
L=list(map(int, input().split()))

D=0
count=1
while D<=X and count<=N+1:
   D=D+L[count-1]
   count+=1

print(min(N+1, count-1))

Fixed submission:
N, X = map(int, input().split())
L=list(map(int, input().split()))

D=0
count=1
while D<=X and count<=N+1:
    if count != N+1:
       D=D+L[count-1] #count kaime no jump
    count+=1

print(count-1)

Buggy python submission (verdict: Runtime Error):
N, X = map(int, input().split())
L=list(map(int, input().split()))

D=0
count=1
while D<=X:
   D=D+L[count-1]
   count+=1

print(min(N+1, count-1))

Fixed submission:
N, X = map(int, input().split())
L=list(map(int, input().split()))

D=0
count=1
while D<=X and count<=N+1:
    if count != N+1:
       D=D+L[count-1] #count kaime no jump
    count+=1

print(count-1)

Buggy python submission (verdict: Runtime Error):
N, X = map(int, input().split())
L=list(map(int, input().split()))

D=0
count=1
while D<=X:
   D=D+L[count-1]
   count+=1
print(count-1)

Fixed submission:
N, X = map(int, input().split())
L=list(map(int, input().split()))

D=0
count=1
while D<=X and count<=N+1:
    if count != N+1:
       D=D+L[count-1] #count kaime no jump
    count+=1

print(count-1)
Buggy python submission (verdict: Runtime Error):
import math

N, K = map(int, input().split())

prob=0
for dice_num in range(1, N+1):
    if K>dice_num:
        pwr=math.ceil(math.log2(K/dice_num))
        prob+=(1/2)**pwr/N
    else:
        prob+=1/N
print(prob)



Fixed submission:
import math

N, K = map(int, input().split())

prob=0
for dice_num in range(1, N+1):
    if K>dice_num:
        pwr=math.ceil(math.log2(K/dice_num))
        prob+=(1/2)**pwr/N
    else:
        prob+=1/N
print(prob)
Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())

A=int(input())
B=int(input())
C=int(input())
D=int(input())
E=int(input())
#print(N,A,B,C,D,E)

def transport(situ):
    temp=[0]*6
    temp[0]+=situ[0]-min(A, situ[0])
    temp[1]+=min(A, situ[0])

    temp[1]+=situ[1]-min(B, situ[1])
    temp[2]+=min(B, situ[1])

    temp[2] += situ[2] - min(C, situ[2])
    temp[3] += min(C, situ[2])

    temp[3] += situ[3] - min(D, situ[3])
    temp[4] += min(D, situ[3])

    temp[4] += situ[4] - min(E, situ[4])
    temp[5] += min(E, situ[4])

    temp[5]+=situ[5]

    return temp

popu=[N, 0, 0, 0, 0, 0]
time=0
while popu!=[0, 0, 0, 0, 0, N]:
    popu=transport(popu)
    time+=1

print(time)


Fixed submission:
import math

N=int(input())
A=int(input())
B=int(input())
C=int(input())
D=int(input())
E=int(input())

spent=[A,B,C,D,E]
limit=min(spent)
limit_index=spent.index(limit)
print(math.ceil(N/limit)-1+5)
Buggy python submission (verdict: Time Limit Exceeded):
import re
N, Q = map(int, input().split())
S=input()
lr=[]
for _ in range(Q):
    lr.append(tuple(map(int, input().split())))
#print(N, Q, S, lr)

ACs=[m.start()+1 for m in re.finditer('AC', S)]
#print(ACs)

for val in lr:
    OK=[strt for strt in ACs if val[0] <= strt and strt + 1 <= val[1]]
    print(len(OK))


Fixed submission:
import re
N, Q = map(int, input().split())
S=input()
lr=[]
for _ in range(Q):
    lr.append(tuple(map(int, input().split())))
#print(N, Q, S, lr)



accum=[0]*(N+1)
should_plus=0
for i in range(2, N+1):
    if S[i-2]=='A' and S[i-1]=='C':
        should_plus+=1
    accum[i]=should_plus
#print(accum)

for val in lr:
    OK=accum[val[1]]-accum[val[0]-1]
    if S[(val[0]-2):val[0]]=='AC':
        OK-=1
    print(OK)
Buggy python submission (verdict: Time Limit Exceeded):
A, B, Q = map(int, input().split())
s = []
for _ in range(A):
    s.append(int(input()))
t = []
for _ in range(B):
    t.append(int(input()))
x = []
for _ in range(Q):
    x.append(int(input()))

def binsearch(threshold, lst):
    left, right = -1, len(lst)

    while right - left > 1:
        mid = (left + right) // 2
        if lst[mid] > threshold:
            right = mid
        else:
            left = mid
    return (left, right)

for q in x:
    ans = 10 ** 11
    for i in range(2):
        for j in range(2):
            a = binsearch(q, s)[i]
            b = binsearch(q, t)[j]
            #print(i, j, a, b)
            if 0 <= a <= A - 1 and 0 <= b <= B - 1:
                    #print(s[a], t[b], q)
                    ans = min(ans, abs(s[a] -t[b]) + min(abs(s[a]-q), abs(t[b]-q)))
    print(ans)

Fixed submission:
A, B, Q = map(int, input().split())
s = []
for _ in range(A):
    s.append(int(input()))
t = []
for _ in range(B):
    t.append(int(input()))
x = []
for _ in range(Q):
    x.append(int(input()))

def binsearch(threshold, lst):
    left, right = -1, len(lst)

    while right - left > 1:
        mid = (left + right) // 2
        if lst[mid] > threshold:
            right = mid
        else:
            left = mid
    return (left, right)

for q in x:
    ans = 10 ** 11
    for i in range(2):
        for j in range(2):
            a = binsearch(q, s)[i]
            b = binsearch(q, t)[j]
            #print(i, j, a, b)
            if 0 <= a <= A - 1 and 0 <= b <= B - 1:
                    #print(s[a], t[b], q)
                    ans = min(ans, abs(s[a] -t[b]) + min(abs(s[a]-q), abs(t[b]-q)))
    print(ans)
Buggy python submission (verdict: Runtime Error):
K, A, B = map(int, input().split())

dp = [0] * (K + 1)

dp[0] = 1
dp[1] = 2
for i in range(2, K + 1):
    dp[i] = max(dp[i - 1] + 1, (dp[i - 2] // A) * B + dp[i - 2] % A)
# print(dp)
print(dp[K])

Fixed submission:
K, A, B = map(int, input().split())

if B - A <= 2:
    print(K + 1)
else:
    time = max(0, (K - (A - 1)) // 2)
    ans = A + (B - A) * time
    if (K - (A - 1)) % 2 == 1:
        ans += 1
    print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

if sum(A) < sum(B):
    print(-1)
else:
    before = [A[i] - B[i] for i in range(N)]
    before.sort()
    diff = [A[i] - B[i] for i in range(N)]
    # print(diff)
    diff.sort()
    left = 0
    right = N - 1
    while not all([x >= 0 for x in diff]):
        if diff[left] < 0:
            temp = diff[left]
            diff[left] = min(0, diff[left] + diff[right])
            diff[right] -= diff[left] - temp
        if diff[left] == 0:
            if diff[left + 1] < 0:
                left += 1
        if diff[right] == 0:
            if diff[left] < 0:
                right -= 1
    # print(diff)
    cnt = 0
    for i in range(N):
        if before[i] != diff[i]:
            cnt += 1
    print(cnt)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))

if sum(A) < sum(B):
    print(-1)
else:
    before = [A[i] - B[i] for i in range(N)]
    before.sort()
    diff = [A[i] - B[i] for i in range(N)]
    # print(diff)
    diff.sort()
    left = 0
    right = N - 1
    while True:
        if diff[left] < 0:
            temp = diff[left]
            diff[left] = min(0, diff[left] + diff[right])
            diff[right] -= diff[left] - temp
        if diff[left] == 0:
            if diff[left + 1] < 0:
                left += 1
        if diff[right] == 0:
            if diff[left] < 0:
                right -= 1
        if diff[left] == 0:
            break
    # print(diff)
    cnt = 0
    for i in range(N):
        if before[i] != diff[i]:
            cnt += 1
    print(cnt)
Buggy python submission (verdict: Runtime Error):
N = int(input())
h = list(map(int, input().split()))
import math
dp = [math.inf] * N
dp[0] = 0
dp[1] = abs(h[1] -h[0])
if N >= 3:
    for i in range(2, N):
        dp[i] = min(dp[i - 1] + abs(h[i] -h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]))
print(dp[N - 1])

Fixed submission:
N = int(input())
h = list(map(int, input().split()))
import math
dp = [0] * N
dp[0] = 0
dp[1] = abs(h[1] -h[0])
if N >= 3:
    for i in range(2, N):
        dp[i] = min(dp[i - 1] + abs(h[i] -h[i - 1]), dp[i - 2] + abs(h[i] - h[i - 2]))
print(dp[N - 1])
Buggy python submission (verdict: Runtime Error):
N, W = map(int, input().split())
wv = []
for _ in range(N):
    wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 110 for _ in range(100100)]


for i in range(N):
    for sum_w in range(W + 1):

        if wv[i][0] <= sum_w:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])

        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])


Fixed submission:
N, W = map(int, input().split())
wv = []
for _ in range(N):
    wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 100100 for _ in range(110)]


for i in range(N):
    for sum_w in range(W + 1):

        if wv[i][0] <= sum_w:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])

        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])

Buggy python submission (verdict: Runtime Error):
N, W = map(int, input().split())
wv = []
for _ in range(N):
    wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 110 for _ in range(100100)]


for i in range(N):
    for sum_w in range(W + 1):

        if sum_w - wv[i][0] >= 0:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])

        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])


Fixed submission:
N, W = map(int, input().split())
wv = []
for _ in range(N):
    wv.append(list(map(int, input().split())))
# print(wv)
dp = [[0] * 100100 for _ in range(110)]


for i in range(N):
    for sum_w in range(W + 1):

        if wv[i][0] <= sum_w:
            dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w - wv[i][0]] + wv[i][1])

        dp[i + 1][sum_w] = max(dp[i + 1][sum_w], dp[i][sum_w])
print(dp[N][W])
Buggy python submission (verdict: Time Limit Exceeded):
N, X = map(int, input().split())

def func(n):
    if n == 1:
        return [1, 3, 1]
    else:
        temp=func(n-1)
        return  [temp[0] + 1] + temp[1:] + [1] +\
                temp[:-1] + [temp[-1] + 1]

bg = func(N)
#print(bg)
cumulative = [0]
for i in range(len(bg)):
    cumulative.append(cumulative[i] + bg[i])
#print(cumulative)

#do binary search
left, right = -1, len(bg)

while right - left > 1:
    mid = (left + right) // 2
    if cumulative[mid] < X:
        left = mid
    else:
        right = mid


#print(right)
if right % 2 == 1: #in B
    print(sum([bg[i] for i in range(1, right, 2)]))
else:
    print(X - sum([bg[i] for i in range(0, right, 2)]))


Fixed submission:
N, X = map(int, input().split())
a = [1]
for _ in range(N - 1):
    a.append(a[-1] * 2 + 3)
p = [1]
for _ in range(N - 1):
    p.append(p[-1] * 2 + 1)
# print(a)
# print(p)
def func(n, x):
    if n == 0 and x == 1:
        return 1
    if x == 1:
        return 0
    elif x <= 1 + a[n - 1]:
        return func(n - 1, x - 1)
    elif x == 2 + a[n - 1]:
        return p[n - 1] + 1
    elif x <= 2 + a[n - 1] * 2:
        return p[n - 1] + 1 + func(n - 1, x - a[n - 1] - 2)
    else:
        return p[n - 1] * 2 + 1

print(func(N, X))
Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())

import math

divisor=[]
for i in range(1, math.ceil(math.sqrt(M))+1):
    if M%i==0:
        divisor.append(i)
#print(divisor)

divisor+=[M//val for val in reversed(divisor)]
#print(divisor)
for val in divisor:
    if val>=N:
        print(M//val)
        break

Fixed submission:
N = int(input())
if N==1:
    print('Hello World')
else:
    A=int(input())
    B=int(input())
    print(A+B)
Buggy python submission (verdict: Runtime Error):
N = int(input())
xyh=[]
for _ in range(N):
    xyh.append(tuple(map(int, input().split())))

def func():
    for Cx in range(0, 101):
        for Cy in range(0, 101):
            H=[el[2]+abs(el[0]-Cx)+abs(el[1]-Cy) for el in xyh if el[2]!=0]
            if H>=1 and len(set(H))==1:
                temp=[H[0]-abs(el[0]-Cx)-abs(el[0]-Cy) for el in xyh if el[2]==0]
                for el in temp:
                    if el>0:
                        break
                else:
                    print(Cx, Cy, H[0])
                    return None
func()


Fixed submission:
N = int(input())
xyh=[]
for _ in range(N):
    xyh.append(tuple(map(int, input().split())))

def func():
    for Cx in range(0, 101):
        for Cy in range(0, 101):
            H=[el[2]+abs(el[0]-Cx)+abs(el[1]-Cy) for el in xyh if el[2]!=0]
            if H[0]>=1 and len(set(H))==1:
                temp=[H[0]-abs(el[0]-Cx)-abs(el[1]-Cy) for el in xyh if el[2]==0]
                for el in temp:
                    if el>0:
                        break
                else:
                    print(Cx, Cy, H[0])
                    return None
func()
Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

for i in range(N, M+1):
    if M%i==0:
        print(M//i)
        break

Fixed submission:
N, M = map(int, input().split())

import math

divisor=[]
for i in range(1, math.ceil(math.sqrt(M))+1):
    if M%i==0:
        divisor.append(i)
#print(divisor)

divisor+=[M//val for val in reversed(divisor)]
#print(divisor)
for val in divisor:
    if val>=N:
        print(M//val)
        break

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

for i in range(N, M+1):
    if M%i==0:
        print(M//i)
        break

Fixed submission:
N, M = map(int, input().split())

import math

divisor=[]
for i in range(1, math.ceil(math.sqrt(M))+1):
    if M%i==0:
        divisor.append(i)
#print(divisor)

divisor+=[M//val for val in reversed(divisor)]
#print(divisor)
for val in divisor:
    if val>=N:
        print(M//val)
        break

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

import math

divisor=[]
for i in range(1, M+1):
    if M%i==0:
        divisor.append(i)
#print(divisor)

divisor=list(reversed(divisor))
for val in divisor:
    if M//val>=N:
        print(val)
        break

Fixed submission:
N, M = map(int, input().split())

import math

divisor=[]
for i in range(1, math.ceil(math.sqrt(M))+1):
    if M%i==0:
        divisor.append(i)
#print(divisor)

divisor+=[M//val for val in reversed(divisor)]
#print(divisor)
for val in divisor:
    if val>=N:
        print(M//val)
        break

Buggy python submission (verdict: Time Limit Exceeded):
N, M = map(int, input().split())

import math

divisor=[]
for i in range(1, M+1):
    if M%i==0:
        divisor.append(i)
#print(divisor)

divisor=list(reversed(divisor))
for val in divisor:
    if M//val>=N:
        print(val)
        break

Fixed submission:
N, M = map(int, input().split())

import math

divisor=[]
for i in range(1, math.ceil(math.sqrt(M))+1):
    if M%i==0:
        divisor.append(i)
#print(divisor)

divisor+=[M//val for val in reversed(divisor)]
#print(divisor)
for val in divisor:
    if val>=N:
        print(M//val)
        break
Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int, input().split()))

def func(b):
    return sum([abs(val - b) for val in I])

#y seppen wo motomeru
import statistics
import math

I=[A[i]-i-1 for i in range(N)]

m=min(I)
M=max(I)

if N % 2 ==1:
    print(func(statistics.median(I)))
else:
    print(func(math.floor(statistics.median(I))))

'''
    for i in range(m, M+1):
        #print(i)
        temp=sum([abs(val-i) for val in I])
        if temp<opp:
            opp=temp
    print(opp)
'''

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

def func(b):
    return sum([abs(val - b) for val in I])

#y seppen wo motomeru

I=[A[i]-i-1 for i in range(N)]
I.sort()

if N % 2 ==1:
    print(func(I[N//2]))
else:
    print(func(I[N//2]))

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))

#y seppen wo motomeru

I=[A[i]-i-1 for i in range(N)]

m=min(I)
M=max(I)

opp=10**100
for i in range(m, M+1):
    #print(i)
    temp=sum([abs(val-i) for val in I])
    if temp<opp:
        opp=temp
print(opp)

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

def func(b):
    return sum([abs(val - b) for val in I])

#y seppen wo motomeru

I=[A[i]-i-1 for i in range(N)]
I.sort()

if N % 2 ==1:
    print(func(I[N//2]))
else:
    print(func(I[N//2]))
Buggy python submission (verdict: Time Limit Exceeded):
import sys
sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = []
for _ in range(M):
    xy.append(tuple(map(int, input().split())))
#print(xy)


G = [[] for _ in range(N+1)]
for el in xy:
    x, y = el
    G[x].append(y)
    G[y].append(x)


seen=[False] * (N+1)
def dfs(n):
    seen[n]=True
    group[-1].append(n)
    for child in G[n]:
        if seen[child] == False:
            dfs(child)

group=[]
for i in range(1,N+1):
    if seen[i] == False:
        group.append([])
        dfs(i)
        #print(seen)
#print(seen)
#print(group)

cnt = 0
for el in group:
    temp = set(el) & set([p[i-1] for i in el])
    cnt += len(temp)
print(cnt)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = []
for _ in range(M):
    xy.append(tuple(map(int, input().split())))
#print(xy)


G = [[] for _ in range(N+1)]
for el in xy:
    x, y = el
    G[x].append(y)
    G[y].append(x)


seen=[False] * (N+1)
def dfs(n):
    seen[n]=True
    group[-1].append(n)
    for child in G[n]:
        if seen[child] == False:
            dfs(child)

group=[]
for i in range(1,N+1):
    if seen[i] == False:
        group.append([])
        dfs(i)
        #print(seen)
#print(seen)
#print(group)

cnt = 0
for el in group:
    temp = set(el) & set([p[i-1] for i in el])
    cnt += len(temp)
print(cnt)

Buggy python submission (verdict: Runtime Error):
N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = []
for _ in range(M):
    xy.append(tuple(map(int, input().split())))
#print(xy)


G = [[] for _ in range(N+1)]
for el in xy:
    x, y = el
    G[x].append(y)
    G[y].append(x)


seen=[False] * (N+1)
def dfs(n):
    seen[n]=True
    group[-1].append(n)
    for child in G[n]:
        if seen[child] == False:
            dfs(child)

group=[]
for i in range(1,N+1):
    if seen[i] == False:
        group.append([])
        dfs(i)
        #print(seen)
#print(seen)
#print(group)

cnt = 0
for el in group:
    temp = set(el) & set([p[i-1] for i in el])
    cnt += len(temp)
print(cnt)


Fixed submission:
import sys
sys.setrecursionlimit(10**6)

N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = []
for _ in range(M):
    xy.append(tuple(map(int, input().split())))
#print(xy)


G = [[] for _ in range(N+1)]
for el in xy:
    x, y = el
    G[x].append(y)
    G[y].append(x)


seen=[False] * (N+1)
def dfs(n):
    seen[n]=True
    group[-1].append(n)
    for child in G[n]:
        if seen[child] == False:
            dfs(child)

group=[]
for i in range(1,N+1):
    if seen[i] == False:
        group.append([])
        dfs(i)
        #print(seen)
#print(seen)
#print(group)

cnt = 0
for el in group:
    temp = set(el) & set([p[i-1] for i in el])
    cnt += len(temp)
print(cnt)