Problem p02936 (python)

Fixed submission:
import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**9)
n,q=map(int,input().split())
cnt=[0]*(n)
edges=[0]*n
for i in range(n):
    edges[i]=[]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].append(s-1);edges[s-1].append(a-1)

from collections import deque
dq=deque([])
#pop/append/(append,pop)_left/in/len/count/[]/index/rotate()(右へnずらす)
for i in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x

for i in edges[0]:
    dq.append((0,i))
while dq:
    f,t=dq.popleft()
    cnt[t]+=cnt[f]
    for to in edges[t]:
        if to!=f:dq.append((t,to))
    
print(*cnt)

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
cnt=[0]*n
used=[0]*n
edges=[[] for i in range(n)]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].append(s-1);edges[s-1].append(a-1)
import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**9)
def f(now):
    for to in  edges[now]:
        if used[to]:continue
        cnt[to]+=cnt[now]
        used[to]=1
        f(to)
for i in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x

used[0]=1
f(0)
print(*cnt)

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
cnt=[0]*n
used=[0]*n
edges=[[] for i in range(n)]
for i in range(n-1):
    a,s=map(int,input().split())
    edges[a-1].append(s-1);edges[s-1].append(a-1)
import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**9)
def f(now):
    for to in  edges[now]:
        if used[to]:continue
        cnt[to]+=cnt[now]
        used[to]=1
        f(to)
for i in range(q):
    p,x=map(int,input().split())
    cnt[p-1]+=x

used[0]=1
f(0)
print(*cnt)

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
n,q=map(int,input().split())
edges=[[] for i in range(n)]
inp=sys.stdin.buffer.readlines()
for i in inp[:n-1]:
    a,s=map(int,i.split());a-=1;s-=1
    edges[a].append(s)
    edges[s].append(a)
cnt=[-1]*n
add=[0]*n
for _ in inp[n-1:]:
    a,s=map(int,_.split())
    add[a-1]+=s


def f(x):
    if cnt[x]!=-1:return 0
    ret=add[x]
    fd=0
    for to in edges[x]:
        if cnt[to]!=-1:ret+=cnt[to];fd=to;break
    cnt[x]=str(ret)
    for to in edges[x]:
        if to!=fd:f(to)
    
    
f(0)
print(' '.join(cnt))

Buggy submission (verdict: Runtime Error):
import sys
input=sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
n,q=map(int,input().split())
edges=[[] for i in range(n)]
inp=sys.stdin.buffer.readlines()
for i in inp[:n-1]:
    a,s=map(int,i.split());a-=1;s-=1
    edges[a].append(s)
    edges[s].append(a)
cnt=[-1]*n
add=[0]*n
for _ in inp[n-1:]:
    a,s=map(int,_.split())
    add[a-1]+=s


def f(x):
    if cnt[x]!=-1:return 
    ret=add[x]
    fd=0
    for to in edges[x]:
        if cnt[to]!=-1:ret+=cnt[to];fd=to;break
    cnt[x]=str(ret)
    for to in edges[x]:
        if to!=fd:f(to)
    
    
f(0)
print(' '.join(cnt))

Buggy submission (verdict: Runtime Error):
n,q=map(int,input().split())
edges=[[] for i in range(n)]
for i in range(n-1):
    a,s=map(int,input().split());a-=1;s-=1
    edges[a].append(s)
    edges[s].append(a)
cnt=[-1]*n
add=[0]*n
for _ in range(q):
    a,s=map(int,input().split())
    add[a-1]+=s

def f(x):
    if cnt[x]!=-1:return cnt[x]
    ret=add[x]
    after=[]
    for to in edges[x]:
        if cnt[to]==-1:after.append(to)
        else:ret+=cnt[to]
    cnt[x]=ret
    for i in after:
        f(i)
    return 0
f(0)
print(*cnt,sep=" ")

Buggy submission (verdict: Runtime Error):
#include<iostream>
#include<queue>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#include<string>// 加法 S.size() s.substr(l,r=s.size()-l) -> l文字目からr文字分
#include<cmath>//三角関数,rad
#include<algorithm>//__gcd()　lower_bound(a+l,a+r,x)-a でindex
// vector -> min/max({vector}) 
// 配列   -> *min_element(a+l,a+r)  *忘れず lとrで[l,r) 
//reverse(a+l,a+r)   配列aの[l,r)を逆順に strはreverse(S.begin(),S.end())
//sort(a+l,a+r,greater <int>()) 配列aの[l,r)昇順sort,大きい順はgreater<type>()
//vectorならv.begin(),v.end()
#include<ctime>//"clock()/CLOCKS_PER_SEC で秒数
#include<vector>//v.push_back(x),v.pop_back()
#include<queue>//q.push(x),front()で先頭を返す,pop()で削除, size(),empty()
#include<functional>//vector,queueと併用でpriority_queue
//宣言は priority_queue< Type, vector<Type>, greater<Type>> Q1; 
// pq.push(x),top()で参照 pop()で削除   greaterで最小 lessで最大がtopに
using namespace std;
typedef pair<int,int> P;
const int inf = 10e7;

int main(){

int n,qq,a,b;
cin>>n>>qq;
vector<vector<int>> edges(n);
vector<vector<int>> rank;
rep(z,n-1){cin>>a>>b;
edges.at(--a).emplace_back(--b);
edges.at(b).emplace_back(a);
}
vector<int> bb(1,0);
rank.emplace_back(bb);
vector<unsigned long long> cnter(n,0ull),res(n+1,0ull);
vector<int> par(n,-1);
queue<int> q;
par.at(0)=n;
q.push(0);
q.push(-1);vector<int> v;
while(!q.empty()){
  int a=q.front();q.pop();
  if (a==-1){
    if(v.empty())break;
    else{rank.emplace_back(v);q.push(-1);v={};}
    }
  else{
    for(int k:edges.at(a)){
      if (par.at(k)==-1){par.at(k)=a;v.emplace_back(k);q.push(k);}
    }}
}
rep(eem,qq){
  unsigned long long r,v;cin>>r>>v;cnter.at(--r)=cnter.at(r)+v;
}
res.at(0)=cnter.at(0);
for(vector<int> vv:rank){for (int jj:vv){res.at(jj)=res.at(par.at(jj))+cnter.at(jj);
                                        }}
rep(u,n-1){cout<<res.at(u)<<' ';}cout<<res.at(n-1)<<'\n';
//rep(i,n){cout<<cnter.at(i)<<'a';}
}