Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
l=[list(map(lambda x:int(x)-1,input().split())) for _ in [0]*m]
s=[0]*n
i=1
for x in l:
    if s[x[0]]==s[x[1]]==0:
        s[x[0]]=i
        s[x[1]]=i
        i+=1
    elif s[x[0]]==s[x[1]]>0:
        pass
    elif s[x[0]]==0:
        s[x[0]]=s[x[1]]
    elif s[x[1]]==0:
        s[x[1]]=s[x[0]]
    else:
        t=s[x[1]]
        for j in range(n):
            if s[j]==t:s[j]=s[x[0]]
list=list(set(s))
ans=0
if 0 in list:
    for j in range(n):
        if s[j]==0 and p[j]==j:ans+=1
    list.remove(0)
for j in list:
    ans+=len(set([p[k] for k in range(n) if s[k]==j])&set([k for k in range(n) if s[k]==j]))
print(ans)

Fixed submission:
n,m=map(int,input().split())
p=list(map(int,input().split()))
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    def same_check(self, x, y):
        return self.find(x) == self.find(y)
uf=UnionFind(n)
for _ in range(m):
    x,y=map(int,input().split())
    uf.union(x,y)
ans=0
for i in range(n):
    if uf.same_check(i+1,p[i]):ans+=1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n,m=map(int,input().split())
p=list(map(lambda x:int(x)-1,input().split()))
l=[list(map(lambda x:int(x)-1,input().split())) for _ in [0]*m]
s=[0]*n
i=1
for x in l:
    if s[x[0]]==s[x[1]]==0:
        s[x[0]]=i
        s[x[1]]=i
        i+=1
    elif s[x[0]]==s[x[1]]>0:
        pass
    elif s[x[0]]==0:
        s[x[0]]=s[x[1]]
    elif s[x[1]]==0:
        s[x[1]]=s[x[0]]
    else:
        t=s[x[1]]
        for j in range(n):
            if s[j]==t:s[j]=s[x[0]]
list=list(set(s))
ans=0
if 0 in list:
    for j in range(n):
        if s[j]==0 and p[j]==j:ans+=1
    list.remove(0)
for j in list:
    ans+=len(set([p[k] for k in range(n) if s[k]==j])&set([k for k in range(n) if s[k]==j]))
print(ans)

Fixed submission:
n,m=map(int,input().split())
p=list(map(int,input().split()))
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1
    def same_check(self, x, y):
        return self.find(x) == self.find(y)
uf=UnionFind(n)
for _ in range(m):
    x,y=map(int,input().split())
    uf.union(x,y)
ans=0
for i in range(n):
    if uf.same_check(i+1,p[i]):ans+=1
print(ans)