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: Runtime Error

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

def readinput():
    n,q=map(int,input().split())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for i in range(n-1):
        a,b=map(int,input().split())
        nList[a].append(b)
        nList[b].append(a)
    px=[]
    for i in range(q):
        p,x=map(int,input().split())
        px.append((p,x))
    return n,q,nList,px

def setDepth(s,nList):
    global depth
    global color

    for next in nList[s]:
        if color[next]==WHITE:
            depth[next]=depth[s]+1
            color[next]=GRAY
            setDepth(next,nList)
    color[s]=BLACK

def addCounter(s,x,nList):
    global depth
    global counter

    for next in nList[s]:
        if depth[next]>depth[s]:
            counter[next]+=x
            addCounter(next,x,nList)
    

WHITE=0
GRAY=1
BLACK=2
color=[]
depth=[]
counter=[]
def main(n,q,nList,px):
    global depth
    global color
    global counter

    color=[WHITE]*(n+1)
    depth=[0]*(n+1)
    counter=[0]*(n+1)

    #print(nList)

    depth[1]=0
    color[1]=GRAY
    setDepth(1,nList)
    #print(depth)

    for p,x in px:
        counter[p]+=x
        addCounter(p,x,nList)

    return counter[1:]

if __name__=='__main__':
    n,q,nList,px=readinput()
    ans=main(n,q,nList,px)
    print(' '.join(map(str,ans)))

Submission status: Runtime Error

Solution code:
def readinput():
    n,q=map(int,input().split())
    nList=[]
    for _ in range(n+1):
        nList.append([])
    for i in range(n-1):
        a,b=map(int,input().split())
        nList[a].append(b)
        nList[b].append(a)
    px=[]
    for i in range(q):
        p,x=map(int,input().split())
        px.append((p,x))
    return n,q,nList,px

def setDepth(s,nList):
    global depth
    global color

    for next in nList[s]:
        if color[next]==WHITE:
            depth[next]=depth[s]+1
            color[next]=GRAY
            setDepth(next,nList)
    color[s]=BLACK

def addCounter(s,x,nList):
    global depth
    global counter

    for next in nList[s]:
        if depth[next]>depth[s]:
            counter[next]+=x
            addCounter(next,x,nList)
    

WHITE=0
GRAY=1
BLACK=2
color=[]
depth=[]
counter=[]
def main(n,q,nList,px):
    global depth
    global color
    global counter

    color=[WHITE]*(n+1)
    depth=[0]*(n+1)
    counter=[0]*(n+1)

    #print(nList)

    depth[1]=0
    color[1]=GRAY
    setDepth(1,nList)
    #print(depth)

    for p,x in px:
        counter[p]+=x
        addCounter(p,x,nList)

    return counter[1:]

if __name__=='__main__':
    n,q,nList,px=readinput()
    ans=main(n,q,nList,px)
    print(' '.join(map(str,ans)))

Submission status: Runtime Error

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

d=[[0] for _ in range(n+1)]
d1=[0 for _ in range(n+1)]

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

for i in range(q):
  p,x=map(int,input().split())
  d1[p]+=x

for i in range(1,n+1):
  d[i].remove(0)
  
vi=[0 for _ in range(n+1)]

def dfs(x,i):
  vi[x]=i+d1[x]
  for m in d[x]:
    if vi[m]==0:
      dfs(m,vi[x])    

dfs(1,0)


print(*vi[1:])

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())

d=[[0] for _ in range(n+1)]
d1=[0 for _ in range(n+1)]

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

for i in range(q):
  p,x=map(int,input().split())
  d1[p]+=x

for i in range(1,n+1):
  d[i].remove(0)
  
vi=[0 for _ in range(n+1)]

def dfs(x,i):
  vi[x]+=i+d1[x]
  for m in d[x]:
    if vi[m]==0:
      dfs(m,vi[x])    

dfs(1,0)


