qyliang/CodeNanoFix huggingface dataset:
Buggy code:
import numpy as np
import sys
from functools import lru_cache

def main():
    n, q = map(int, input().split())
    ab = [[],[]]
    
    for i in range(n-1):
        na,nb = map(int, input().split())
        ab[0].append(na)
        ab[1].append(nb)

    px = [[],[]]
    for i in range(q):
        np,nx = map(int, input().split())        
        px[0].append(np)
        px[1].append(nx)
    
    # print(n,q,ab,px)
    count = [0 for i in range(n)]

    oya_list = [0 for i in range(n)]
    for i in range(len(ab[1])):
        oya_list[ab[1][i]-1] = ab[0][i]
    
    for i in range(len(px[1])):
        count[px[0][i]-1] = px[1][i]

    
    for i in range(1,n):

        count[i] += count[oya_list[i]-1]
    
    print(' '.join(map(str,count)))


if __name__ == '__main__':
    main()

Fixed code:
import numpy as np
import sys
from functools import lru_cache

def main():
    n, q = map(int, input().split())
    ab = [[],[]]
    
    for i in range(n-1):
        na,nb = map(int, input().split())
        ab[0].append(na)
        ab[1].append(nb)

    px = [[],[]]
    for i in range(q):
        np,nx = map(int, input().split())        
        px[0].append(np)
        px[1].append(nx)
    
    # print(n,q,ab,px)
    count = [0 for i in range(n)]

    oya_list = [0 for i in range(n)]
    for i in range(len(ab[1])):
        oya_list[ab[1][i]-1] = ab[0][i]
    
    for i in range(len(px[1])):
        count[px[0][i]-1] += px[1][i]
        
    for i in range(1,n):

        count[i] += count[oya_list[i]-1]
    
    print(' '.join(map(str,count)))


if __name__ == '__main__':
    main()
Buggy code:
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n-1)]
pq=[list(map(int,input().split())) for _ in range(m)]
ab2=sorted(ab, key=lambda x:x[0])
pq2=sorted(pq, key=lambda x:x[0])

res=[0 for _ in range(n)]
for p,q in pq2:
	res[p-1]+=q
for a,b in ab2:
	res[b-1]+=res[a-1]
ans=[]
for i in range(n):
	ans.append(str(res[i]))
#print(" ".join(ans))

Fixed code:
n,m=map(int,input().split())
ab=[list(map(int,input().split())) for _ in range(n-1)]
pq=[list(map(int,input().split())) for _ in range(m)]
ab2=sorted(ab, key=lambda x:x[0])
pq2=sorted(pq, key=lambda x:x[0])

res=[0 for _ in range(n)]
for p,q in pq2:
	res[p-1]+=q
for a,b in ab2:
	res[b-1]+=res[a-1]
ans=str(res[0])
for i in range(1,n):
	ans+=" "+(str(res[i]))
print(ans)
Buggy code:
import sys
sys.setrecursionlimit(100000)
readline = sys.stdin.readline

N , Q = map(int,readline().rstrip().split())
G = [[] for _ in range(N)]
for n in range(N-1):
    a , b = map(int,readline().rstrip().split())
    G[a-1].append(b-1)
query = []
[query.append( list(map(int,readline().rstrip().split()))) for _ in range(Q) ]
counter = [0] * N 
# visit = []

def dfs(root , x , visit):
    if visit[root] == 0:
        visit[root] = 1
        counter[root] += x
    else:
        return 0
    
    for g in G[root]:
        dfs( g, x , visit)
    
    return 0

def solve():
    for q in query:
        visit = [0] * N
        dfs(q[0]-1,q[1] , visit)
    [print(c , end="" ) for c in counter]
    print("")

if __name__ == "__main__":
    solve()

Fixed code:
import sys
sys.setrecursionlimit(10000000)
readline = sys.stdin.readline

N , Q = map(int,readline().rstrip().split())
G = [[] for _ in range(N)]
for n in range(N-1):
    a , b = map(int,readline().rstrip().split())
    G[a-1].append(b-1)
    G[b-1].append(a-1)
query = [0] * N
for q in range(Q):
    p , x = map(int,readline().rstrip().split())
    query[p-1] += x
counter = [0] * N 
visit = [0] * N

