結果

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

ソースコード

diff #

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
N,Q = map(int,input().split())
AB = [tuple(map(int,input().split())) for i in range(N-1)]
PX = [tuple(map(int,input().split())) for i in range(Q)]

es = [[] for _ in range(N)]
for a,b in AB:
    a,b = a-1,b-1
    es[a].append(b)
    es[b].append(a)

cs = [0] * N
def dfs(v,p=-1):
    if len(es[v])==1 and es[v][0]==p:
        cs[v] = 1
        return cs[v]
    ret = 1
    for to in es[v]:
        if to==p: continue
        ret += dfs(to,v)
    cs[v] = ret
    return ret
dfs(0)

ans = 0
for p,x in PX:
    p -= 1
    ans += cs[p] * x
    print(ans)
0