print(*vi[1:])

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, cnt, visited):
    visited[p] = True
    cnt_here = cnt[p]
    for c in tree.get(p,1):
        if visited[c]:
            continue
        cnt[c] += cnt_here
        dfs(tree, c, cnt, visited)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    tree = dict(tree)
    cnt = [0] * (n + 1)

    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x
    visited = [False] * (n + 1)
    dfs(tree, 1, cnt, visited)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, cnt, visited):
    visited[p] = True
    cnt_here = cnt[p]
    for c in tree[p]:
        if visited[c]:
            continue
        cnt[c] += cnt_here
        dfs(tree, c, cnt, visited)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)
    tree = dict(tree)
    cnt = [0] * (n + 1)

    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x
    visited = [False] * (n + 1)
    dfs(tree, 1, cnt, visited)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, cnt, visited):
    visited[p] = True
    cnt_here = cnt[p]
    for c in tree[p]:
        if visited[c]:
            continue
        cnt[c] += cnt_here
        dfs(tree, c, cnt, visited)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)

    cnt = [0] * (n + 1)

    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x
    visited = [False] * (n + 1)
    dfs(tree, 1, cnt, visited)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, cnt, visited):
    visited[p] = True
    cnt_here = cnt[p]
    for c in tree[p]:
        if visited[c]:
            continue
        cnt[c] += cnt_here
        dfs(tree, c, cnt, visited)
    return 0


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)

    cnt = [0] * (n + 1)

    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x
    visited = [False] * (n + 1)
    dfs(tree, 1, cnt, visited)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, cnt, visited):
    visited[p] = True
    for c in tree[p]:
        if visited[c]:
            continue
        cnt[c] += cnt[p]
        dfs(tree, c, cnt, visited)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)

    cnt = [0] * (n + 1)

    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x
    visited = [False] * (n + 1)
    dfs(tree, 1, cnt, visited)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

cnt = [0] * 220000
visited = [False] * 220000


def dfs(tree, p):
    global cnt, visited
    visited[p] = True
    for c in tree[p]:
        if visited[c]:
            continue
        cnt[c] += cnt[p]
        dfs(tree, c)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)

    # cnt = [0] * (n + 1)
    global cnt, visited
    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x

    dfs(tree, 1)
    print(*cnt[1 : n + 1])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

cnt = [0] * 3 * 10 ** 5
visited = [False] * 3 * 10 ** 5


def dfs(tree, p):
    global cnt, visited
    visited[p] = True
    for c in tree[p]:
        if visited[c]:
            continue
        cnt[c] += cnt[p]
        dfs(tree, c)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)
        tree[b].append(a)

    # cnt = [0] * (n + 1)
    global cnt
    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x

    dfs(tree, 1)
    print(*cnt[1 : n + 1])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict

cnt = [0] * 3 * 10 ** 5


def dfs(tree, p):
    global cnt
    for c in tree[p]:
        cnt[c] += cnt[p]
        dfs(tree, c)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)

    # cnt = [0] * (n + 1)
    global cnt
    for _ in range(q):
        p, x = map(int, input().split())
        cnt[p] += x

    dfs(tree, 1)
    print(*cnt[1 : n + 1])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import sys
from collections import defaultdict


def dfs(tree, p, cnt):
    for c in tree[p]:
        cnt[c] += cnt[p]
        dfs(tree, c, cnt)


def main():
    input = sys.stdin.buffer.readline
    n, q = map(int, input().split())
    tree = defaultdict(list)
    for _ in range(n - 1):
        a, b = map(int, input().split())
        tree[a].append(b)

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

    dfs(tree, 1, cnt)
    print(*cnt[1:])


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())

ARR = []

for i in range(N-1):
    ARR.append(list(map(int, input().split())))

BRR = []

for i in range(Q):
    BRR.append(list(map(int, input().split())))


def prepare(n, q, arr, brr):
    nodePoints = [0 for i in range(n)]
    for i in range(q):
        nodeIndex = brr[i][0] - 1
        nodePoints[nodeIndex] += brr[i][1]

    nodes = [[] for i in range(n)]

    for i in range(n - 1):
        startNode = arr[i][0] - 1
        endNode = arr[i][1] - 1

        nodes[startNode].append(endNode)
        nodes[endNode].append(startNode)

    return nodes, nodePoints


nodes, nodePoints = prepare(N, Q, ARR, BRR)


def dfs(currentNode, nodes, parentNode=-1, parentPoint=0):
    nodePoints[currentNode] = nodePoints[currentNode] + parentPoint
    childNodes = nodes[currentNode]
    for childNode in childNodes:
        if childNode == parentNode:
            continue
        dfs(childNode, nodes, currentNode, nodePoints[currentNode])


dfs(0, nodes)

print(" ".join([str(nodePoints[i]) for i in range(N)]))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)

n, q = map(int, input().split())
Tree = {i:[] for i in range(1, n+1)}
Counter = {i:0 for i in range(1, n+1)}

