Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = list(map(int, input().split()))
uf = [i for i in range(n + 1)]

def dfs(x):
    if uf[x] == x:
        return x
    return dfs(uf[x])

for _ in range(m):
    xy = list(map(int, input().split()))
    rx, ry = dfs(xy[0]), dfs(xy[1])
    if rx != ry:
        uf[rx] = ry
count = 0
for i in range(n):
    num = i + 1
    if p[i] == num or dfs(num) == dfs(p[i]):
        count += 1
print(count)

Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = list(map(int, input().split()))
uf = [i for i in range(n + 1)]

def dfs(x):
    if uf[x] == x:
        return x
    return dfs(uf[x])

for _ in range(m):
    xy = list(map(int, input().split()))
    rx, ry = dfs(xy[0]), dfs(xy[1])
    if rx != ry:
        uf[rx] = ry
count = sum([1 for i in range(n) if p[i] == i + 1 or dfs(i + 1) == dfs(p[i])])
print(count)

Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
p = list(map(int, input().split()))
uf = [i for i in range(n + 1)]

def dfs(x):
    if uf[x] == x:
        return x
    return dfs(uf[x])

for _ in range(m):
    xy = list(map(int, input().split()))
    rx, ry = dfs(xy[0]), dfs(xy[1])
    if rx != ry:
        uf[rx] = ry
count = 0
for i in range(n):
    num = i + 1
    if p[i] == num or dfs(num) == dfs(p[i]):
        count += 1
print(count)

Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
uf = {}

def dfs(cld):
    if cld not in uf:
        return []
    elif uf[cld] < 0:
        return [cld]
    return dfs(uf[cld]) + [cld]

for xy in xys:
    xin = (xy[0] in uf)
    yin = (xy[1] in uf)
    xp = dfs(xy[0])
    yp = dfs(xy[1])
    if xin and yin:
        if uf[xp[0]] == uf[yp[0]]:
            uf[yp[0]] = xp[0]
            uf[xp[0]] -= 1
        elif uf[xp[0]] < uf[yp[0]]:
            uf[xy[1]] = xp[0]
        else:
            uf[xy[0]] = yp[0]
    elif xin:
        uf[xy[1]] = xp[0]
    elif yin:
        uf[xy[0]] = yp[0]
    else:
        uf[xy[0]] = -1
        uf[xy[1]] = xy[0]
count = 0
for i in range(n):
    num = i + 1
    pi = p[i]
    if num == pi or sum(set(dfs(num)) & set(dfs(pi))) > 0:
        count += 1
print(count)

Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
uf = {}

def dfs(cld):
    if cld not in uf:
        return []
    elif uf[cld] < 0:
        return [cld]
    return dfs(uf[cld]) + [cld]

for xy in xys:
    xin = (xy[0] in uf)
    yin = (xy[1] in uf)
    xp = dfs(xy[0])
    yp = dfs(xy[1])
    if xin and yin:
        if uf[xp[0]] == uf[yp[0]]:
            uf[xy[1]] = xp[0]
            uf[xp[0]] -= 1
        elif uf[xp[0]] < uf[yp[0]]:
            uf[xy[1]] = xp[0]
        else:
            uf[xy[0]] = yp[0]
    elif xin:
        uf[xy[1]] = xp[0]
    elif yin:
        uf[xy[0]] = yp[0]
    else:
        uf[xy[0]] = -1
        uf[xy[1]] = xy[0]
count = 0
for i in range(n):
    num = i + 1
    pi = p[i]
    if num == pi or sum(set(dfs(num)) & set(dfs(pi))) > 0:
        count += 1
print(count)

Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
uf = {}

def dfs(cld):
    if cld not in uf:
        return []
    elif uf[cld] == None:
        return [cld]
    return dfs(uf[cld]) + [cld]

for xy in xys:
    xin = (xy[0] in uf)
    yin = (xy[1] in uf)
    xp = dfs(xy[0])
    yp = dfs(xy[1])
    if xin and yin:
        if len(xp) > len(yp):
            uf[xy[1]] = xp[0]
        else:
            uf[xy[0]] = yp[0]
    elif xin:
        uf[xy[1]] = xp[0]
    elif yin:
        uf[xy[0]] = yp[0]
    else:
        uf[xy[0]] = None
        uf[xy[1]] = xy[0]
count = 0
for i in range(n):
    num = i + 1
    pi = p[i]
    if sum(set(dfs(num)) & set(dfs(pi))) > 0:
        count += 1
    elif num == pi:
        count += 1
print(count)

Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)

Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
uf = {}

def dfs(cld):
    if cld not in uf:
        return []
    elif uf[cld] == None:
        return [cld]
    elif cld in uf:
        return dfs(uf[cld]) + [cld]

for xy in xys:
    xin = (xy[0] in uf)
    yin = (xy[1] in uf)
    xp = dfs(xy[0])
    yp = dfs(xy[1])
    if xin and yin:
        if len(xp) > len(yp):
            uf[xy[1]] = xy[0]
        else:
            uf[xy[0]] = xy[1]
    elif xin:
        uf[xy[1]] = xp[0]
    elif yin:
        uf[xy[0]] = yp[0]
    else:
        uf[xy[0]] = None
        uf[xy[1]] = xy[0]
count = 0
for i in range(n):
    num = i + 1
    pi = p[i]
    if sum(set(dfs(num)) & set(dfs(pi))) > 0:
        count += 1
    elif num == pi:
        count += 1
print(count)

Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
lsxy = {}
stxy = {}
maxi = 0
allst = set()
for xy in xys:
    xin = True if xy[0] in lsxy else False
    yin = True if xy[1] in lsxy else False
    allst.add(xy[0])
    allst.add(xy[1])
    if xin and yin:
        xidx = lsxy[xy[0]]
        yidx = lsxy[xy[1]]
        stx = stxy[xidx]
        sty = stxy[yidx]
        if len(stx) > len(sty):
            stxy[xidx] |= stxy.pop(yidx)
            for i in sty:
                lsxy[i] = xidx
        else:
            stxy[yidx] |= stxy.pop(xidx)
            for i in stx:
                lsxy[i] = yidx
    elif xin:
        lsxy[xy[1]] = lsxy[xy[0]]
        stxy[lsxy[xy[0]]].add(xy[1])
    elif yin:
        lsxy[xy[0]] = lsxy[xy[1]]
        stxy[lsxy[xy[1]]].add(xy[0])
    else:
        stxy[maxi] = set(xy)
        lsxy[xy[0]] = maxi
        lsxy[xy[1]] = maxi
        maxi += 1
count = 0
for i in range(n):
    if i + 1 == p[i]:
        count += 1
    elif i + 1 in lsxy and p[i] in stxy[lsxy[i + 1]]:
        count += 1
print(count)


Fixed submission:
n, m = map(int, input().split())
p = list(map(int, input().split()))
xys = [list(map(int, input().split())) for _ in range(m)]
mapp = [[] for _ in range(n + 1)]
for xy in xys:
    mapp[xy[0]].append(xy[1])
    mapp[xy[1]].append(xy[0])

bfs = [0] * (n + 1)
count = 0
for i in range(1, n + 1):
    nexts = mapp[i]
    if not(bfs[i]):
        bfs[i] = i
    for j in nexts:
        if bfs[j]:
            continue
        bfs[j] = i
        nexts += mapp[j]
    if bfs[i] == bfs[p[i - 1]]:
        count += 1
print(count)