結果

問題 No.1637 Easy Tree Query
ユーザー sotanishy
提出日時 2021-08-06 21:25:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 191 ms / 2,000 ms
コード長 608 bytes
コンパイル時間 131 ms
コンパイル使用メモリ 82,412 KB
実行使用メモリ 96,596 KB
最終ジャッジ日時 2024-09-17 03:28:53
合計ジャッジ時間 6,648 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N, Q = map(int, input().split())
G = [[] for _ in range(N)]
for _ in range(N-1):
    a, b = map(lambda x: int(x) - 1, input().split())
    G[a].append(b)
    G[b].append(a)
size = [1] * N
st = [(0, -1, 0)]
while st:
    v, p, t = st.pop()
    if t == 0:
        st.append((v, p, 1))
        for c in G[v]:
            if c != p:
                st.append((c, v, 0))
    else:
        for c in G[v]:
            if c != p:
                size[v] += size[c]
ans = 0
for _ in range(Q):
    p, x = map(int, input().split())
    p -= 1
    ans += size[p] * x
    print(ans)
0