for _ in range(n-1):
  a,b = map(int, input().split())
  Tree[a].append(b)
  
def dfs(i, c, t):
  if t[i]:
    for j in t[i]:
      c[j] += c[i]
      dfs(j, c, t)

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

dfs(1, Counter, Tree)
print(*Counter.values())

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10000)

n, q = map(int, input().split())
Tree = {i:[] for i in range(1, n+1)}
Counter = {i:0 for i in range(1, n+1)}

for _ in range(n-1):
  a,b = map(int, input().split())
  Tree[a].append(b)
  
def dfs(n, c, t):
  if t[n]:
    for m in t[n]:
      c[m] += c[n]
      dfs(m, c, t)

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

dfs(1, Counter, Tree)
print(*Counter.values())

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
Tree = {i:[] for i in range(1, n+1)}
Counter = {i:0 for i in range(1, n+1)}

for _ in range(n-1):
  a,b = map(int, input().split())
  Tree[a].append(b)
  
def dfs(n, c, t):
  if t[n]:
    for m in t[n]:
      c[m] += c[n]
      dfs(m, c, t)

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

dfs(1, Counter, Tree)
print(*Counter.values())

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
Tree = {i:[] for i in range(1, n+1)}
Counter = {i:0 for i in range(1, n+1)}

for _ in range(n-1):
  a,b = map(int, input().split())
  Tree[a].append(b)
  
def dfs(n, x, c, t):
  c[n] += x
  
  if t[n]:
    for m in t[n]:
      dfs(m, x, c, t)

for _ in range(q):
  p,x = map(int, input().split())
  dfs(p, x, Counter, Tree)
  
print(*Counter.values())

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

def val_add(i,con,val):
    if con[i] :
        for j in con[i]:
            val[j]+=val[i]
            val_add(j,con,val)

def tree(i,con):
    if not con[i]: pass
    else :
        for j in con[i]:
            con[j].remove(i)
            tree(j,con)

def main():
    n,q=map(int,input().split())
    con=[[] for _ in range(n)]
    val=[0]*n
    for _ in range(n-1):
        a,b=map(int,input().split())
        con[a-1].append(b-1)
        con[b-1].append(a-1)

    tree(0,con)

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

    val_add(0,con,val)


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

main()

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(100000)

def search(i,x,con,val):
    if con[i] :
        for j in con[i]:
            search(j,x,con,val)
    val[i]+=x

def tree(i,con):
    if not con[i]: pass
    else :
        for j in con[i]:
            con[j].remove(i)
            tree(j,con)

def main():
    n,q=map(int,input().split())
    con=[[] for _ in range(n)]
    val=[0]*n
    for _ in range(n-1):
        a,b=map(int,input().split())
        con[a-1].append(b-1)
        con[b-1].append(a-1)

    tree(0,con)

    for _ in range(q):
        p,x=map(int,input().split())
        search(p-1,x,con,val)

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

main()

Submission status: Runtime Error

Solution code:
def search(i,x,con,val):
    if con[i] :
        for j in con[i]:
            search(j,x,con,val)
    val[i]+=x

def tree(i,con):
    if not con[i]: pass
    else :
        for j in con[i]:
            con[j].remove(i)
            tree(j,con)


n,q=map(int,input().split())
con=[[] for _ in range(n)]
val=[0]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    con[a-1].append(b-1)
    con[b-1].append(a-1)

tree(0,con)

for _ in range(q):
    p,x=map(int,input().split())
    search(p-1,x,con,val)

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

Submission status: Runtime Error

Solution code:
def search(i,x,con,val):
    if con[i] :
        for j in con[i]:
            search(j,x,con,val)
    val[i]+=x


n,q=map(int,input().split())
con=[[] for _ in range(n)]
val=[0]*n
for _ in range(n-1):
    a,b=map(int,input().split())
    con[a-1].append(b-1)

for _ in range(q):
    p,x=map(int,input().split())
    search(p-1,x,con,val)

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict
from collections import deque
from sys import stdin
input = stdin.readline


