Problem p03354 (python)

Fixed submission:


def get_p(ind):
    if parent[ind] != ind:
        parent[ind] = get_p(parent[ind])
    return parent[ind]



num, swap = map(int, input().split())
data = list(map(int, input().split()))

parent = [i for i in range(num + 1)]

for i in range(swap):
    a, b = map(int, input().split())
    a_parent = get_p(a)
    b_parent = get_p(b)
    if a_parent != b_parent:
        parent[max(a_parent, b_parent)] = min(a_parent, b_parent)

ans = 0

for i in range(num):
    now_int = i + 1
    ind =  data[i]
    if get_p(now_int) == get_p(ind):
        ans += 1
    elif ind == now_int:
        ans += 1

print(ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

def get_p(ind):
    global parent
    while parent[ind] != ind:
        ind = parent[ind]
    return ind

def get_p2(ind):
    global parent
    if parent[ind] == -1:
        return ind
    else:
        parent[ind] = get_p2(parent[ind])
        return parent[ind]

parent = []

def main():
    global parent

    num, swap = map(int, input().split())
    data = list(map(int, input().split()))

    parent = [-1 for i in range(num + 1)]

    for i in range(swap):
        a, b = map(int, input().split())
        if parent[a] == -1 and parent[b] == -1:
            parent[max(a, b)] = min(a, b)
        elif parent[a] != -1 and parent[b] != -1:
            a_parent = get_p2(a)
            b_parent = get_p2(b)
            parent[max(a_parent, b_parent)] = min(a_parent, b_parent)
        elif parent[a] == -1:
            parent[a] = get_p2(b)
        else:
            parent[b] = get_p2(a)

    ans = 0

    for i in range(num + 1):
        if parent[i] == -1:
            parent[i] = i


    for i in range(num):
        now_int = i + 1
        ind = data.index(i + 1)
        ind += 1
        if get_p(now_int) == get_p(ind):
            ans += 1
        elif ind == now_int:
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
from collections import defaultdict

def get_p(ind, parent):
    while parent[ind] != ind:
        ind = parent[ind]
    return ind

def get_p2(ind, parent):
    if parent[ind] == -1:
        return ind, parent
    else:
        parent[ind], parent = get_p2(parent[ind], parent)
        return parent[ind], parent

def main():
    num, swap = map(int, input().split())
    data = list(map(int, input().split()))

    parent = [-1 for i in range(num + 1)]

    for i in range(swap):
        a, b = map(int, input().split())
        if parent[a] == -1 and parent[b] == -1:
            parent[max(a, b)] = min(a, b)
        elif parent[a] != -1 and parent[b] != -1:
            a_parent, parent = get_p2(a, parent)
            b_parent, parent = get_p2(b, parent)
            parent[max(a_parent, b_parent)] = min(a_parent, b_parent)
        elif parent[a] == -1:
            parent[a], parent = get_p2(b, parent)
        else:
            parent[b], parent = get_p2(a, parent)





    ans = 0

    for i in range(num + 1):
        if parent[i] == -1:
            parent[i] = i


    for i in range(num):
        now_int = i + 1
        ind = data.index(i + 1)
        ind += 1
        if get_p(now_int, parent) == get_p(ind, parent):
            ans += 1
        elif ind == now_int:
            ans += 1

    print(ans)


if __name__ == '__main__':
    main()