def dfs(root , x ):
    if visit[root] == 0:
        visit[root] = 1
        x += query[root]
        counter[root] += x
    else:
        return 0
    
    for g in G[root]:
        dfs( g, x )

    return 0

def solve():
    dfs(0 , 0)
    [print(c , end = " " ) for c in counter]
    print("")

if __name__ == "__main__":
    solve()
Buggy code:
def o():return map(int,input().split())
n,q=o()
a=[[]for i in range(n)]
x=[0]*n
for i in range(n-1):u,v=o()
  a[u-1]+=[v-1]
for i in range(q):u,v=o()
  p=[u-1]
  while p:r=p.pop()
    x[r]+=v
    p+=a[r]
print(*x)

Fixed code:
def o():return map(int,input().split())
n,q=o()
a=[[]for i in range(n)]
x=[0]*n
for i in range(n-1):
  u,v=o()
  a[u-1]+=[v-1]
  a[v-1]+=[u-1]
for i in range(q):
  u,v=o()
  x[u-1]+=v
p=[[0,0]]
s=0
while p:
  r,s=p.pop()
  for i in a[r]:
    if i!=s:
      p+=[[i,r]]
      x[i]+=x[r]
print(*x)
Buggy code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10000000)

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

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

    for _ in range(Q):
        node, x = map(int, input().split())
        node -= 1
        cnt[node] += x

    visited[0]=1
    st = [0]
    while st:
        v = st.pop()
        for u in G[v]:
            if visited[u]:
                continue
            visited[u] = 1
            cnt[u] += cnt[v]
            st.append(u)    

    print(*cnt)

if __name__ == '__main__':
    main()



Fixed code:
def main():
  import sys
  sys.setrecursionlimit(500000)
  input = sys.stdin.readline

  N, Q = map(int, input().split())
  G = [set() for _ in range(N)]
  cnt = [0]*N
  visited = [0]*N

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

  for i in range(Q):
      node, x = map(int, input().split())
      node -= 1
      cnt[node] += x

  visited[0]=1
  st = [0]
  while st:
      v = st.pop()
      for u in G[v]:
          if visited[u]:
              continue
          visited[u] = 1
          cnt[u] += cnt[v]
          st.append(u)    

  print(*cnt)
if __name__ == '__main__':
    main()
Buggy 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)]
dp = [0] * n
ls = [[] for i in range(n)]
for i in ab:
    ls[i[0]-1].append(i[1]-1)
for i in px:
    dp[i[0]-1] += i[1]
for i in range(n):
    for j in ls[i]:
        dp[j] += dp[i]
print(dp)


Fixed 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)]
dp = [0] * n
ls = [[] for i in range(n)]
for i in ab:
    ls[i[0]-1].append(i[1]-1)
for i in px:
    dp[i[0]-1] += i[1]
for i in range(n):
    for j in ls[i]:
        dp[j] += dp[i]
print(*dp)
Buggy code:
N,Q = map(int,input().split())
scores = [0 for _ in range(N)]
flags = [1 for _ in range(N)]

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

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

from collections import deque
nextp = deque([0])
flags[0] = 0
while nextp:
    p = nextp.pop()
    for t in tree[p]:
        if flags[t]:
            scores[t] += scores[p]
            flags[t] = 0
            nextp.append(t)


Fixed code:
N,Q = map(int,input().split())
scores = [0 for _ in range(N)]
flags = [1 for _ in range(N)]

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

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

from collections import deque
nextp = deque([0])
flags[0] = 0
while nextp:
    p = nextp.pop()
    for t in tree[p]:
        if flags[t]:
            scores[t] += scores[p]
            flags[t] = 0
            nextp.append(t)
print(*scores)
Buggy code:
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:])


Fixed code:
import sys
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 code:
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:])


Fixed code:
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 code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**6)
 
N, Q = map(int, input().split())
V = {i: [] for i in range(1,N+1)}
for _ in range(N-1):
    a, b = map(int, input().split())
    V[a].append(b)
    V[b].append(a)
C = {i: 0 for i in range(1,N+1)}
 
for _ in range(Q):
    p, x = map(int, input().split())
    C[p] += x
 
 
def propagate(u, p):
    C[u] += C[p]
    for v in V[u]:
        if v == p:
            continue
        propagate(v, u)

propagate(1, 0)
 
print(' '.join(str(C[i]) for i in range(1,N+1)))

Fixed code:
import sys

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

