Problem p03142 (python)

Fixed submission:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 全国統一プログラミング王決定戦予選：D - Restore the Tree
import sys
sys.setrecursionlimit(1000000)
back = []


def calc(e):
    global back
    # print 'e =', e, ', b =', back[e]

    if back[e][1] == -1:
        if len(back[e][0]) == 0:
            back[e][1] = 0
            # print 'ret =', 0
            return 0
        elif len(back[e][0]) == 1:
            l = calc(back[e][0][0]) + 1
            back[e][1] = l
            # print 'ret =', l
            return l
        maxl = calc(back[e][0][0])
        maxi = 0
        for i, s in enumerate(back[e][0][1:], 1):
            l = calc(s)
            if l >= maxl:
                maxl = l
                back[e][0][maxi] = -1
                maxi = i
            else:
                back[e][0][i] = -1
        back[e][1] = maxl + 1
        # print 'ret =', maxl + 1
        return maxl + 1
    else:
        # print 'ret =', back[e][1]
        return back[e][1]


def main():
    global back
    N, M = [int(i) for i in raw_input().strip().split(' ')]

    A = []
    for _ in range(N+M-1):
        A.append([int(i)-1 for i in raw_input().strip().split(' ')])
    # H.sort(reverse=False)

    back = [[[], -1] for _ in range(N)]

    for s, e in A:
        back[e][0].append(s)
    # print 'back =', back

    for i, b in enumerate(back):
        if b[1] == -1:
            # back[i][1] = calc(i)
            calc(i)

    # print 'back =', back
    for b in back:
        if b[0] == []:
            print 0
        else:
            for s in b[0]:
                # print 's =', s
                if s != -1:
                    print s+1


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python
# -*- coding: utf-8 -*-

# 全国統一プログラミング王決定戦予選：D - Restore the Tree
back = []


def calc(e):
    global back
    # print 'e =', e, ', b =', back[e]

    if back[e][1] == -1:
        if len(back[e][0]) == 0:
            back[e][1] = 0
            # print 'ret =', 0
            return 0
        elif len(back[e][0]) == 1:
            l = calc(back[e][0][0]) + 1
            back[e][1] = l
            # print 'ret =', l
            return l
        maxl = calc(back[e][0][0])
        maxi = 0
        for i, s in enumerate(back[e][0][1:], 1):
            l = calc(s)
            if l >= maxl:
                maxl = l
                back[e][0][maxi] = -1
                maxi = i
            else:
                back[e][0][i] = -1
        back[e][1] = maxl + 1
        # print 'ret =', maxl + 1
        return maxl + 1
    else:
        # print 'ret =', back[e][1]
        return back[e][1]


def main():
    global back
    N, M = [int(i) for i in raw_input().strip().split(' ')]

    A = []
    for _ in range(N+M-1):
        A.append([int(i)-1 for i in raw_input().strip().split(' ')])
    # H.sort(reverse=False)

    back = [[[], -1] for _ in range(N)]

    for s, e in A:
        back[e][0].append(s)
    # print 'back =', back

    for i, b in enumerate(back):
        if b[1] == -1:
            # back[i][1] = calc(i)
            calc(i)

    # print 'back =', back
    for b in back:
        if b[0] == []:
            print 0
        else:
            for s in b[0]:
                # print 's =', s
                if s != -1:
                    print s+1


if __name__ == '__main__':
    main()