Problem p02936 (python)

Fixed submission:
while True:
    try:
        N, Q = map(int, input().split())
        verts = [None] * N
        qs = [0] * N
        results = [0] * N
        for i in range(N - 1):
            a, b = map(int, input().split())
            a -= 1
            b -= 1
            if verts[a] is None:
                verts[a] = list()
            if verts[b] is None:
                verts[b] = list()
            verts[a].append(b)
            verts[b].append(a)
        for i in range(Q):
            q, x = map(int, input().split())
            q -= 1
            qs[q] += x
        rests = [(0, None)]
        while rests:
            x, parent = rests.pop()
            if parent is not None:
                results[x] += results[parent]
            results[x] += qs[x]
            for i in verts[x]:
                if i != parent:
                    rests.append((i, x))
        print(" ".join(map(str, results)))
    except EOFError:
        break

Buggy submission (verdict: Runtime Error):
while True:
    try:
        N, Q = map(int, input().split())
        verts = [None] * N
        results = [0] * N
        for i in range(N - 1):
            a, b = map(int, input().split())
            if a == 1:
                continue
            a -= 1
            if verts[a] is None:
                verts[a] = list()
            verts[a].append(b)
        for i in range(Q):
            q, x = map(int, input().split())
            q-= 1
            results[q] += x
            if q == 0:
                for i in range(1, N):
                    results[i] += x
            elif verts[q] is not None:
                rests = verts[q][:]
                while rests:
                    i = rests.pop(0)
                    results[i-1] += x
                    if verts[i-1] is not None:
                        rests.extend(verts[i])
        print(" ".join(map(str, results)))
    except EOFError:
        break

Buggy submission (verdict: Runtime Error):
while True:
    try:
        N, Q = map(int, input().split())
        verts = [None] * N
        results = [0] * N
        for i in range(N - 1):
            a, b = map(int, input().split())
            a -= 1
            b -= 1
            if verts[a] is None:
                verts[a] = list()
            verts[a].append(b)

        maxmap = [None] * N
        map_a = [None] * N
        map_b = [None] * N
        def trav(a, x):
            map_a[a] = x
            map_b[x] = a
            if verts[a] is None:
                return x
            for i in verts[a]:
                maxmap[i] = x = trav(i, x + 1)
            return x

        maxmap[0] = trav(0, 0)

        for i in range(Q):
            q, x = map(int, input().split())
            q-= 1
            if q == 0:
                for i in range(N):
                    results[i] += x
            elif verts[q] is not None:
                for i in range(map_a[q], maxmap[q] + 1):
                    results[i] += x
            else:
                results[q] += x
        print(" ".join(map(str, results)))
    except EOFError:
        break

Buggy submission (verdict: Runtime Error):
while True:
    try:
        N, Q = map(int, input().split())
        verts = [None] * N
        results = [0] * N
        for i in range(N - 1):
            a, b = map(int, input().split())
            a -= 1
            b -= 1
            if verts[a] is None:
                verts[a] = list()
            verts[a].append(b)

        maxmap = [None] * N
        map_b = [None] * N
        def trav(a, x):
            map_b[x] = a
            if verts[a] is None:
                return x
            for i in verts[a]:
                maxmap[i] = x = trav(i, x + 1)
            return x

        maxmap[0] = trav(0, 0)

        for i in range(Q):
            q, x = map(int, input().split())
            q-= 1
            if q == 0:
                for i in range(N):
                    results[i] += x
            elif verts[q] is not None:
                for i in range(map_b.index(q), maxmap[q] + 1):
                    results[i] += x
            else:
                results[q] += x
        print(" ".join(map(str, results)))
    except EOFError:
        break