結果

問題 No.1637 Easy Tree Query
ユーザー yudai1102jpyudai1102jp
提出日時 2021-08-06 23:57:30
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 1,088 ms / 2,000 ms
コード長 750 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 46,080 KB
最終ジャッジ日時 2024-09-17 04:23:08
合計ジャッジ時間 21,963 ms
ジャッジサーバーID
(参考情報)
judge1 / judge6
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 33
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq
sys.setrecursionlimit(10**6)  # 再帰関数使う時有効


def countup(now, R):
    # if len(root[now]) == 1 and root[now][0] in used:
    #     count[root[now][0]] += count[now]+1
    #     return
    # if now in used:
    #     return
    for next_ in root[now]:
        if next_ == R:
            continue
        countup(next_, now)
    if now == R:
        return
    global count
    count[R] += count[now]


N, Q = map(int, input().split())
root = [[] for i in range(N+1)]
for i in range(N-1):
    a, b = map(int, input().split())
    root[a-1].append(b-1)
    root[b-1].append(a-1)
count = [1]*N
countup(0, 0)

ans = 0
for i in range(Q):
    p, x = map(int, input().split())

    ans += count[p-1]*x
    print(ans)
0