Problem:


Score : 400 points

Problem Statement

Given is a rooted tree with N vertices numbered 1 to N.
The root is Vertex 1, and the i-th edge (1 \leq i \leq N - 1) connects Vertex a_i and b_i.

Each of the vertices has a counter installed. Initially, the counters on all the vertices have the value 0.

Now, the following Q operations will be performed:

Operation j (1 \leq j \leq Q): Increment by x_j the counter on every vertex contained in the subtree rooted at Vertex p_j.

Find the value of the counter on each vertex after all operations.

Constraints

2 \leq N \leq 2 \times 10^5

1 \leq Q \leq 2 \times 10^5

1 \leq a_i < b_i \leq N

1 \leq p_j \leq N

1 \leq x_j \leq 10^4

The given graph is a tree.

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N Q
a_1 b_1
:
a_{N-1} b_{N-1}
p_1 x_1
:
p_Q x_Q

Output

Print the values of the counters on Vertex 1, 2, \ldots, N after all operations, in this order, with spaces in between.

Sample Input 1

4 3
1 2
2 3
2 4
2 10
1 100
3 1

Sample Output 1

100 110 111 110

The tree in this input is as follows:

Each operation changes the values of the counters on the vertices as follows:

Operation 1: Increment by 10 the counter on every vertex contained in the subtree rooted at Vertex 2, that is, Vertex 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 0, 10, 10, 10, respectively.

Operation 2: Increment by 100 the counter on every vertex contained in the subtree rooted at Vertex 1, that is, Vertex 1, 2, 3, 4. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 110, 110, respectively.

Operation 3: Increment by 1 the counter on every vertex contained in the subtree rooted at Vertex 3, that is, Vertex 3. The values of the counters on Vertex 1, 2, 3, 4 are now 100, 110, 111, 110, respectively.

Sample Input 2

6 2
1 2
1 3
2 4
3 6
2 5
1 10
1 10

Sample Output 2

20 20 20 20 20 20



Sample input:
4 3
1 2
2 3
2 4
2 10
1 100
3 1


Sample output:
100 110 111 110

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

n, q = map(int, input().split())

edges = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    edges[a-1].append(b-1)
    edges[b-1].append(a-1)

operations = [0] * n
for i in range(q):
    p, x = map(int, input().split())
    operations[p-1] += x

counter = [-1] * n


def calculate(p, i, v):
    total = operations[i] + v
    for c in edges[i]:
        if c != p:
            calculate(i, c, total)
    counter[i] = total


calculate(-1, 0, 0)
print(' '.join(list(map(str, counter))))

Submission status: Time Limit Exceeded

Solution code:
import sys
stdin=sys.stdin

ns=lambda:input().rstrip()
ni=lambda:int(input().rstrip())
nm=lambda:map(int,input().split())
nl=lambda:list(map(int,input().split()))

N,Q=nm()
AB=[nl() for i in range(N-1)]
PQ=[nl() for i in range(Q)]
root=[[] for i in range(N)]
N_val=[0]*N
ans_val=[0]*N
d=[0]*N

def dfs(x,d,s):
  for i in root[x]:
    if(d[i]==0):
      s+=N_val[i]
      ans_val[i]=s
      d[i]=1
      dfs(i,d,s)
      s-=N_val[i]
      

for i in range(N-1):
  root[AB[i][0]-1]+=[AB[i][1]-1]
  root[AB[i][1]-1]+=[AB[i][0]-1]
  
for i in range(Q):
  N_val[PQ[i][0]-1]+=PQ[i][1]

  
ans_val[0]=N_val[0]
d[0]=1
dfs(0,d,N_val[0])
print(*ans_val)

Submission status: Time Limit Exceeded

Solution code:
def dfs_rec(start,prev):
    ans[start] = cnt[start]
    if start!=0:
        ans[start] +=ans[prev]
    for node in tree[start]:
        if node != prev:
            dfs_rec(node,start)
    return
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

n,q = map(int,input().split())
tree = [[] for i in range(n)]
cnt = [0] *n
ans = [0] *n

for i in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
for i in range(q):
    p,x = map(int,input().split())
    cnt[p-1]+=x
dfs_rec(0,0)
print(*ans)

Submission status: Time Limit Exceeded

Solution code:
def dfs_rec(start,prev):
    ans[start] = cnt[start]
    if start!=0:
        ans[start] +=ans[prev]
    for node in tree[start]:
        if node != prev:
            dfs_rec(node,start)
    return
