Problem p02936 (python)

Fixed submission:
def main():
    n, q = map(int, input().split(' '))
    import sys
    from collections import deque
    sys.setrecursionlimit(10**7)

    visit = [True for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    next_visit = deque()
    next_visit.append(0)
    visit[0] = False
    counter = 1
    while counter < n:
        t = next_visit.pop()
        for s in dic[t]:
            if visit[s]:
                point[s] += point[t]
                visit[s] = False
                counter += 1
                next_visit.append(s)
    print(*point)
if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))


visit = [0 for _ in range(n)]
point = [0 for _ in range(n)]
dic = [[] for _ in range(n)]

def add_p(num):
    if visit[num] == 1:
        return 0
    visit[num] = 1
    for i in dic[num]:
        if visit[i] == 0:
            point[i] += point[num]
            add_p(i)
    return 0

for _ in range(n-1):
    a, b = map(int, input().split(' '))
    dic[a-1].append(b-1)
    dic[b-1].append(a-1)
for _ in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x

add_p(0)

print(*point)

Buggy submission (verdict: Runtime Error):
def main():
    n, q = map(int, input().split(' '))
    visit = [0 for _ in range(n)]
    point = [0 for _ in range(n)]
    dic = [[] for _ in range(n)]

    def add_p(num):
        if visit[num] == 1:
            return 0
        visit[num] = 1
        for i in dic[num]:
            if visit[i] == 0:
                point[i] += point[num]
                add_p(i)
        return 0
    for _ in range(n-1):
        a, b = map(int, input().split(' '))
        dic[a-1].append(b-1)
        dic[b-1].append(a-1)
    for _ in range(q):
        p, x = map(int, input().split(' '))
        point[p-1] += x
    add_p(0)
    print(*point)

main()

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))


visit = [0 for _ in range(n)]
point = [0 for _ in range(n)]
dic = [[] for _ in range(n)]

def add_p(num):
    if visit[num] == 1:
        return 0
    visit[num] = 1
    for i in dic[num]:
        if visit[i] == 0:
            point[i] += point[num]
            add_p(i)
    return 0

for _ in range(n-1):
    a, b = map(int, input().split(' '))
    dic[a-1].append(b-1)
    dic[b-1].append(a-1)
for _ in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x

add_p(0)

print(*point)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
score = [0 for _ in range(n)]
visit = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split(' '))
    score[p-1] += x
def addp(num):
    if all(visit[x] == 1 for x in inf[num]):
        return 0
    for i in inf[num]:
        if visit[i] == 0:
            score[i] += score[num]
            visit[i]  = 1
    for i in inf[num]:
        addp(i)
    return 0
visit[0] = 1
addp(0)
print(*score)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
score = [0 for _ in range(n)]
visit = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split(' '))
    score[p-1] += x
def addp(num):
    if all(visit[x] == 1 for x in inf[num]):
        return 0
    visit[num] = 1
    for i in inf[num]:
        if visit[i] == 0:
            score[i] += score[num]
            addp(i)
    return 0
addp(0)
print(*score)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
score = [0 for _ in range(n)]
visit = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split(' '))
    score[p-1] += x
def addp(num):
    if all(visit[x] == 1 for x in inf[num]):
        return 0
    visit[num] = 1
    for i in inf[num]:
        if visit[i] == 1:
            continue
        else:
            score[i] += score[num]
            addp(i)
    return 0
addp(0)
print(*score)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))
inf = [[] for _ in range(n)]
for _ in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
score = [0 for _ in range(n)]
visit = [0 for _ in range(n)]
for _ in range(q):
    p, x = map(int, input().split(' '))
    score[p-1] += x
def addp(num):
    if visit[inf[num][0]] == 1 and len(inf[num]) == 1:
        return 0
    visit[num] = 1
    for i in inf[num]:
        if visit[i] == 1:
            continue
        else:
            score[i] += score[num]
            addp(i)
    return 0
addp(0)
print(*score)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))
     
inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
visit = [0 for _ in range(n)]
def add(num):
    if visit[num] == 1:
        return 0
    visit[num] = 1
    for k in inf[num]:
        if visit[k] == 0:
            point[k] += point[num]
            add(k)
        else:
            continue
    return 0
add(0)
print(' '.join(map(str, point)))

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))
     
inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
visit = [0 for _ in range(n)]
def add(num):
    if len(inf[num]) == 1 and visit[num] == 1:
        return 0
    visit[num] = 1
    for k in inf[num]:
        if visit[k] == 0:
            point[k] += point[num]
            add(k)
    return 0
add(0)
print(' '.join(map(str, point)))

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))
     
inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
visit = [0 for _ in range(n)]
def add(num):
    if len(inf[num]) == 1 and visit[num] == 1:
        return 0
    visit[num] = 1
    for k in inf[num]:
        if visit[k] == 0:
            point[k] += point[num]
            add(k)
    return 0
add(0)
print(*point)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))
     
inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
def add(num):
    if len(inf[num]) == 0:
        return 0
    for k in inf[num]:
        inf[k].remove(num)
        point[k] += point[num]
        add(k)
    return 0
     
add(0)
print(*point)

Buggy submission (verdict: Runtime Error):
n, q = map(int, input().split(' '))

inf = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split(' '))
    inf[a-1].append(b-1)
    inf[b-1].append(a-1)
point = [0 for _ in range(n)]
for i in range(q):
    p, x = map(int, input().split(' '))
    point[p-1] += x
def add(num):
    if len(inf[num]) == 0:
        return 0
    for k in inf[num]:
        inf[k].remove(num)
        point[k] += point[num]
        add(k)

add(0)
print(*point)

Buggy submission (verdict: Runtime Error):
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n)]
p_list = [0 for _ in range(n)]

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

for i in range(q):
    p, x = list(map(int, input().split(' ')))
    p_list[p-1] += x

def add_p(num):
    if len(inf[num]) == 0:
        return 0
    for k in inf[num]:
        inf[k] = [x for x in inf[k] if x != num]
        p_list[k] += p_list[num]
        add_p(k)
    return 0

add_p(0)
print(*p_list)

Buggy submission (verdict: Runtime Error):
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n)]
p_list = [0 for _ in range(n)]

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

for i in range(q):
    p, x = list(map(int, input().split(' ')))
    p_list[p-1] += x

def add_p(num):
    if len(inf[num]) == 0:
        return 0
    for k in inf[num]:
        inf[k].remove(num)
        p_list[k] += p_list[num]
        add_p(k)
    return 0

add_p(0)
print(*p_list)

Buggy submission (verdict: Runtime Error):
n, q = list(map(int, input().split(' ')))
inf = [[] for _ in range(n+1)]
p_list = [0 for _ in range(n+1)]

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

def add_p(num, point):
    if len(inf[num]) == 0:
        p_list[num] += point
        return 0
    p_list[num] += point
    for next_num in inf[num]:
        print(next_num, point)
        add_p(next_num, point)
    return 0
for i in range(q):
    p, x = list(map(int, input().split(' ')))
    add_p(p, x)
result = p_list[1:]
result = list(map(str, result))
print(' '.join(result))