Problem p02936 (python)

Fixed submission:
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
graph = [[] for _ in range(N+1)]
for ab in AB:
    graph[ab[0]].append(ab[1])
    graph[ab[1]].append(ab[0])
val = [0] * (N+1)
for px in PX:
    val[px[0]] += px[1]
parent = [0] * (N+1)
from collections import deque
Q  = deque([1])
while Q:
    q = Q.popleft()
    for c in graph[q]:
        if c == parent[q]:
            continue
        parent[c] = q
        Q.append(c)
        val[c] += val[q]
print(*val[1:])

Buggy submission (verdict: Runtime Error):
input = sys.stdin.readline
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
graph = [[] for _ in range(N+1)]
for ab in AB:
    graph[ab[0]].append(ab[1])
    graph[ab[1]].append(ab[0])
val = [0] * (N+1)
for px in PX:
    val[px[0]] += px[1]
parent = [0] * (N+1)
Q  = [1]
while Q:
    q = Q.pop()
    for c in graph[q]:
        if c == parent[q]:
            continue
        parent[c] = q
        Q.append(c)
        val[c] += val[q]
print(*val[1:])

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
graph = [[] for _ in range(N+1)]
for ab in AB:
    graph[ab[0]].append(ab[1])
    graph[ab[1]].append(ab[0])
val = [0] * (N+1)
for px in PX:
    val[px[0]] += px[1]
parent = [0] * (N+1)
Q  = [1]
while Q:
    q = Q.popleft()
    for c in graph[q]:
        if c == parent[q]:
            continue
        parent[c] = q
        Q.append(c)
        val[c] += val[q]
print(*val[1:])

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
value = dict()
for px in PX:
    value.setdefault(px[0],0)
    value[px[0]] += px[1]
tree = dict()
for ab in AB:
    tree.setdefault(ab[0],[])
    tree[ab[0]].append(ab[1])
from collections import deque
Q = deque([1])
while Q:
    q = Q.pop()
    tree.setdefault(q,[])
    for c in tree[q]:
        value.setdefault(c,0)
        value.setdefault(q,0)
        value[c] += value[q]
        Q.append(c)
ans = []
for n in range(N):
    ans.append(value[n+1])
print(*ans)

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
AB = [[] for _ in range(N-1)]
for n in range(N-1):
    AB[n] = list(map(int,input().split()))
PX = [[] for _ in range(Q)]
for q in range(Q):
    PX[q] = list(map(int,input().split()))
value = dict()
for px in PX:
    value.setdefault(px[0],0)
    value[px[0]] += px[1]
tree = dict()
for ab in AB:
    tree.setdefault(ab[0],[])
    tree[ab[0]].append(ab[1])
from collections import deque
Q = deque([1])
while Q:
    q = Q.pop()
    tree.setdefault(q,[])
    for c in tree[q]:
        value.setdefault(c,0)
        value[c] += value[q]
        Q.append(c)
ans = []
for n in range(N):
    ans.append(value[n+1])
print(*ans)

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N,Q = map(int,input().split())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
PX = []
for _ in range(Q):
    PX.append([int(i) for i in input().split()])
class Tree:
    def __init__(self,n):
        self.tree = defaultdict(list)
        self.score = [0]*n
    def add_edge(self,n1,n2):
        self.tree[n1].append(n2)
    def calc_score(self,n,s):
        self.score[n-1]+=s
        if self.tree[n] == []:
            pass
        else:
            for n in self.tree[n]:
                self.calc_score(n,s)
tree = Tree(N)
for ab in AB:
    tree.add_edge(ab[0],ab[1])
for px in PX:
    tree.calc_score(px[0],px[1])
print(*tree.score)

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
ab = []
a=[]
for n in range(N-1):
    ab.append([int(i) for i in input().split()])
px = []
for q in range(Q):
    px.append([int(i) for i in input().split()])
class Graph():
    def __init__(self):
        """ ノードのつながりを辞書型で表現する """
        self.adjacency_dict = {}
    
    def add_vertex(self, v):
        """ ノードを追加する """
        self.adjacency_dict[v] = []
    def add_edge(self, v1, v2):
        """ ノード同士をつなぐ。"""
        # 無向グラフの場合は双方向。もし有向グラフなら片側のみ。
        self.adjacency_dict[v1].append(v2)    
    def print_graph(self):
        print(self.adjacency_dict)
graph = Graph()
for n in range(N-1):
    if graph.adjacency_dict.get(ab[n][0]):
        pass
    else:
        graph.add_vertex(ab[n][0])
    graph.add_edge(ab[n][0],ab[n][1])
def search(graph,j,ret):
    if graph.get(j):
        for k in graph[j]:
            ret.append(k)
            search(graph,k,ret)
    return ret
values = [0]* N
for q in range(Q):
    add_list = search(graph.adjacency_dict,px[q][0],[px[q][0]])
    for add in add_list:
        values[add-1] += px[q][1]
print(' '.join(map(str, values)))

Buggy submission (verdict: Runtime Error):
from collections import defaultdict
N,Q = map(int,input().split())
AB = []
for _ in range(N-1):
    AB.append([int(i) for i in input().split()])
PX = []
for _ in range(Q):
    PX.append([int(i) for i in input().split()])
class Tree:
    def __init__(self,n):
        self.tree = defaultdict(list)
        self.score = [0]*n
    def add_edge(self,n1,n2):
        self.tree[n1].append(n2)
    def calc_score(self,n,s):
        self.score[n-1]+=s
        if self.tree[n] == []:
            pass
        else:
            for n in self.tree[n]:
                self.calc_score(n,s)
tree = Tree(N)
for ab in AB:
    tree.add_edge(ab[0],ab[1])
for px in PX:
    tree.calc_score(px[0],px[1])
print(*tree.score)

Buggy submission (verdict: Runtime Error):
N,Q = map(int,input().split())
ab = []
a=[]
for n in range(N-1):
    ab.append([int(i) for i in input().split()])
px = []
for q in range(Q):
    px.append([int(i) for i in input().split()])
class Graph():
    def __init__(self):
        """ ノードのつながりを辞書型で表現する """
        self.adjacency_dict = {}
    
    def add_vertex(self, v):
        """ ノードを追加する """
        self.adjacency_dict[v] = []
    def add_edge(self, v1, v2):
        """ ノード同士をつなぐ。"""
        # 無向グラフの場合は双方向。もし有向グラフなら片側のみ。
        self.adjacency_dict[v1].append(v2)    
    def print_graph(self):
        print(self.adjacency_dict)
graph = Graph()
for n in range(N-1):
    if graph.adjacency_dict.get(ab[n][0]):
        pass
    else:
        graph.add_vertex(ab[n][0])
    graph.add_edge(ab[n][0],ab[n][1])
def search(graph,j,ret):
    if graph.get(j):
        for k in graph[j]:
            ret.append(k)
            search(graph,k,ret)
    return ret
values = [0]* N
for q in range(Q):
    add_list = search(graph.adjacency_dict,px[q][0],[px[q][0]])
    for add in add_list:
        values[add-1] += px[q][1]
print(' '.join(map(str, values)))