結果
| 問題 | 
                            No.1637 Easy Tree Query
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2024-01-10 12:06:06 | 
| 言語 | Python3  (3.13.1 + numpy 2.2.1 + scipy 1.14.1)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,238 ms / 2,000 ms | 
| コード長 | 882 bytes | 
| コンパイル時間 | 529 ms | 
| コンパイル使用メモリ | 12,672 KB | 
| 実行使用メモリ | 50,176 KB | 
| 最終ジャッジ日時 | 2024-09-27 20:20:10 | 
| 合計ジャッジ時間 | 25,709 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 33 | 
ソースコード
from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
# input = sys.stdin.readline
sys.setrecursionlimit(200000)
N,Q = map(int,input().split())
e = [[] for _ in range(N)]
for i in range(N-1):
  a,b = map(int,input().split())
  a -= 1
  b -= 1
  e[a].append(b)
  e[b].append(a)
dist = [-1]*N
def dfs(x):
  if(x!=0):
    if len(e[x]) == 1:
        return
  for ix in e[x]:
    if dist[ix] == -1:
      dist[ix] = dist[x] + 1
      dfs(ix)
  return
dist[0] = 0
dfs(0)
flg = [-1]*N
def dfs_v2(x):
  if(x!=0):
    if len(e[x]) == 1:
        flg[x] = 1
        return 1
  if flg[x] != -1:
    return flg[x]
  tmp = 1
  for ix in e[x]:
    if dist[ix] > dist[x]:
      tmp += dfs_v2(ix)
  flg[x] = tmp
  return tmp
dfs_v2(0)
S = 0
for _ in range(Q):
  p,x = map(int,input().split())
  p -= 1
  S += x*flg[p]
  print(S)
# print(flg)