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

def add_x2y(x, y, d, key2rep):
    rep = key2rep[y]
    big_rep = key2rep[x]
    if rep == big_rep:
        return
    for old in d[big_rep]:
        d[rep].add(old)
        key2rep[old] = rep

N, M = map(int,input().split(' '))
p_list = [int(i) for i in input().split(' ')]
d = {i+1:set([i+1]) for i in range(N)}
key2rep = {i+1:i+1 for i in range(N)}

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), d, key2rep)

print(len([1 for i in range(N) if p_list[i] in d[key2rep[i+1]]]))



Fixed submission:
import sys

def key2rep_iter(key, key2rep, temp_list):
    if key2rep[key] == key:
        for i in temp_list:
            key2rep[i] = key2rep[key]
        return key
    temp_list.append(key)
    return key2rep_iter(key2rep[key], key2rep, temp_list)

def add_x2y(x, y, key2rep):
    rep = key2rep_iter(y, key2rep, [])
    big_rep = key2rep_iter(x, key2rep, [])
    if rep == big_rep:
        return
    key2rep[big_rep] = rep

N, M = map(int,input().split(' '))
p_list = [int(i) for i in input().split(' ')]
key2rep = {i+1:i+1 for i in range(N)}

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), key2rep)

print(len([1 for i in range(N) if key2rep_iter(p_list[i], key2rep, []) == key2rep_iter(i+1, key2rep, [])]))

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

def create_dict(N):
    d = {}
    key2rep = {}
    for i in range(N):
        d[i+1] = set([i+1])
        key2rep[i+1] = i+1
    return d, key2rep

def key2rep_iter(x, key2rep):
    if key2rep[x] != x:
        return key2rep_iter(key2rep[x], key2rep)
    return key2rep[x]

def add_x2y(x, y, d, key2rep):
    rep = key2rep_iter(y, key2rep)
    big_rep = key2rep_iter(x, key2rep)
    if rep == big_rep:
        return
    d[rep] = d[rep] | d[big_rep]
    key2rep[big_rep] = rep
    d[big_rep] = set()
#    print(d)

N, M = map(int,input().split(' '))
d, key2rep = create_dict(N)
p_list = list(map(int,input().split(' ')))

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), d, key2rep)

score = 0
for i in d:
    for x in d[i]:
        if p_list[x-1] in d[i]:
            score += 1
print(score)


Fixed submission:
import sys

def key2rep_iter(key, key2rep, temp_list):
    if key2rep[key] == key:
        for i in temp_list:
            key2rep[i] = key2rep[key]
        return key
    temp_list.append(key)
    return key2rep_iter(key2rep[key], key2rep, temp_list)

def add_x2y(x, y, key2rep):
    rep = key2rep_iter(y, key2rep, [])
    big_rep = key2rep_iter(x, key2rep, [])
    if rep == big_rep:
        return
    key2rep[big_rep] = rep

N, M = map(int,input().split(' '))
p_list = [int(i) for i in input().split(' ')]
key2rep = {i+1:i+1 for i in range(N)}

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), key2rep)

print(len([1 for i in range(N) if key2rep_iter(p_list[i], key2rep, []) == key2rep_iter(i+1, key2rep, [])]))

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

def create_dict(N):
    d = {}
    key2rep = {}
    for i in range(N):
        d[i+1] = set([i+1])
        key2rep[i+1] = i+1
    return d, key2rep

def add_x2y(x, y, d, key2rep):
    rep = key2rep[y]
    big_rep = key2rep[x]
    if rep == big_rep:
        return
    for old in d[big_rep]:
        d[rep].add(old)
        key2rep[old] = rep
    d[big_rep] = set()
#    print(d)

N, M = map(int,input().split(' '))
d, key2rep = create_dict(N)
p_list = list(map(int,input().split(' ')))

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), d, key2rep)

score = 0
for i in d:
    for x in d[i]:
        if p_list[x-1] in d[i]:
            score += 1
print(score)


