Problem p02936 (python)

Fixed submission:
#!/usr/bin/env python3
import sys
from collections import deque


def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):

    edges = [[] for _ in range(N)]
    for ai, bi in zip(a,b):
        edges[ai-1].append(bi)
        edges[bi-1].append(ai)
    pls = [0]*N
    for pi, xi in zip(p, x):
        pls[pi-1]+=xi
    
    ans = [0]*N
    ans[0] = pls[0]
    q = deque([1])
    while q:
        temp = q.pop()
        for t in edges[temp-1]:
            edges[t-1].remove(temp)
            ans[t-1] = pls[t-1] + ans[temp-1]
        q.extend(edges[temp-1])

    print(*ans)

    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  # type: "List[int]"
    x = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000)

class Node:
    def __init__(self, num):
        self.num = num
        self.ct = 0
        self.temp_ct = 0
        self.edge = []
    
    def set_temp_ct(self, ct_):
        self.temp_ct += ct_
    
    def set_ct(self, ct):
        self.ct = self.temp_ct + ct
        return self.ct

    def set_edge(self, e):
        self.edge.append(e)

    def pop_edge(self, e):
        if e in self.edge:
            self.edge.remove(e)



def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    nodes = [Node(num+1) for num in range(N)]
    for qa, qb in zip(a,b):
        nodes[qa-1].set_edge(qb)
        nodes[qb-1].set_edge(qa)

    for qp, qx in zip(p, x):
        nodes[qp-1].set_temp_ct(qx)

    def d_node(num, pre_ct):
        pre_ct = nodes[num-1].set_ct(pre_ct)
        for ls in nodes[num-1].edge:
            nodes[ls-1].pop_edge(num)
            d_node(ls, pre_ct)
    
    d_node(1, 0)

    a = [nodes[x].ct for x in range(N)]
    print(*a)


    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  # type: "List[int]"
    x = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys


class Node:
    def __init__(self, num):
        self.num = num
        self.ct = 0
        self.temp_ct = 0
        self.edge = []
    
    def set_temp_ct(self, ct_):
        self.temp_ct += ct_
    
    def set_ct(self, ct):
        self.ct = self.temp_ct + ct
        return self.ct

    def set_edge(self, e):
        self.edge.append(e)

    def pop_edge(self, e):
        if e in self.edge:
            self.edge.remove(e)



def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    nodes = [Node(num+1) for num in range(N)]
    for qa, qb in zip(a,b):
        nodes[qa-1].set_edge(qb)
        nodes[qb-1].set_edge(qa)

    for qp, qx in zip(p, x):
        nodes[qp-1].set_temp_ct(qx)

    def d_node(num, pre_ct):
        pre_ct = nodes[num-1].set_ct(pre_ct)
        for ls in nodes[num-1].edge:
            nodes[ls-1].pop_edge(num)
            d_node(ls, pre_ct)
    
    d_node(1, 0)

    a = [nodes[x].ct for x in range(N)]
    print(*a)


    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  # type: "List[int]"
    x = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys


class Node:
    def __init__(self, num):
        self.num = num
        self.ct = 0
        self.temp_ct = 0
        self.edge = []

    def set_leaf(self, leaf):
        self.leaf = leaf
    
    def set_temp_ct(self, ct_):
        self.temp_ct += ct_
    
    def set_ct(self, ct):
        self.ct = self.temp_ct + ct
        return self.ct

    def set_edge(self, e):
            self.edge.append(e)

    def pop_edge(self, e):
            self.edge.remove(e)



def solve(N: int, Q: int, a: "List[int]", b: "List[int]", p: "List[int]", x: "List[int]"):
    nodes = [Node(num+1) for num in range(N)]
    for qa, qb in zip(a,b):
        nodes[qa-1].set_edge(qb)
        nodes[qb-1].set_edge(qa)

    for qp, qx in zip(p, x):
        nodes[qp-1].set_temp_ct(qx)

    def d_node(num, pre_ct):
        pre_ct = nodes[num-1].set_ct(pre_ct)
        for ls in nodes[num-1].edge:
            nodes[ls-1].pop_edge(num)
            d_node(ls, pre_ct)
    
    d_node(1, 0)

    a = [nodes[x].ct for x in range(N)]
    print(*a)


    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    a = [int()] * (N - 1)  # type: "List[int]"
    b = [int()] * (N - 1)  # type: "List[int]"
    for i in range(N - 1):
        a[i] = int(next(tokens))
        b[i] = int(next(tokens))
    p = [int()] * (Q)  # type: "List[int]"
    x = [int()] * (Q)  # type: "List[int]"
    for i in range(Q):
        p[i] = int(next(tokens))
        x[i] = int(next(tokens))
    solve(N, Q, a, b, p, x)

if __name__ == '__main__':
    main()