N, Q = map(int, input().split())
V = {i: [] for i in range(1, N + 1)}
for _ in range(N - 1):
    a, b = map(int, input().split())
    V[a].append(b)
    V[b].append(a)

C = {i: 0 for i in range(1, N + 1)}

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


def propagate(u, p):
    for v in V[u]:
        if v == p:
            continue
        C[v] += C[u]
        propagate(v, u)


propagate(1, 0)

print(' '.join(str(C[i]) for i in range(1, N + 1)))
Buggy code:
N,Q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(N-1)]
px = [list(map(int,input().split())) for i in range(Q)]

graph = [[] for _ in range(N+1)]
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)

value = [0] * (N+1)
for p,x in px:
    value[p] += x

from collections import deque
todo = deque([(1,0])])
while len(todo) > 0:
    x,parent = todo.pop()
    value[x] += value[parent]
    for y in graph[x]:
        if y == parent:
            continue
        todo.append((y,x))

print(*value[1:])

Fixed code:
N,Q = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(N-1)]
px = [list(map(int,input().split())) for i in range(Q)]

graph = [[] for _ in range(N+1)]
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)

value = [0] * (N+1)
for p,x in px:
    value[p] += x

from collections import deque
todo = deque([(1,0)])
while len(todo) > 0:
    x,parent = todo.pop()
    value[x] += value[parent]
    for y in graph[x]:
        if y == parent:
            continue
        todo.append((y,x))

answer = ' '.join(map(str,value[1:]))
print(answer)
Buggy code:
import sys
sys.setrecursionlimit(10 ** 7)

