Buggy python submission (verdict: Time Limit Exceeded):
# D - Equals

import sys
from collections import deque

N, M = map(int, sys.stdin.readline().split())
p = list(map(int, sys.stdin.readline().split()))
xy = sys.stdin.readlines()

neighbor = [[] for _ in range(N)]
for idx in range(M):
    x,y = map(int, xy[idx].split())
    neighbor[x-1].append(y-1)
    neighbor[y-1].append(x-1)
    
can_swap = []
visited = [-1]*N

for start in range(N):
    if visited[start] == -1:
        tmp = [start] # startから到達可能なpointのリスト
        d = deque([start])
        while d:
            current_point = d.pop()
            visited[current_point] = 1
            for next_point in neighbor[current_point]:
                if visited[next_point] == -1:
                    d.append(next_point)
                    tmp.append(next_point)
        can_swap.append(tmp)                

ans = 0
for idx_group in can_swap:
    tmp_p = [p[idx]-1 for idx in idx_group]
    ans += len(set(tmp_p) & set(idx_group))

print(ans)

Fixed submission:
# D - Equals

from collections import deque

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

neighbor = [[] for _ in range(N)]
for idx in range(M):
    neighbor[xy[idx][0]-1].append(xy[idx][1]-1)
    neighbor[xy[idx][1]-1].append(xy[idx][0]-1)
    
can_swap = []
visited = [-1]*N

for start in range(N):
    if visited[start] == -1:
        tmp = [start] # startから到達可能なpointのリスト
        d = deque([start])
        while d:
            current_point = d.pop()
            visited[current_point] = 1
            for next_point in neighbor[current_point]:
                if visited[next_point] == -1:
                    d.append(next_point)
                    tmp.append(next_point)
        can_swap.append(tmp)                

ans = 0
for idx_group in can_swap:
    tmp_p = [p[idx]-1 for idx in idx_group]
    ans += len(set(tmp_p) & set(idx_group))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# D - Equals

from collections import deque

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

neighbor = [[] for _ in range(N)]
for idx in range(M):
    neighbor[xy[idx][0]-1].append(xy[idx][1]-1)
    neighbor[xy[idx][1]-1].append(xy[idx][0]-1)
    
can_swap = []
visited = [-1]*N

for start in range(N):
    if visited[start] == -1:
        tmp = [start] # startから到達可能なpointのリスト
        d = deque([start])
        while d:
            current_point = d.pop()
            visited[current_point] = 1
            for next_point in neighbor[current_point]:
                if visited[next_point] == -1:
                    d.append(next_point)
                    tmp.append(next_point)
        can_swap.append(tmp)                

ans = 0
for idx_group in can_swap:
    tmp_p = [p[idx]-1 for idx in idx_group]
    ans += len(set(tmp_p) & set(idx_group))

print(ans)

Fixed submission:
# D - Equals

from collections import deque

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

neighbor = [[] for _ in range(N)]
for idx in range(M):
    neighbor[xy[idx][0]-1].append(xy[idx][1]-1)
    neighbor[xy[idx][1]-1].append(xy[idx][0]-1)
    
can_swap = []
visited = [-1]*N

for start in range(N):
    if visited[start] == -1:
        tmp = [start] # startから到達可能なpointのリスト
        d = deque([start])
        while d:
            current_point = d.pop()
            visited[current_point] = 1
            for next_point in neighbor[current_point]:
                if visited[next_point] == -1:
                    d.append(next_point)
                    tmp.append(next_point)
        can_swap.append(tmp)                

ans = 0
for idx_group in can_swap:
    tmp_p = [p[idx]-1 for idx in idx_group]
    ans += len(set(tmp_p) & set(idx_group))

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
# D - Equals

import numpy as np
import sys, bisect
from collections import deque

N, M = map(int, sys.stdin.buffer.readline().split())
p = np.array(list(map(int, sys.stdin.buffer.readline().split()))) - 1
xy = np.array(sys.stdin.buffer.read().split(), np.int64) - 1

x = xy[::2]
y = xy[1::2]

neighbor = [[] for _ in range(N)]
for idx in range(M):
    neighbor[x[idx]].append(y[idx])
    neighbor[y[idx]].append(x[idx])
    
can_swap = []
visited = [-1]*N

for start in range(N):
    if visited[start] == -1:
        tmp = [start] # startから到達可能なpointのリスト
        visited[start] = 1
        d = deque([start])
        while d:
            current_point = d.pop()
            visited[current_point] = 1
            for next_point in neighbor[current_point]:
                if visited[next_point] == -1:
                    d.append(next_point)
                    tmp.append(next_point)
        can_swap.append(tmp)                

ans = 0
for idx_group in can_swap:
    tmp_p = [p[idx] for idx in idx_group]
    ans += len(set(tmp_p) & set(idx_group))

print(ans)

Fixed submission:
# D - Equals

from collections import deque

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

neighbor = [[] for _ in range(N)]
for idx in range(M):
    neighbor[xy[idx][0]-1].append(xy[idx][1]-1)
    neighbor[xy[idx][1]-1].append(xy[idx][0]-1)
    
can_swap = []
visited = [-1]*N

for start in range(N):
    if visited[start] == -1:
        tmp = [start] # startから到達可能なpointのリスト
        d = deque([start])
        while d:
            current_point = d.pop()
            visited[current_point] = 1
            for next_point in neighbor[current_point]:
                if visited[next_point] == -1:
                    d.append(next_point)
                    tmp.append(next_point)
        can_swap.append(tmp)                

ans = 0
for idx_group in can_swap:
    tmp_p = [p[idx]-1 for idx in idx_group]
    ans += len(set(tmp_p) & set(idx_group))

print(ans)