Problem p03354 (python)

Fixed submission:
N, M = map(int, input().split())
# p = [int(i) for i in input().split()]
p = map(int,input().split())
# graph = [[]]*(N+1)
graph = [[] for _ in range(N+1)]
group = [0]*(N+1)
ans = 0
for _ in range(M):
    x, y = map(int, input().split())
    graph[x].append(y)
    graph[y].append(x)
for i, num in enumerate(p, 1):
    space = [i]
    for point in space:
        if group[point]:
            continue
        group[point] = i
        space.extend(graph[point])
    ans += (group[num] == group[i])
print(ans)

Buggy submission (verdict: Runtime Error):
def main():
    n, m = map(int, input().split())
    p = [0] + list(map(int, input().split()))
    cnt = 0
    global parent
    parent = list(range(n+1))
    for _ in range(m):
        x, y = map(int, input().split())
        unite(x, y)
    for i in range(1, n+1):
        if root(i) == root(p[i]):
        cnt += 1
    print(cnt)

def root(x):
    if parent[x] == x:
        return x
    parent[x] = root(parent[x])
    return parent[x]

def unite(x, y):
    root_x = root(x)
    if root_x != root(y):
        parent[root_x] = y

if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = [int(i) for i in input().split()]
graph = [[] for _ in range(n+1)]
group = [0]*n+1
ans = 0
for _ in range(m):
    x, y = map(int, input().split())
    graph[x].append(y)
    graph[y].append(x)
for i, num in enumerate(p,1):
    space = [i]
    for point in space:
        if group[point]:
            continue
        group[point] = i
        space.extend(graph[point])
    ans += group[num]-group[i]
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = [int(i) for i in input().split()]
graph = [[] for _ in range(n)]
group = [-1]*n
ans = 0
for _ in range(m):
    x, y = map(int, input().split())
    graph[x-1].append(y)
    graph[y-1].append(x)
for i, num in enumerate(p):
    space = [i]
    for point in space:
        if group[point]+1:
            continue
        group[point] = i
        space.extend(graph[point])
    ans += (group[num] == group[i])
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = [int(i) for i in input().split()]
graph = [[] for _ in range(n)]
group = [0]*n
ans = 0
for _ in range(m):
    x, y = map(int, input().split())
    graph[x-1].append(y)
    graph[y-1].append(x)
for i, num in enumerate(p):
    space = [i]
    for point in space:
        if group[point]:
            continue
        group[point] = i+1
        space.extend(graph[point])
    ans += (group[num] == group[i])
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
p = [int(i) for i in input().split()]
graph = [[] for _ in range(n)]
group = [0]*n
ans = 0
for _ in range(m):
    x, y = map(int, input().split())
    graph[x].append(y)
    graph[y].append(x)
for i, num in enumerate(p):
    space = [i]
    for point in space:
        if group[point]:
            continue
        group[point] = i+1
        space.extend(graph[point])
    ans += (group[num] == group[i])
print(ans)

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = [int(i) for i in input().split()];a.insert(0,0)
p = [int(i) for i in range(n+1)]
def f(x):return p[x] if p[x]==x else f(p[x])
def u(x,y):p[f(x)]=f(y)
xy = [u(*[int(i) for i in input().split()]) for _ in range(m)]
out = [f(i+1)==f(a[i+1]) for i in range(n)]
print(sum(out))

Buggy submission (verdict: Runtime Error):
n, m = map(int, input().split())
a = [int(i) for i in input().split()];a.insert(0,0)
p = [int(i) for i in range(n+1)]
f = lambda x: p[x] if p[x]==x else f(p[x])
def u(x,y):p[f(x)]=f(y)
xy = [u(*[int(i) for i in input().split()]) for _ in range(m)]
out = [f(i+1)==f(a[i+1]) for i in range(n)]
print(sum(out))