Problem p02936 (python)

Fixed submission:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
edge = [[] for _ in range(N)]
for _ in range(N-1):
    a,b = I()
    edge[a-1].append(b-1)
    edge[b-1].append(a-1)
ans = [0]*N
for _ in range(Q):
    p,x = I()
    ans[p-1] += x
que = [0]
visited = {0}
while que:
    p = []
    for i in que:
        for j in edge[i]:
            if j in visited:
                continue
            visited.add(j)
            ans[j] += ans[i]
            p.append(j)
    que = p
print(*ans)

Buggy submission (verdict: Runtime Error):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
graph = [-1]*N
roots = [[] for _ in range(N)]
for i in range(N-1):
    a,b = I()
    graph[b-1] = a-1
def dfs(a):
    if a == 0:
        return
    b = graph[a]
    roots[b].append(i)
    dfs(b)
for i in range(N):
    dfs(i)
    roots[i].append(i)
counter = [0]*N
for i in range(Q):
  p,x = I()
  r = roots[p-1]
  if r:
      for j in r:
          counter[j] += x
print(*counter)

Buggy submission (verdict: Runtime Error):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
graph = [-1]*N
roots = [[] for _ in range(N)]
for i in range(N-1):
    a,b = I()
    graph[b-1] = a-1
def dfs(a):
    if a == 0:
        return
    b = graph[a]
    roots[b].append(i)
    dfs(b)
for i in range(N):
    dfs(i)
counter = [0]*N
for i in range(Q):
  p,x = I()
  r = roots[p-1]
  counter[p-1] += x
  if r:
      for j in r:
          counter[j] += x
print(c*)

Buggy submission (verdict: Runtime Error):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
graph = [-1]*N
roots = [[] for _ in range(N)]
for i in range(N-1):
    a,b = I()
    graph[b-1] = a-1
def dfs(a):
    if a == 0:
        return
    b = graph[a]
    roots[b].append(i)
    dfs(b)
for i in range(N):
    dfs(i)
counter = [0]*N
for i in range(Q):
  p,x = I()
  r = roots[p-1]
  counter[p-1] += x
  if r:
      for j in r:
          counter[j] += x
for c in counter:
    print(c)

Buggy submission (verdict: Runtime Error):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
mod = 10**9+7
N,Q = I()
graph = [-1]*N
roots = [[] for _ in range(N)]
for i in range(N-1):
    a,b = I()
    graph[b-1] = a-1
def dfs(a):
    if a == 0:
        return
    b = graph[a]
    roots[b].append(i)
    dfs(b)
for i in range(N-1,-1,-1):
    dfs(i)
counter = [0]*N
for i in range(Q):
  p,x = I()
  r = roots[p-1]
  counter[p-1] += x
  if r:
      for j in r:
          counter[j] += x
for c in counter:
    print(c)