N,Q = map(int,input().split())
AB = [[x for x in map(int,input().split())] for _ in range(N-1)]
PX = [[x for x in 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)

cnt = [0]*N
for p,x in PX:
    cnt[p-1] += x

def dfs(v,parent,add):
    for w in graph[v]:
        if w != parent:
             cnt[w-1] += add
             dfs(w,v,cnt[w-1])

dfs(1,0,0)

print(*cnt)

Fixed code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

N,Q = map(int,input().split())
AB = [[x for x in map(int,input().split())] for _ in range(N-1)]
PX = [[x for x in 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)

cnt = [0]*N
for p,x in PX:
    cnt[p-1] += x

def dfs(v,parent,add):
    for w in graph[v]:
        if w != parent:
             cnt[w-1] += add
             dfs(w,v,cnt[w-1])

for i in graph[1]:
    cnt[i-1] += cnt[0]
    dfs(i,1,cnt[i-1])

print(*cnt)
Buggy code:
import sys
sys.setrecursionlimit(1000000)

def input():
    return sys.stdin.readline()[:-1]

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

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

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

# print(edge)
# print(counter)

visited = [False] * (N + 1)
visited[1] = True
ans = [0] * (N + 1)

def dfs(fro, p):
    visited[fro] = True
    now = counter[fro] + p
    for to in edge[fro]:
        if not visited[to]:
            dfs(to, now)
    ans[fro] = now
    return

dfs(1, 0)

print(*ans[1:])


Fixed code:
import sys
sys.setrecursionlimit(1000000)

def input():
    return sys.stdin.readline()

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

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

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

# print(edge)
# print(counter)

visited = [False] * (N + 1)
visited[1] = True
ans = [0] * (N + 1)

def dfs(fro, p):
    visited[fro] = True
    now = counter[fro] + p
    for to in edge[fro]:
        if not visited[to]:
            dfs(to, now)
    ans[fro] = now
    return

dfs(1, 0)

print(*ans[1:])
Buggy code:
import sys
input = sys.stdin.readline


N,Q=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(N-1)]
PLUS=[list(map(int,input().split())) for i in range(Q)]

SCORE=[0]*(N+1)
for p,x in PLUS:
    SCORE[p]+=x

ELIST=[[] for i in range(N+1)]

for x,y in EDGE:
    ELIST[x].append(y)
    ELIST[y].append(x)
#print(ELIST)

from collections import deque
Q=deque([1])

USED=[0]*(N+1)
USED[1]=1
#USED = [0, 1, 0, 0, 0] Ex1
#SCORE = [0, 100, 10, 1, 0] Ex1
#ELIST = [[], [2], [1, 3, 4], [2], [2]] Ex1

while Q:
    x=Q.pop()
    print(x)

    for to in ELIST[x]:
        if USED[to]==0:
            USED[to]=1
            SCORE[to]+=SCORE[x]
            Q.append(to)

print(*SCORE[1:])


Fixed code:
import sys
input = sys.stdin.readline


N,Q=map(int,input().split())
EDGE=[list(map(int,input().split())) for i in range(N-1)]
PLUS=[list(map(int,input().split())) for i in range(Q)]

SCORE=[0]*(N+1)
for p,x in PLUS:
    SCORE[p]+=x

ELIST=[[] for i in range(N+1)]

for x,y in EDGE:
    ELIST[x].append(y)
    ELIST[y].append(x)
#print(ELIST)

from collections import deque
Q=deque([1])

USED=[0]*(N+1)
USED[1]=1
#USED = [0, 1, 0, 0, 0] Ex1
#SCORE = [0, 100, 10, 1, 0] Ex1
#ELIST = [[], [2], [1, 3, 4], [2], [2]] Ex1

while Q:
    x=Q.pop()

    for to in ELIST[x]:
        if USED[to]==0:
            USED[to]=1
            SCORE[to]+=SCORE[x]
            Q.append(to)

print(*SCORE[1:])
Buggy code:
import sys
sys.setrecursionlimit(10**6)
n, q = map(int, input().split())

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

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

def dfs(v, p):
    for u in edge[v]:
        if u != p:
            imos[u] += imos[v]
            dfs(u, v)

dfs(0, -1)
print(' '.join([str(x) for x in imos]))


Fixed code:
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline

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

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

def dfs(v, p):
    for u in edge[v]:
        if u == p:
            continue
        imos[u] += imos[v]
        dfs(u, v)

dfs(0, -1)
print(' '.join([str(x) for x in imos]))
Buggy code:
#/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6). 

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

adj = [[] for _ in range(N)]

for _ in range(N - 1):
    a, b = map(int, input().split())
    a, b = (a - 1, b - 1)
    adj[a].append(b)
    adj[b].append(a)
    
values = [0 for _ in range(N)]
    
for _ in range(Q):
    p, x = map(int, input().split())
    values[p - 1] += x

def dfs(node, parent):
    for child in adj[node]:
        if child == parent: continue
        values[child] += values[node]
        dfs(child, node)

dfs(0, -1)

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

Fixed code:
#/usr/bin/env python3
import sys
sys.setrecursionlimit(10**6)

def input():
    return sys.stdin.readline()

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

adj = [[] for _ in range(N)]

for _ in range(N - 1):
    a, b = map(int, input().split())
    a, b = (a - 1, b - 1)
    adj[a].append(b)
    adj[b].append(a)
    
values = [0 for _ in range(N)]
    
for _ in range(Q):
    p, x = map(int, input().split())
    values[p - 1] += x

def dfs(node, parent):
    for child in adj[node]:
        if child == parent: continue
        values[child] += values[node]
        dfs(child, node)

dfs(0, -1)

print(" ".join(map(str, values)))
Buggy code:
from collections import deque
n, q = map(int, input().split())
t = [[] for _ in range(n + 1)]
for _ in range(n - 1):
  a, b = map(int, input().split())
  t[a].append(b)
  t[b].append(a)
c = [0] * n
for _ in range(q):
  p, x = map(int, input().split())
  c[p - 1] += x
stk = deque()
stk.appendleft((0, -1))
while len(stk) != 0:
  i, p = stk.popleft()
  for j in t[i]:
    if j == p:
      continue
    c[j] += c[i]
    stk.appendleft((j, i))
print(*c)


Fixed code:
from collections import deque
n, q = map(int, input().split())
t = [[] for _ in range(n)]
for _ in range(n - 1):
  a, b = map(lambda x: int(x) - 1, input().split())
  t[a].append(b)
  t[b].append(a)
c = [0] * n
for _ in range(q):
  p, x = map(int, input().split())
  c[p - 1] += x
stk = deque()
stk.appendleft((0, -1))
while len(stk) != 0:
  i, p = stk.popleft()
  for j in t[i]:
    if j == p:
      continue
    c[j] += c[i]
    stk.appendleft((j, i))
print(*c)
Buggy code:
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n,q=LI()
ab=[LI() for _ in range(n-1)]
px=[LI() for _ in range(q)
ans=[0]*(n+1)
graph={i:collections.deque() for i in range(1,n+1)}
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
for p,x in px:
    ans[p]+=x
seen=[0]*(n+1)
stack=[]
def dfs():
    seen[1]=1
    stack.append(1)
    while stack:
        s=stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            gno=graph[s].popleft()
            if seen[gno]:
                continue
            seen[gno]=1
            stack.append(gno)
            ans[gno]+=ans[s]
dfs()
print(*ans[1:])

Fixed code:
import collections,sys
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
n,q=LI()
ab=[LI() for _ in [None]*(n-1)]
px=[LI() for _ in [None]*q]
ans=[0]*(n+1)
graph={i:collections.deque() for i in range(1,n+1)}
for a,b in ab:
    graph[a].append(b)
    graph[b].append(a)
for p,x in px:
    ans[p]+=x
seen=[0]*(n+1)
stack=[]
def dfs():
    seen[1]=1
    stack.append(1)
    while stack:
        s=stack.pop()
        if not graph[s]:
            continue
        for j in range(len(graph[s])):
            gno=graph[s].popleft()
            if seen[gno]:
                continue
            seen[gno]=1
            stack.append(gno)
            ans[gno]+=ans[s]
dfs()
print(*ans[1:])
Buggy code:
import sys
sys.setrecursionlimit(10**7) 

def dfs(G,v,p):
    for next in G[v]:
        if next == p:
            continue
        dfs(G,next,v)
    
    for c in G[v]:
        if c == p:
            continue
        subtree[v].append(c)
        subtree[v]+=subtree[c]

N,Q = map(int, input().split())
counter = [0]*N
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)

subtree = [[] for _ in range(N)]
dfs(G,0,-1)
print(subtree)
for _ in range(Q):
    p,x = map(int, input().split())
    p-=1
    counter[p]+=x
    for i in subtree[p]:
        counter[i]+=x
counter = map(str,counter)
print(' '.join(counter))

Fixed code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7) 