def main():
  # input
  N, Q = list(map(int, input().split()))

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

  PX = defaultdict(int)
  for _ in range(Q):
    p, x = map(int, input().split())
    PX[p] += x

  # solve
  ans = [0]*(N+1)
  visited = set()
  # temp = PX[1]
  # S = [1] # Stack
  # S = deque([1])

  # while len(S):
  #   now = S[-1]
  #   visited.add(now)

  #   for next_ in G[now]:
  #     if next_ not in visited:
  #       S.append(next_)
  #       temp += PX[next_]
  #       break
  #   else:
  #     S.pop()
  #     ans[now] = temp
  #     temp -= PX[now]

  def dfs(now, sum_):
    visited.add(now)
    for next_ in G[now]:
      if next_ not in visited:
        sum_ += PX[next_]
        sum_ = dfs(next_, sum_)

    ans[now] = sum_
    sum_ -= PX[now]
    return sum_

  dfs(1, PX[1])

  print(*ans[1:], sep=' ')


if(__name__ == '__main__'):
  main()

Submission status: Runtime Error

Solution code:
#初期データ、構造
N,Q = (int(x) for x in input().split())
# 隣接リスト表現
connection ={i:set() for i in range(1,N+1)}
for i in range(N-1):
    a,b = (int(x) for x in input().split())
    connection[a].add(b)
    connection[b].add(a)
point ={i:0 for i in range(1,N+1)}
for i in range(Q):
    p,x = (int(x) for x in input().split())
    point[p] +=x

def DFS(now,prev = -1):
    for next in connection[now]:
        # 親へのエッジはスキップ
        if next == prev:
            continue
        point[next] += point[now] 
        DFS(next,now)

DFS(1)
print(*point.values())

Submission status: Runtime Error

Solution code:
from sys import setrecursionlimit

setrecursionlimit(10 ** 6)

N, Q = map(int, input().split())

graph   = [ [] for _ in range(N) ]
queries = { p: 0 for p in range(N) }

for _ in range(N - 1):
    a, b = map(lambda x: int(x) - 1, input().split())

    graph[a].append(b)
    graph[b].append(a)

for _ in range(Q):
    p, x = map(int, input().split())

    queries[p - 1] += x

answer = [ 0 ] * N

def dfs(p, p_before, acc):
    acc += queries[p]

    answer[p] = acc

    for child in graph[p]:
        if child == p_before:
            continue

        dfs(child, acc)

dfs(0, -1, 0)

print(*answer)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())

edges = [[] for i 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)

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

def dfs(now,prev):
    for next in edges[now]:
        if next == prev:
            continue
        counter[next] += counter[now]
        dfs(next,now)

dfs(0,-1)
print(*counter)

Submission status: Runtime Error

Solution code:
n,q = map(int,input().split())

edges = [[] for i 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)

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

def dfs(now,prev):
    for next in edges[now]:
        if next == prev:
            continue
        counter[next] += counter[now]
        dfs(next,now)

dfs(0,-1)
print(*counter)

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
edges = [[] for i in range(n)]
counter = [0] * 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)
for i in range(q):
    p, x = map(int, input().split())
    counter[p - 1] += x
def dfs(now, prev):
    for next in edges[now]:
        if next == prev:
            continue
        counter[next] += counter[now]
        dfs(next, now)
dfs(0, -1)
print(*counter)

Submission status: Runtime Error

Solution code:
n,q = map(int, input().split())
graph1 = [[] for _ in range(n+1)]
graph2 = [[] for _ in range(n+1)]
ans=[0]*(n+1)
reached=[0]*(n+1)
def main():
  for i in range(n-1):
    a,b = map(int, input().split())
    graph1[a].append(b)
    graph2[b].append(a)

  for i in range(q):
    p,x = map(int, input().split())
    ans[p] += x
  dfs(1) 
  print(' '.join([str(i) for i in ans[1:]]))

def dfs(i):
  if reached[i] == 1:
    return
  reached[i] = 1
  for g in graph1[i], graph2[i]:
    for j in g:
      if reached[j] == 1:
        continue
      ans[j] += ans[i]
      dfs(j)

if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
n,q = map(int, input().split())
graph = [[] for _ in range(n+1)]
ans=[0]*(n+1)
def main():
  for i in range(n-1):
    a,b = map(int, input().split())
    graph[a].append(b)

  for i in range(q):
    p,x = map(int, input().split())
    ans[p] += x
  dfs(1) 
  print(' '.join([str(i) for i in ans[1:]]))

def dfs(i):
  for j in graph[i]:
    ans[j] += ans[i]
    dfs(j)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc138/tasks/abc138_d

import sys
input = sys.stdin.readline
import marshal


