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

input = sys.stdin.readline

def search(r):
    while root[r] != r: r = root[r]
    return r
        
def rebuild(r, n):
    while root[r] != r: root[r], r = n, root[r]
    
N, M = map(int, input().split())
p = list(map(int, input().split()))

root = {i:i for i in range(1, N+1)}
size = {i:1 for i in range(1, N+1)}

for i in range(M):
    x, y = map(int, input().split())
    x, y = p[x-1], p[y-1]
    
    x_r, y_r = search(x), search(y)
    if x_r == y_r: continue

    root[x_r] = y_r
    size[y_r] = max(size[y_r], size[x_r])
    
    if size[y_r] > 10:
        rebuild(x, y_r)
        rebuild(y, y_r)
        size[y_r] = 1
    
ans = 0

for i, j in enumerate(p):
    if search(i+1) == search(j): ans += 1
print(ans)

Fixed submission:
def find(x):
    while union[x] != x:
        x = union[x]
        
    return x

N, M = map(int, input().split())
p = list(map(int, input().split()))

union = [i for i in range(N+1)]

for i in range(M):
    x, y = map(int, input().split())
    x = find(x)
    while union[y] != y:
        union[y], y = x, union[y]
        
    union[y] = x
    
ans = 0
for idx, n in enumerate(p):
    if idx+1 == n:
        ans += 1
    else:
        x, y = find(idx+1), find(n)
        if x == y:
            ans += 1
        
print(ans)

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

input = sys.stdin.readline

def search(r):
    while root[r] != r: r = root[r]
    return r
        
N, M = map(int, input().split())
p = list(map(int, input().split()))

root = {i:i for i in range(1, N+1)}
size = {i:1 for i in range(1, N+1)}

for i in range(M):
    x, y = map(int, input().split())
    x, y = p[x-1], p[y-1]
    
    x_r, y_r = search(x), search(y)
    if x_r == y_r: continue

    root[x_r] = y_r
    size[y_r] += size[x_r]
    
ans = 0

for i, j in enumerate(p):
    if search(i+1) == search(j): ans += 1
print(ans)

Fixed submission:
def find(x):
    while union[x] != x:
        x = union[x]
        
    return x

N, M = map(int, input().split())
p = list(map(int, input().split()))

union = [i for i in range(N+1)]

for i in range(M):
    x, y = map(int, input().split())
    x = find(x)
    while union[y] != y:
        union[y], y = x, union[y]
        
    union[y] = x
    
ans = 0
for idx, n in enumerate(p):
    if idx+1 == n:
        ans += 1
    else:
        x, y = find(idx+1), find(n)
        if x == y:
            ans += 1
        
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def find(x):
    while union[x] != x:
        x = union[x]
    return x

N, M = map(int, input().split())
p = list(map(int, input().split()))

union = [i for i in range(N+1)]

for i in range(M):
    x, y = map(int, input().split())
    a, b = find(x), find(y)
    if a != b:
        union[a] = b

ans = 0
for idx, n in enumerate(p):
    x, y = find(idx+1), find(n)
    if x == y:
        ans += 1
    
print(ans)

Fixed submission:
def find(x):
    while union[x] != x:
        x = union[x]
        
    return x

N, M = map(int, input().split())
p = list(map(int, input().split()))

union = [i for i in range(N+1)]

for i in range(M):
    x, y = map(int, input().split())
    x = find(x)
    while union[y] != y:
        union[y], y = x, union[y]
        
    union[y] = x
    
ans = 0
for idx, n in enumerate(p):
    if idx+1 == n:
        ans += 1
    else:
        x, y = find(idx+1), find(n)
        if x == y:
            ans += 1
        
print(ans)

Buggy python submission (verdict: Runtime Error):
H, W, D = map(int, input().split())

#a = [0] * (H*W+1)
a = {}

for i in range(H):
    A = list(map(int, input().split()))
    for j, _ in enumerate(A):
        a[_] = (i+1, j+1)

#table = [0] * (H*W+1)
table = {i:0 for i in range(H*W+1)}

for i in range(D+1, H*W):
    x, y = a[i]
    x_, y_ = a[i-D]
    table[i] = table[i-D] + abs(x - x_) + abs(y - y_)

for i in range(int(input())):
    L, R = map(int, input().split())
    print(table[R] - table[L])

Fixed submission:
def find(x):
    while union[x] != x:
        x = union[x]
        
    return x

N, M = map(int, input().split())
p = list(map(int, input().split()))

union = [i for i in range(N+1)]

for i in range(M):
    x, y = map(int, input().split())
    x = find(x)
    while union[y] != y:
        union[y], y = x, union[y]
        
    union[y] = x
    
ans = 0
for idx, n in enumerate(p):
    if idx+1 == n:
        ans += 1
    else:
        x, y = find(idx+1), find(n)
        if x == y:
            ans += 1
        
print(ans)

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

root = {i:i for i in range(1, N+1)}
find = {idx+1:{n} for idx, n in enumerate(p)}

ans = 0
for i in range(M):
    x_, y_ = list(map(int, input().split()))
    x, y = x_, y_

    while x != root[x]:
        x = root[x]

    while y != root[y]:
        y = root[y]

    if x != y:
        find[x] |= find[y]
        find[y] = None
        root[x_] = root[y_] = root[y] = x

for i in range(1, N+1):
    r = root[i]
    while r != root[r]:
        r = root[r]

    if i in find[r]:
        find[r].remove(i)
        ans += 1
print(ans)

Fixed submission:
def find(x):
    while union[x] != x:
        x = union[x]
        
    return x

N, M = map(int, input().split())
p = list(map(int, input().split()))

union = [i for i in range(N+1)]

for i in range(M):
    x, y = map(int, input().split())
    x = find(x)
    while union[y] != y:
        union[y], y = x, union[y]
        
    union[y] = x
    
ans = 0
for idx, n in enumerate(p):
    if idx+1 == n:
        ans += 1
    else:
        x, y = find(idx+1), find(n)
        if x == y:
            ans += 1
        
print(ans)