Buggy python submission (verdict: Time Limit Exceeded):
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
 
    def root(self, i):
        while self.parent[i] != i:
            i = self.parent[i]
        return i
    def find(self, x, y):
        return self.root(x) == self.root(y)
 
    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        self.parent[j] = i
N, M = map(int, input().split())
ps = list(map(int, input().split()))
ps = [i-1 for i in ps]
swap =[list(map(int, input().split())) for i in range(M)]
u = UnionFind(N)
for x, y in swap:
    u.union(x-1, y-1)
ans = 0
for i, p in enumerate(ps):
    if u.find(i, p):
        ans += 1
print(ans)

Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
 
    def root(self, i):
        if self.parent[i] == i:
            return i
        else:
            self.parent[i] = self.root(self.parent[i])
            return self.parent[i]
    
    def find(self, x, y):
        return self.root(x) == self.root(y)
 
    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        self.parent[j] = i
N, M = map(int, input().split())
ps = list(map(int, input().split()))
ps = [i-1 for i in ps]
swap =[list(map(int, input().split())) for i in range(M)]
u = UnionFind(N)
for x, y in swap:
    u.union(x-1, y-1)
ans = 0
for i, p in enumerate(ps):
    if u.find(i, p):
        ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
 
    def root(self, i):
        while self.parent[i] != i:
            i = self.parent[i]
        return i
    def find(self, x, y):
        return self.root(x) == self.root(y)
 
    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        self.parent[j] = i
N, M = map(int, input().split())
ps = list(map(int, input().split()))
ps = [i-1 for i in ps]
swap =[list(map(int, input().split())) for i in range(M)]
u = UnionFind(N)
for x, y in swap:
    u.union(x-1, y-1)
ans = 0
for i, p in enumerate(ps):
    if u.find(i, p):
        ans += 1
print(ans)

Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
 
    def root(self, i):
        if self.parent[i] == i:
            return i
        else:
            self.parent[i] = self.root(self.parent[i])
            return self.parent[i]
    
    def find(self, x, y):
        return self.root(x) == self.root(y)
 
    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        self.parent[j] = i
N, M = map(int, input().split())
ps = list(map(int, input().split()))
ps = [i-1 for i in ps]
swap =[list(map(int, input().split())) for i in range(M)]
u = UnionFind(N)
for x, y in swap:
    u.union(x-1, y-1)
ans = 0
for i, p in enumerate(ps):
    if u.find(i, p):
        ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
 
    def find(self, i):
        while self.parent[i] != i:
            i = self.parent[i]
        return i
 
    def union(self, i, j):
        i = self.find(i)
        j = self.find(j)
        self.parent[j] = i
N, M = map(int, input().split())
ps = list(map(int, input().split()))
ps = [i-1 for i in ps]
swap =[list(map(int, input().split())) for i in range(M)]
u = UnionFind(N)
for x, y in swap:
    u.union(x-1, y-1)
for i in range(N):
    u.parent[i] = u.find(i)
a = {}
for i, k in enumerate(u.parent):
    if k in a:
        a[k].append(i)
    else:
        a[k] = [i]
ans = 0
for i, p in enumerate(ps):
    for l in a.values():
        if i in l and p in l:
            ans += 1
print(ans)



Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
 
    def root(self, i):
        if self.parent[i] == i:
            return i
        else:
            self.parent[i] = self.root(self.parent[i])
            return self.parent[i]
    
    def find(self, x, y):
        return self.root(x) == self.root(y)
 
    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        self.parent[j] = i
N, M = map(int, input().split())
ps = list(map(int, input().split()))
ps = [i-1 for i in ps]
swap =[list(map(int, input().split())) for i in range(M)]
u = UnionFind(N)
for x, y in swap:
    u.union(x-1, y-1)
ans = 0
for i, p in enumerate(ps):
    if u.find(i, p):
        ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
 
    def find(self, i):
        while self.parent[i] != i:
            i = self.parent[i]
        return i
 
    def union(self, i, j):
        i = self.find(i)
        j = self.find(j)
        self.parent[j] = i
N, M = map(int, input().split())
ps = list(map(int, input().split()))
ps = [i-1 for i in ps]
swap =[list(map(int, input().split())) for i in range(M)]
u = UnionFind(N)
for x, y in swap:
    u.union(x-1, y-1)
for i in range(N):
    u.parent[i] = u.find(i)
a = {}
for i, k in enumerate(u.parent):
    if k in a:
        a[k].append(i)
    else:
        a[k] = [i]
ans = 0
for i, p in enumerate(ps):
    for l in a.values():
        if i in l and p in l:
            ans += 1
print(ans)



Fixed submission:
class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
 
    def root(self, i):
        if self.parent[i] == i:
            return i
        else:
            self.parent[i] = self.root(self.parent[i])
            return self.parent[i]
    
    def find(self, x, y):
        return self.root(x) == self.root(y)
 
    def union(self, i, j):
        i = self.root(i)
        j = self.root(j)
        self.parent[j] = i
N, M = map(int, input().split())
ps = list(map(int, input().split()))
ps = [i-1 for i in ps]
swap =[list(map(int, input().split())) for i in range(M)]
u = UnionFind(N)
for x, y in swap:
    u.union(x-1, y-1)
ans = 0
for i, p in enumerate(ps):
    if u.find(i, p):
        ans += 1
print(ans)