Fixed submission:
import sys

def key2rep_iter(key, key2rep, temp_list):
    if key2rep[key] == key:
        for i in temp_list:
            key2rep[i] = key2rep[key]
        return key
    temp_list.append(key)
    return key2rep_iter(key2rep[key], key2rep, temp_list)

def add_x2y(x, y, key2rep):
    rep = key2rep_iter(y, key2rep, [])
    big_rep = key2rep_iter(x, key2rep, [])
    if rep == big_rep:
        return
    key2rep[big_rep] = rep

N, M = map(int,input().split(' '))
p_list = [int(i) for i in input().split(' ')]
key2rep = {i+1:i+1 for i in range(N)}

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), key2rep)

print(len([1 for i in range(N) if key2rep_iter(p_list[i], key2rep, []) == key2rep_iter(i+1, key2rep, [])]))

Buggy python submission (verdict: Runtime Error):
import sys

def create_dict(N):
    d = {}
    key2rep = {}
    for i in range(N):
        d[i+1] = set([i+1])
        key2rep[i+1] = i+1
    return d, key2rep

def add_x2y(x, y, d, key2rep):
#    print(y)
    rep = key2rep[y]
    big_rep = key2rep[x]
    for old in d[big_rep]:
        d[rep].add(old)
        key2rep[old] = rep
    del d[big_rep]
#    print(d)

N, M = map(int,input().split(' '))
d, key2rep = create_dict(N)
p_list = list(map(int,input().split(' ')))

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), d, key2rep)

score = 0
for i in d:
    for x in d[i]:
        if p_list[x-1] in d[i]:
            score += 1
print(score)


Fixed submission:
import sys

def key2rep_iter(key, key2rep, temp_list):
    if key2rep[key] == key:
        for i in temp_list:
            key2rep[i] = key2rep[key]
        return key
    temp_list.append(key)
    return key2rep_iter(key2rep[key], key2rep, temp_list)

def add_x2y(x, y, key2rep):
    rep = key2rep_iter(y, key2rep, [])
    big_rep = key2rep_iter(x, key2rep, [])
    if rep == big_rep:
        return
    key2rep[big_rep] = rep

N, M = map(int,input().split(' '))
p_list = [int(i) for i in input().split(' ')]
key2rep = {i+1:i+1 for i in range(N)}

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), key2rep)

print(len([1 for i in range(N) if key2rep_iter(p_list[i], key2rep, []) == key2rep_iter(i+1, key2rep, [])]))

Buggy python submission (verdict: Runtime Error):
import sys

def create_dict(N):
    d = {}
    for i in range(N):
        d[i+1] = set([i+1])
    return d

N, M = map(int,input().split(' '))
d = create_dict(N)
p_list = list(map(int,input().split(' ')))

x_list = []
y_list = []
for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    d[min(x, y)].add(max(x,y))
    del d[max(x,y)]

score = 0
for i in d:
    for x in d[i]:
        if p_list[x-1] in d[i]:
            score += 1
print(score)


Fixed submission:
import sys

def key2rep_iter(key, key2rep, temp_list):
    if key2rep[key] == key:
        for i in temp_list:
            key2rep[i] = key2rep[key]
        return key
    temp_list.append(key)
    return key2rep_iter(key2rep[key], key2rep, temp_list)

def add_x2y(x, y, key2rep):
    rep = key2rep_iter(y, key2rep, [])
    big_rep = key2rep_iter(x, key2rep, [])
    if rep == big_rep:
        return
    key2rep[big_rep] = rep

N, M = map(int,input().split(' '))
p_list = [int(i) for i in input().split(' ')]
key2rep = {i+1:i+1 for i in range(N)}

for line in sys.stdin:
    x,y = map(int, line.strip().split(' '))
    add_x2y(max(x,y), min(x,y), key2rep)

print(len([1 for i in range(N) if key2rep_iter(p_list[i], key2rep, []) == key2rep_iter(i+1, key2rep, [])]))