class Graph:
    def __init__(self, n, dictated=False, decrement=True, destroy=False, edges=[]):
        self.n = n
        self.dictated = dictated
        self.decrement = decrement
        self.destroy = destroy
        self.edges = [[] for _ in range(self.n)]
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y in edges:
            self.add_edge(x,y)

    def add_edge(self, x, y):
        if self.decrement:
            x -= 1
            y -= 1
        self.edges[x].append(y)
        if self.dictated == False:
            self.edges[y].append(x)

    def add_adjacent_list(self, i, adjacent_list):
        if self.decrement:
            self.edges[i] = list(map(lambda x: x - 1, adjacent_list))
        else:
            self.edges[i] = list(adjacent_list)

    def dfs2(self, info, start, goal=-1, time=0, save=False):
        """
        :param info: 各頂点の付加情報
        :param start: スタート地点
        :param goal: ゴール地点
        :param save: True = 前回の探索結果を保持する
        :return: ゴール地点までの距離。存在しなければ -1
        """

        if self.decrement:
            start -= 1
            goal -= 1
        if not save:
            self.parent = [-1] * self.n
        if self.destroy:
            edge2 = self.edge
        else:
            edge2 = marshal.loads(marshal.dumps(self.edge))

        p, t = start, time
        self.parent[p] = -2
        while True:
            if edge2[p]:
                q = edge2[p].pop()
                if q == self.parent[p] and not self.dictated:
                    """ 逆流した時の処理 """
                    """"""""""""""""""""
                    continue
                if self.parent[q] != -1:
                    """ サイクルで同一点を訪れた時の処理 """
                    """"""""""""""""""""
                    continue
                if q == goal:
                    """ ゴール時の処理"""
                    # return t + 1
                    """"""""""""""""""""
                    continue
                """ p から q への引継ぎ"""
                info[q] += info[p]
                """"""""""""""""""""
                self.parent[q] = p
                p, t = q, t + 1
            else:
                """ 探索完了時の処理 """
                """"""""""""""""""""
                if p == start and t == time:
                    break
                p, t = self.parent[p], t-1
                """ 二度目に訪問時の処理 """

                """"""""""""""""""""
        return info


######################################################################################################

N, Q = map(int, input().split())  # N:頂点数, Q: クエリの数
M = N-1

graph = Graph(N, dictated=False, destroy=True)

for _ in range(M):
    x, y = map(int, input().split())
    graph.add_edge(x,y)

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

print(*graph.dfs2(info, start=1))

Submission status: Runtime Error

Solution code:
# https://atcoder.jp/contests/abc138/tasks/abc138_d

import sys
input = sys.stdin.readline
import marshal


class Graph:
    def __init__(self, n, dictated=False, decrement=True, destroy=False, edge=[]):
        self.n = n
        self.dictated = dictated
        self.decrement = decrement
        self.destroy = destroy
        self.edge = [[] for _ in range(self.n)]
        self.parent = [-1]*self.n
        self.info = [-1]*self.n
        for x, y in edge:
            self.add_edge(x,y)

    def add_edge(self, x, y):
        if self.decrement:
            x -= 1
            y -= 1
        self.edge[x].add(y)
        if self.dictated == False:
            self.edge[y].append(x)

    def add_adjacent_list(self, i, adjacent_list):
        if self.decrement:
            self.edge[i] = list(map(lambda x:x-1, adjacent_list))
        else:
            self.edge[i] = list(adjacent_list)

    def dfs2(self, info, start, goal=-1, time=0, save=False):
        """
        :param info: 各頂点の付加情報
        :param start: スタート地点
        :param goal: ゴール地点
        :param save: True = 前回の探索結果を保持する
        :return: ゴール地点までの距離。存在しなければ -1
        """

        if self.decrement:
            start -= 1
            goal -= 1
        if not save:
            self.parent = [-1] * self.n
        if self.destroy:
            edge2 = self.edge
        else:
            edge2 = marshal.loads(marshal.dumps(self.edge))

        p, t = start, time
        self.parent[p] = -2
        while True:
            if edge2[p]:
                q = edge2[p].pop()
                if q == self.parent[p] and not self.dictated:
                    """ 逆流した時の処理 """
                    """"""""""""""""""""
                    continue
                if self.parent[q] != -1:
                    """ サイクルで同一点を訪れた時の処理 """
                    """"""""""""""""""""
                    continue
                if q == goal:
                    """ ゴール時の処理"""
                    # return t + 1
                    """"""""""""""""""""
                    continue
                """ p から q への引継ぎ"""
                info[q] += info[p]
                """"""""""""""""""""
                self.parent[q] = p
                p, t = q, t + 1
            else:
                """ 探索完了時の処理 """
                """"""""""""""""""""
                if p == start and t == time:
                    break
                p, t = self.parent[p], t-1
                """ 二度目に訪問時の処理 """

                """"""""""""""""""""
        return info


