Problem p03142 (python)

Fixed submission:
# coding: utf-8

import sys
import math
import fractions
import heapq
import collections
import re
import array
import bisect
sys.setrecursionlimit(1000000)

from collections import Counter, defaultdict


def array2d(dim1, dim2, init=None):
    return [[init for _ in range(dim2)] for _ in range(dim1)]

def argsort(l, reverse=False):
    return sorted(range(len(l)), key=lambda i: l[i], reverse=reverse)

def argmin(l):
    return l.index(min(l))


II = lambda: int(input())
MI = lambda: map(int, input().split())



def main():
    N, M = MI()
    in_edges = [set() for _ in range(N)]
    out_edges = [set() for _ in range(N)]
    num_in_edges = [0] * N
    for _ in range(N - 1 + M):
        a, b = MI()
        a -= 1
        b -= 1
        num_in_edges[b] += 1
        in_edges[b].add(a)
        out_edges[a].add(b)
    Root = argmin(num_in_edges)
    parent = [0] * N
    parent[Root] = -1

    q = [Root]

    while q:
        v = q.pop()
        for child in out_edges[v]:
            in_edges[child].remove(v)
            num_in_edges[child] -= 1
            if num_in_edges[child] == 0:
                parent[child] = v
                q.append(child)

    for p in parent:
        print(p + 1)


if __name__ == "__main__":
    main()

Buggy submission (verdict: Runtime Error):
# coding: utf-8

import math
import fractions
import heapq
import collections
import re
import array
import bisect

from collections import Counter, defaultdict


def array2d(dim1, dim2, init=None):
    return [[init for _ in range(dim2)] for _ in range(dim1)]

II = lambda: int(input())
MI = lambda: map(int, input().split())



def main():
    N, M = MI()
    node_parents = [set() for _ in range(N)]
    node_children = [set() for _ in range(N)]
    for i in range(N - 1 + M):
        a, b = MI()
        node_parents[b-1].add(a-1)
        node_children[a-1].add(b-1)

    for i in range(N):
        if len(node_parents[i]) == 0:
            Root = i
            break

    leaves = set()
    visited = set()
    def find_leaf(v):
        if v in visited: return
        visited.add(v)
        if len(node_children[v]) == 0:
            leaves.add(v)
        for c in node_children[v]:
            find_leaf(c)
    find_leaf(Root)


    def find_root_path(v):
        if v == Root:
            return [[v]]
        path = []
        for p in node_parents[v]:
            ppath = find_root_path(p)
            if ppath is not None:
                path.extend(ppath)
        if len(path) == 0:
            return None
        for p in path:
            p.append(v)
        return path

    def find_longest_path(paths):
        ia, l = 0, 0
        for i, p in enumerate(paths):
            if len(p) > l:
                l = len(p)
                ia = i
        return ia

    for l in leaves:
        paths = find_root_path(l)
        longest_path_i = find_longest_path(paths)
        lp = paths.pop(longest_path_i)
        lp_edges = [(lp[i], lp[i + 1]) for i in range(len(lp) - 1)]
        for path in paths:
            for i in range(len(path) - 1):
                edge = (path[i], path[i + 1])
                if edge not in lp_edges and edge[0] in node_parents[edge[1]]:
                    node_parents[edge[1]].remove(edge[0])

    for i in range(N):
        if i == Root:
            print(0)
        else:
            print(node_parents[i].pop() + 1)


if __name__ == "__main__":
    main()