def dfs(G,v,p):
    for next in G[v]:
        if next == p:
            continue
        counter[next]+=counter[v]
        dfs(G,next,v)
    

N,Q = map(int, input().split())
counter = [0]*N
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)

#print(subtree)
for _ in range(Q):
    p,x = map(int, input().split())
    p-=1
    counter[p]+=x
dfs(G,0,-1)
counter = map(str,counter)
print(' '.join(counter))
Buggy code:
n,q = map(int,input().split())
v = [[0,0] for i in range(n+1)]
for i in range(n-1):
    a,b = map(int,input().split())
    v[a].append(b)
for i in range(q):
    p,x = map(int,input().split())
    v[p][1] += x
q = [1]
while q:
    now = q.pop()
    # print(now)
    for i in v[now][2:]:
        v[i][1] += v[now][1]
    q += v[now][2:]
    # print(q)
print(v)
s = []
for i in v:
    s.append(i[1])
print(*s[1:])

Fixed code:
n,q = map(int,input().split())
v = [[0,0] for i in range(n+1)]
for i in range(n-1):
    a,b = map(int,input().split())
    v[a].append(b)
    v[b].append(a)
for i in range(q):
    p,x = map(int,input().split())
    v[p][1] += x
q = [1]
while q:
    now = q.pop()
    v[now][0] = 1
    for i in v[now][2:]:
        if v[i][0] == 0:
            v[i][1] += v[now][1]
            q.append(i)
s = []
for i in v:
    s.append(i[1])
print(*s[1:])
Buggy code:
n, q = map(int, input().split())
ko=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int, input().split())
    ko[a-1].append(b-1)
ans=[0]*n
for i in range(q):
    a,b=map(int, input().split())
    ans[a-1]+=b
nex=[0]
while nex:
    p=nex.pop()
    print(nex)
    for i in ko[p]:
        ans[i]+=ans[p]
    nex+=ko[p]
print(*ans)

Fixed code:
n, q = map(int, input().split())
ko=[[] for i in range(n)]
for i in range(n-1):
    a,b=map(int, input().split())
    ko[a-1].append(b-1)
ans=[0]*n
for i in range(q):
    a,b=map(int, input().split())
    ans[a-1]+=b
nex=[0]
while nex:
    p=nex.pop()
    for i in ko[p]:
        ans[i]+=ans[p]
    nex+=ko[p]
print(*ans)