Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
p = list(map(int, input().split()))

l = [[] for j in range(n)]

for i in range(m):
    x,y = map(int, input().split())
    l[x-1].append(y-1)
    l[y-1].append(x-1)

connect = []
a = []
v = []

for i in range(n):
    if i not in v:
        connect.append([i])
        a.append([p[i]-1])
        v.append(i)
        stack =[i]
        visited = [i]
        while stack:
            q = stack.pop()
            for j in range(len(l[q])):
                if l[q][j] not in visited:
                    connect[-1].append(l[q][j])
                    a[-1].append(p[l[q][j]]-1)
                    visited.append(l[q][j])
                    stack.append(l[q][j])
                    v.append(l[q][j])

ans = 0

for i in range(len(connect)):
    ans += len(set(a[i]) & set(connect[i]))

print(ans)


Fixed submission:
n,m = map(int, input().split())
p = list(map(int, input().split()))

l = [[] for j in range(n)]

for i in range(m):
    x,y = map(int, input().split())
    l[x-1].append(y-1)
    l[y-1].append(x-1)

connect = []
a = []
v = [0 for i in range(n)]

for i in range(n):
    if v[i] == 0:
        connect.append([i])
        a.append([p[i]-1])
        v[i]=1
        stack =[i]
        while stack:
            q = stack.pop()
            for j in range(len(l[q])):
                if v[l[q][j]]==0:
                    connect[-1].append(l[q][j])
                    a[-1].append(p[l[q][j]]-1)
                    stack.append(l[q][j])
                    v[l[q][j]]=1

ans = 0

for i in range(len(connect)):
    ans += len(set(a[i]) & set(connect[i]))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
p = list(map(int, input().split()))

l = [[] for j in range(n)]

for i in range(m):
    x,y = map(int, input().split())
    l[x-1].append(y-1)
    l[y-1].append(x-1)

connect = []
v = []

for i in range(n):
    if i not in v:
        connect.append([i])
        v.append(i)
        stack =[i]
        visited = [i]
        while stack:
            q = stack.pop()
            for j in range(len(l[q])):
                if l[q][j] not in visited:
                    connect[-1].append(l[q][j])
                    visited.append(l[q][j])
                    stack.append(l[q][j])
                    v.append(l[q][j])

a = []
for i in range(len(connect)):
    a.append([])
    for j in connect[i]:
        a[-1].append(p[j]-1)

ans = 0

for i in range(len(connect)):
    ans += len(set(a[i]) & set(connect[i]))

print(ans)


Fixed submission:
n,m = map(int, input().split())
p = list(map(int, input().split()))

l = [[] for j in range(n)]

for i in range(m):
    x,y = map(int, input().split())
    l[x-1].append(y-1)
    l[y-1].append(x-1)

connect = []
a = []
v = [0 for i in range(n)]

for i in range(n):
    if v[i] == 0:
        connect.append([i])
        a.append([p[i]-1])
        v[i]=1
        stack =[i]
        while stack:
            q = stack.pop()
            for j in range(len(l[q])):
                if v[l[q][j]]==0:
                    connect[-1].append(l[q][j])
                    a[-1].append(p[l[q][j]]-1)
                    stack.append(l[q][j])
                    v[l[q][j]]=1

ans = 0

for i in range(len(connect)):
    ans += len(set(a[i]) & set(connect[i]))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m = map(int, input().split())
p = list(map(int, input().split()))

l = [[0 for i in range(n)] for j in range(n)]

for i in range(m):
    x,y = map(int, input().split())
    l[x-1][y-1] = 1
    l[y-1][x-1] = 1

connect = []
v = []

for i in range(n):
    if i not in v:
        connect.append([i])
        v.append(i)
        stack =[i]
        visited = [i]
        while stack:
            q = stack.pop()
            for j in range(n):
                if l[q][j]==1 and j not in visited:
                    connect[-1].append(j)
                    visited.append(j)
                    stack.append(j)
                    v.append(j)

a = []
for i in range(len(connect)):
    a.append([])
    for j in connect[i]:
        a[-1].append(p[j]-1)

ans = 0

for i in range(len(connect)):
    ans += len(set(a[i]) & set(connect[i]))

print(ans)


Fixed submission:
n,m = map(int, input().split())
p = list(map(int, input().split()))

l = [[] for j in range(n)]

for i in range(m):
    x,y = map(int, input().split())
    l[x-1].append(y-1)
    l[y-1].append(x-1)

connect = []
a = []
v = [0 for i in range(n)]

for i in range(n):
    if v[i] == 0:
        connect.append([i])
        a.append([p[i]-1])
        v[i]=1
        stack =[i]
        while stack:
            q = stack.pop()
            for j in range(len(l[q])):
                if v[l[q][j]]==0:
                    connect[-1].append(l[q][j])
                    a[-1].append(p[l[q][j]]-1)
                    stack.append(l[q][j])
                    v[l[q][j]]=1

ans = 0

for i in range(len(connect)):
    ans += len(set(a[i]) & set(connect[i]))

print(ans)