######################################################################################################

N, Q = map(int, input().split())  # N:頂点数, Q: クエリの数
M = N-1

graph = Graph(N, dictated=False, destroy=True)

for _ in range(M):
    x, y = map(int, input().split())
    graph.add_edge(x,y)

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

print(*graph.dfs2(info, start=1))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10*10)
def MI(): return map(int, input().split())
from collections import deque
N,Q=MI()
Edge=[[] for _ in range(N)]
for i in range(N-1):
  a,b=MI()
  Edge[a-1].append(b-1)
  Edge[b-1].append(a-1)
Point=[0]*N
for i in range(Q):
  p,x=MI()
  Point[p-1]+=x
def dfs(now,pre=-1):
  for nxt in Edge[now]:
    if nxt==pre:
      continue
    Point[nxt]+=Point[now]
    dfs(nxt,now)
dfs(0)
print(*Point)

Submission status: Runtime Error

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

def MI(): return map(int, input().split())
from collections import deque
N,Q=MI()
N1=N
#Edge1=[[] for _ in range(N)]
#print(Edge1)
Edge=[[] for _ in range(N1)]
#print(Edge)
for i in range(N-1):
  a,b=MI()
  Edge[a-1].append(b-1)
  Edge[b-1].append(a-1)
Point=[0]*N
for i in range(Q):
  p,x=MI()
  Point[p-1]+=x
def dfs(now,pre=-1):
  for nxt in Edge[now]:
    if nxt==pre:
      continue
    Point[nxt]+=Point[now]
    dfs(nxt,now)
dfs(0)
print(*Point)

Submission status: Runtime Error

Solution code:
n,q=map(int,input().split())
edge=[[] for i in range(n)]
a=[0]*n
b=[0]*n
for i in range(n-1):
    x,y=map(int,input().split())
    edge[x-1].append(y-1)
    edge[y-1].append(x-1)
for i in range(q):
    p,x=map(int,input().split())
    a[p-1]+=x
def dfs(node,tmp):
    a[node]+=tmp
    b[node]=1
    for i in edge[node]:
        if b[i]==0:
            dfs(i,a[node])
dfs(0,0)
print(*a)

Submission status: Runtime Error

Solution code:
import sys

def main():

    st = sys.stdin.readline()
    (n,q) = tuple(map(int, st.strip().split(' ')))

    
    t = {}

    for i in range(0, n-1):
        st = sys.stdin.readline()
        (ai,bi) = tuple(map(int, st.strip().split(' ')))

        if ai > bi:
                ai,bi = bi,ai
        t[bi] = ai

    s = [0] * (n + 1)
    for i in range(0, q):
        st = sys.stdin.readline()
        (i,x) = tuple(map(int, st.strip().split(' ')))
        operate(t, n, s, i, x)

    for i in range(2, n+1):
        s[i] += s[t[i]]


    print((' ').join(map(str, s[1:])))

def operate(t, n, s, i, x):
    s[i] += x

#    for j in t[i]:
#            operate(t, n, s, j, x)


main()

Submission status: Runtime Error

Solution code:
import sys

def main():

    st = sys.stdin.readline()
    (n,q) = tuple(map(int, st.strip().split(' ')))

    
    t = []
    for i in range(0, n+1):
        t.append([])

    for i in range(0, n-1):
        st = sys.stdin.readline()
        (ai,bi) = tuple(map(int, st.strip().split(' ')))

        if ai > bi:
                ai,bi = bi,ai
        t[ai].append(bi)

    s = [0] * (n + 1)
    for i in range(0, q):
        st = sys.stdin.readline()
        (i,x) = tuple(map(int, st.strip().split(' ')))
        operate(t, n, s, i, x)

    print((' ').join(map(str, s[1:])))

def operate(t, n, s, i, x):
    s[i] += x

    for j in t[i]:
            operate(t, n, s, j, x)


main()