import sys
sys.setrecursionlimit(10**6)


n,q = map(int,input().split())
tree = [[] for i in range(n)]
cnt = [0] *n
ans = [0] *n

for i in range(n-1):
    a,b = map(int,input().split())
    tree[a-1].append(b-1)
    tree[b-1].append(a-1)
for i in range(q):
    p,x = map(int,input().split())
    cnt[p-1]+=x
dfs_rec(0,0)
print(*ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import OrderedDict

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = set()
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].add(v2)
        self.adjacency_dict[v2].add(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    def dfs_stack(self, start):
        stack = [start]
        visited = set()

        parent_vertex = 0
        while stack:
            current_vertex = stack.pop()
            visited.add(current_vertex)
            for v in self.adjacency_dict[current_vertex]:
                if v not in visited and v != parent_vertex:
                    self.counter_dict[v] += self.counter_dict[current_vertex]
                    stack.append(v)
            parent_vertex = current_vertex

    def dfs_rec(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs_rec(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs_stack(1)
    # tree.dfs_rec(1, 0) # TLE
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import OrderedDict

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = set()
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].add(v2)
        self.adjacency_dict[v2].add(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    def dfs_stack(self, start):
        stack = [start]
        visited = set()

        parent_vertex = 0
        while stack != []:
            current_vertex = stack.pop()
            if parent_vertex != 0:
                self.counter_dict[current_vertex] += self.counter_dict[parent_vertex]
            visited.add(current_vertex)
            for v in self.adjacency_dict[current_vertex]:
                if v not in visited and v not in stack and v != parent_vertex:
                    stack.append(v)
            parent_vertex = current_vertex

    def dfs_rec(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs_rec(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    # tree.dfs_stack(1)
    tree.dfs_rec(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import OrderedDict

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = set()
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].add(v2)
        self.adjacency_dict[v2].add(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    def dfs_stack(self, start):
        stack = [start]
        visited = set()

        parent_vertex = 0
        while stack != []:
            current_vertex = stack.pop()
            if parent_vertex != 0:
                self.counter_dict[current_vertex] += self.counter_dict[parent_vertex]
            visited.add(current_vertex)
            for v in self.adjacency_dict[current_vertex]:
                if v not in visited and v not in stack and v != parent_vertex:
                    stack.append(v)
            parent_vertex = current_vertex

    def dfs_rec(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs_stack(1)
    # tree.dfs_rec(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import OrderedDict

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    def dfs_stack(self, start):
        stack = [start]
        visited = []

        parent_vertex = 0
        while stack != []:
            current_vertex = stack.pop()
            if parent_vertex != 0:
                self.counter_dict[current_vertex] += self.counter_dict[parent_vertex]
            visited.append(current_vertex)
            for v in self.adjacency_dict[current_vertex]:
                if v not in visited and v not in stack and v != parent_vertex:
                    stack.append(v)
            parent_vertex = current_vertex

    def dfs_rec(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        # print(*self.counter_dict.values())
        print(' '.join(map(str, self.counter_dict.values())))


def main():
    n, q=map(int, input().split())
    tree=Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b=map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x=map(int, input().split())
        tree.add_count(p, x)
    tree.dfs_stack(1)
    # tree.dfs_rec(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import OrderedDict

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count
    
    def dfs_stack(self, start):
        stack = [start]
        visited = []

        parent_vertex = 0
        while stack != []:
            current_vertex = stack.pop()
            if parent_vertex != 0:
                self.counter_dict[current_vertex] += self.counter_dict[parent_vertex]
            visited.append(current_vertex)
            for v in self.adjacency_dict[current_vertex]:
                if v not in visited and v not in stack and v != parent_vertex:
                    stack.append(v)
            parent_vertex = current_vertex

    def dfs_rec(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs_stack(1)
    # tree.dfs_rec(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache
from collections import OrderedDict

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    @lru_cache(maxsize=1000)
    def dfs(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from functools import lru_cache
from collections import OrderedDict

input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    @lru_cache(maxsize=None)
    def dfs(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import OrderedDict
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = {}
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    def dfs(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import OrderedDict
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**6)


class Graph():
    def __init__(self):
        self.adjacency_dict = OrderedDict()
        self.counter_dict = OrderedDict()

    def add_vertex(self, v):
        self.adjacency_dict[v] = []
        self.counter_dict[v] = 0

    def add_edge(self, v1, v2):
        self.adjacency_dict[v1].append(v2)
        self.adjacency_dict[v2].append(v1)

    def add_count(self, v, count):
        self.counter_dict[v] += count

    def dfs(self, v, pv):
        for nv in self.adjacency_dict[v]:
            if nv != pv:
                self.counter_dict[nv] += self.counter_dict[v]
                self.dfs(nv, v)

    def print_counters(self):
        print(*self.counter_dict.values())


def main():
    n, q = map(int, input().split())
    tree = Graph()
    for v in range(1, n+1):
        tree.add_vertex(v)
    for _ in range(n-1):
        a, b = map(int, input().split())
        tree.add_edge(a, b)
    for _ in range(q):
        p, x = map(int, input().split())
        tree.add_count(p, x)
    tree.dfs(1, 0)
    tree.print_counters()


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#全体に同じものを足すときは累積和を考えたい

N,Q = map(int,input().split())
AB = [list(map(int,input().split())) for _ in range(N-1)]
PX = [list(map(int,input().split())) for _ in range(Q)]

graph = [[] for _ in range(N+1)]

for a,b in AB:
    graph[a].append(b)
    graph[b].append(a)
#どちらが親か分からないがとりあえずつながっている
#どちらが親かは後で判断

counter = [0 for _ in range(N+1)]
#counter[i]:i番目の頂点のカウンター
parent = [None for _ in range(N+1)]

for p,x in PX:
    counter[p] += x

def dfs(graph,counter,edge):
    stk = []
    stk.append(edge)
    while len(stk) > 0:
        p = stk.pop()
        for e in graph[p]:
            if parent[p] == e:
                continue
            else:
                parent[e] = p
                counter[e] += counter[p]
                stk.append(e)

dfs(graph,counter,1)

ans = " ".join(map(str,counter[1:]))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import queue
import sys
sys.setrecursionlimit(5*10**5)

N, Q = map(int, input().split())
ab=[]
for _ in range(N-1):
    ab.append(tuple(map(int, input().split())))
px=[]
for _ in range(Q):
    px.append(tuple(map(int, input().split())))

cnt=[0]*(N+1)
for el in px:
    cnt[el[0]]+=el[1]
#print(cnt)

todo=queue.Queue()
seen=[False]*(N+1)

lst=[[] for _ in range(N+1)]
for el in ab:
    lst[el[0]].append(el[1])
    lst[el[1]].append(el[0])
#print(lst)

def bfs(n):
    seen[n]=True
    for child in lst[n]:
        if seen[child]==False:
            cnt[child]+=cnt[n]
            todo.put(child)
    if not todo.empty():
        bfs(todo.get(0))
    else:
        return None

bfs(1)
#print(cnt)
print(' '.join(list(map(str, cnt[1:]))))

Submission status: Time Limit Exceeded

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)


def dfs(G, v, p, point):
    """G: graph, v: vertex, p: parent"""
    # Loop for each child
    for c in G[v]:
        if c == p:
            continue
        point[c] += point[v]
        dfs(G, c, v, point)


def main():
    N, Q = map(int, input().split())
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)

    point = [0] * N
    for i in range(Q):
        p, x = map(int, input().split())
        p -= 1
        point[p] += x

    dfs(G, 0, -1, point)
    print(" ".join(map(str, point)))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def dfs(G, v, p, point):
    """G: graph, v: vertex, p: parent"""
    # Loop for each child
    for c in G[v]:
        if c == p:
            continue
        point[c] += point[v]
        dfs(G, c, v, point)


def main():
    N, Q = map(int, input().split())
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)

    point = [0] * N
    for i in range(Q):
        p, x = map(int, input().split())
        p -= 1
        point[p] += x

    dfs(G, 0, -1, point)
    print(" ".join(map(str, point)))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def dfs(G, v, p, C):
    """G: graph, v: vertex, p: parent"""
    # Loop for each child
    for c in G[v]:
        if c == p:
            continue
        dfs(G, c, v, C)
        C[v].extend(C[c])


def main():
    N, Q = map(int, input().split())
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)

    # Give all children for each parent
    C = [[i] for i in range(N)]
    dfs(G, 0, -1, C)

    point = [0] * N
    for i in range(Q):
        p, x = map(int, input().split())
        p -= 1
        children = C[p]
        for c in children:
            point[c] += x

    print(" ".join(map(str, point)))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)


def give_p(G, v, p, P):
    P[v] = p
    # Loop for each child
    for c in G[v]:
        if c == p:
            continue
        give_p(G, c, v, P)


def dfs(G, v, p, point, x):
    """G: graph, v: vertex, p: parent"""
    point[v] += x
    # Loop for each child
    for c in G[v]:
        if c == p:
            continue
        dfs(G, c, v, point, x)


def main():
    N, Q = map(int, input().split())
    G = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int, input().split())
        a -= 1
        b -= 1
        G[a].append(b)
        G[b].append(a)

    # Give parent for each child
    P = [-1] * N
    give_p(G, 0, -1, P)

    point = [0] * N
    for i in range(Q):
        p, x = map(int, input().split())
        p -= 1
        dfs(G, p, P[p], point, x)

    print(" ".join(map(str, point)))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(1000000)
n, q = map(int, input().split())

nd = [[] for i in range(n)]
ans = [0] * n

for _ in range(n-1):
    a, b = map(int, input().split())
    nd[a-1].append(b-1)
    nd[b-1].append(a-1)

def f(pi, pp=-1):
    for j in nd[pi]:
        if j == pp:
            continue
        ans[j] += ans[pi]
        f(j, pi)

for _ in range(q):
    p, x = map(int, input().split())
    ans[p-1] += x

f(0, 0)
for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)


def dfs(graph,start,point,mark_dict):
    global tree_point
    mark_dict[start] = True
    tree_point[start] += point
    for target in graph[start]:
        if target not in mark_dict and target != start:
            dfs(graph,target,tree_point[start],mark_dict)


n, q = map(int, input().split())

tree = [[] for _ in range(n+1)]
tree_point = [0] * (n+1)

XX = [[int(x) for x in input().split()] for _ in range(n-1)]
YY = [[int(x) for x in input().split()] for _ in range(q)]

for a, b in XX:
    tree[a].append(b)
    tree[b].append(a)

for a, b in YY:
    tree_point[a] += b

dfs(tree,1,0,mark_dict={})

for i in range(1, n+1):
    print(tree_point[i], end=' ')

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**7)


def dfs(graph,start,point,mark_dict):
    global tree_point
    mark_dict[start] = True
    tree_point[start] += point
    for target in graph[start]:
        if target not in mark_dict:
            mark_dict[target] = True
            dfs(graph,target,tree_point[start],mark_dict)


n, q = map(int, input().split())

tree = [[] for _ in range(n+1)]
tree_point = [0] * (n+1)

XX = [[int(x) for x in input().split()] for _ in range(n-1)]
YY = [[int(x) for x in input().split()] for _ in range(q)]


for a, b in XX:
    tree[a].append(b)
    tree[b].append(a)

for a, b in YY:
    tree_point[a] += b

dfs(tree,1,0,mark_dict={})

for i in range(1, n+1):
    print(tree_point[i], end=' ')

Submission status: Time Limit Exceeded

Solution code:
def submit():
  n,q = map(int,input().split())
  y = [[] for _ in range(n)]
  c = [0]*n
  for _ in range(n-1):
    ai,bi = map(int,input().split())
    y[ai-1].append(bi-1)
    y[bi-1].append(ai-1)
  for _ in range(q):
    pi,xi = map(int,input().split())
    c[pi-1] += xi
  z = [[0,-1]]
  while z:
    j,b = z.pop(0)
    for yj in y[j]:
      if yj != b:
        z.append([yj,j])
        c[yj] += c[j]
  for i in range(n):
    print(c[i],end=" ")
submit()

Submission status: Time Limit Exceeded

Solution code:
def submit():
  n,q = map(int,input().split())
  y = [[] for _ in range(n)]
  c = [0]*n
  for _ in range(n-1):
    ai,bi = map(int,input().split())
    y[ai-1].append(bi-1)
    y[bi-1].append(ai-1)
  for _ in range(q):
    pi,xi = map(int,input().split())
    c[pi-1] += xi
  z = [(0,-1)]
  while z:
    j,b = z.pop(0)
    for yj in y[j]:
      if yj != b:
        z.append((yj,j))
        c[yj] += c[j]
  for i in range(n):
    print(c[i],end=" ")
submit()

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
y = [[] for _ in range(n)]
c = [0 for _ in range(n)]
for _ in range(n-1):
  ai,bi = map(int,input().split())
  y[ai-1].append(bi-1)
  y[bi-1].append(ai-1)
for _ in range(q):
  pi,xi = map(int,input().split())
  c[pi-1] += xi
z = [[0,-1]]
while z:
  j,b = z.pop(0)
  for yj in y[j]:
    if yj != b:
      z.append([yj,j])
      c[yj] += c[j]
for i in range(n):
  print(c[i],end=" ")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(200000)
n,q = map(int,input().split())
y = [[] for _ in range(n)]
c = [0 for _ in range(n)]
for _ in range(n-1):
  ai,bi = map(int,input().split())
  y[ai-1].append(bi-1)
  y[bi-1].append(ai-1)
for _ in range(q):
  pi,xi = map(int,input().split())
  c[pi-1] += xi
def f(j,b):
  for yj in y[j]:
    if yj != b:
      c[yj] += c[j]
      f(yj,j)
f(0,-1)
for i in range(n):
  print(c[i],end=" ")

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

N, Q = list(map(int, input().split()))
nodes = [[] for i in range(N)]
for i in range(N-1):
  r, e = list(map(int, input().split()))
  r, e = r - 1, e - 1
  nodes[r].append(e)
  nodes[e].append(r)
#print(nodes)

result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i == prev:
      continue
    result[i] += result[r]
    dfs(i, r)

dfs(0)
#ここのせい？
print(*result)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

N, Q = list(map(int, input().split()))
nodes = [[] for i in range(N)]
for i in range(N-1):
  r, e = list(map(int, input().split()))
  r, e = r - 1, e - 1
  nodes[r].append(e)
  nodes[e].append(r)
#print(nodes)

result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i == prev:
      continue
    result[i] += result[r]
    dfs(i, r)

dfs(0)
print(*result)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

N, Q = list(map(int, input().split()))
nodes = [[] for i in range(N)]
for i in range(N-1):
  r, e = list(map(int, input().split()))
  r, e = r - 1, e - 1
  nodes[r].append(e)
  nodes[e].append(r)
#print(nodes)

result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i == prev:
      continue
    result[i] += result[r]
    dfs(i, r)

dfs(0)
print(" ".join(map(str, result)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

N, Q = list(map(int, input().split()))
nodes = {i:[] for i in range(N)}
for i in range(N-1):
  r, e = list(map(int, input().split()))
  r, e = r - 1, e - 1
  nodes[r].append(e)
  nodes[e].append(r)
#print(nodes)

result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i != prev:
      result[i] += result[r]
      dfs(i, r)

dfs(0)
print(" ".join(map(str, result)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

N, Q = list(map(int, input().split()))
nodes = {i:[] for i in range(N)}
for i in range(N-1):
  r, e = list(map(int, input().split()))
  r, e = r - 1, e - 1
  nodes[r].append(e)
  nodes[e].append(r)
#print(nodes)

result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i == prev:
      continue
    result[i] += result[r]
    dfs(i, r)

dfs(0)
print(" ".join(map(str, result)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(10**6)

N, Q = list(map(int, input().split()))
nodes = {i:[] for i in range(N)}
for i in range(N-1):
  r, e = list(map(int, input().split()))
  r, e = r - 1, e - 1
  nodes[r].append(e)
  nodes[e].append(r)
#print(nodes)

result = [0] * N
for i in range(Q):
  r, s = list(map(int, input().split()))
  result[r-1] += s

def dfs(r, prev=-1):
  for i in nodes[r]:
    if i == prev:
      continue
    result[i] += result[r]
    dfs(i, r)

dfs(0)
print(" ".join(map(str, result)))

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(2**31-1)

N, Q = [int(i) for i in input().split()]
G = [[] for _ in range(N)]
counter = [0] * N
res = [0] * N
visited = [0] * N

for _ in range(N-1):
    a, b = [int(i)-1 for i in input().split()]
    G[a].append(b)
    G[b].append(a)

for _ in range(Q):
    p, x = [int(i) for i in input().split()]
    p -= 1
    counter[p] += x

def dfs(G, v, p):
    visited[v] = 1
    res[v] = res[p]+counter[v]
    for nv in G[v]:
        if visited[nv]: continue
        dfs(G, nv, v)

dfs(G, 0, 0)
print(*res)