Buggy python submission (verdict: Time Limit Exceeded):
import collections
import sys

input = sys.stdin.readline
N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = [tuple(map(int, input().split())) for i in range(M)]
table = []
table2 = set()
ans = 0
for i, x in enumerate(p):
    if i+1 == x:
        ans += 1
    else:
        table2.add(x)
id = 0
for x, y in xy:
    a, b = -1, -1
    for z in range(len(table)):
        if x in table[z]:
            a = z
            break
    for z in range(len(table)):
        if y in table[z]:
            b = z
            break
    if a == b:
        if a == -1:
            table.append({x, y})
    elif a == -1 or b == -1:
        if a == -1:
            table[b].add(x)
        else:
            table[a].add(y)
    else:
        table[a] |= table[b]
        table[b], table[-1] = table[-1], table[b]
        table.pop()


for x in range(len(table)):
    tmp = set()
    for a in table[x]:
        tmp.add(p[a-1])
    ans += len(table2 & table[x] & tmp)

print(ans)


Fixed submission:
import collections
import sys


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            return


input = sys.stdin.readline
N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = [tuple(map(int, input().split())) for i in range(M)]
ans = 0
t = unionFind(N)

for x, y in xy:
    t.unite(x, y)

for i in range(N):
    if t.same(i+1, p[i]):
        ans += 1
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
import sys
input = sys.stdin.readline
N, M = map(int, input().split())
p = list(map(int, input().split()))

xy = [tuple(map(int, input().split())) for i in range(M)]

table = collections.defaultdict(set)

table2 = collections.defaultdict(int)
ans = 0
for i, x in enumerate(p):
    if i+1 == x:
        ans += 1
    else:
        table2[i + 1] = x
id = 0
for x, y in xy:
    a, b = -1, -1
    for z in table:
        if x in table[z]:
            a = z
            break
    for z in table:
        if y in table[z]:
            b = z
            break
    if a == b:
        if a == -1:
            table[id].add(x)
            table[id].add(y)
            id += 1
    elif a == -1 or b == -1:
        if a == -1:
            table[b].add(x)
        else:
            table[a].add(y)
    else:
        table[a] |= (table[b])
        del(table[b])

for x in table:
    for a in table[x]:
        if a in table2 and table2[a] in table[x]:
            ans += 1
print(ans)


Fixed submission:
import collections
import sys


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            return


input = sys.stdin.readline
N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = [tuple(map(int, input().split())) for i in range(M)]
ans = 0
t = unionFind(N)

for x, y in xy:
    t.unite(x, y)

for i in range(N):
    if t.same(i+1, p[i]):
        ans += 1
print(ans)

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

xy = [tuple(map(int, input().split())) for i in range(M)]

table = collections.defaultdict(set)

table2 = collections.defaultdict(int)
ans = 0
for i, x in enumerate(p):
    if i+1 == x:
        ans += 1
    else:
        table2[i + 1] = x
id = 0
for x, y in xy:
    a, b = -1, -1
    for z in table:
        if x in table[z]:
            a = z
            break
    for z in table:
        if y in table[z]:
            b = z
            break
    if a == b:
        if a == -1:
            table[id].add(x)
            table[id].add(y)
            id += 1
    elif a == -1 or b == -1:
        if a == -1:
            table[b].add(x)
        else:
            table[a].add(y)
    else:
        table[a] |= (table[b])
        del(table[b])

for x in table:
    for a in table[x]:
        if a in table2 and table2[a] in table[x]:
            ans += 1
print(ans)


Fixed submission:
import collections
import sys


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            return


input = sys.stdin.readline
N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = [tuple(map(int, input().split())) for i in range(M)]
ans = 0
t = unionFind(N)

for x, y in xy:
    t.unite(x, y)

for i in range(N):
    if t.same(i+1, p[i]):
        ans += 1
print(ans)

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

xy = [tuple(map(int, input().split())) for i in range(M)]

table = collections.defaultdict(set)

table2 = collections.defaultdict(int)
ans = 0
for i, x in enumerate(p):
    if i+1 == x:
        ans += 1
    else:
        table2[i + 1] = x
id = 0
for x, y in xy:
    a, b = -1, -1
    for z in table:
        if x in table[z]:
            a = z
            break
    for z in table:
        if y in table[z]:
            b = z
            break
    if a == b:
        if a == -1:
            table[id].add(x)
            table[id].add(y)
            id += 1
    elif a == -1 or b == -1:
        if a == -1:
            table[b].add(x)
        else:
            table[a].add(y)
    else:
        table[a] |= (table[b])
        del(table[b])

for x in table:
    for a in table[x]:
        if a in table2 and table2[a] in table[x]:
            ans += 1
print(ans)


Fixed submission:
import collections
import sys


class unionFind:
    parent = []

    def __init__(self, N):
        self.parent = [i for i in range(N)]

    def root(self, x):
        if self.parent[x] == x:
            return(x)
        else:
            self.parent[x] = self.root(self.parent[x])
            return(self.parent[x])

    def same(self, x, y):
        x, y = x-1, y-1
        return(self.root(x) == self.root(y))

    def unite(self, x, y):
        x, y = x-1, y-1
        x = self.root(x)
        y = self.root(y)
        if x == y:
            return
        else:
            self.parent[x] = y
            return


input = sys.stdin.readline
N, M = map(int, input().split())
p = list(map(int, input().split()))
xy = [tuple(map(int, input().split())) for i in range(M)]
ans = 0
t = unionFind(N)

for x, y in xy:
    t.unite(x, y)

for i in range(N):
    if t.same(i+1, p[i]):
        ans += 1
print(ans)