結果
問題 | 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.readlinesys.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 -= 1b -= 1e[a].append(b)e[b].append(a)dist = [-1]*Ndef dfs(x):if(x!=0):if len(e[x]) == 1:returnfor ix in e[x]:if dist[ix] == -1:dist[ix] = dist[x] + 1dfs(ix)returndist[0] = 0dfs(0)flg = [-1]*Ndef dfs_v2(x):if(x!=0):if len(e[x]) == 1:flg[x] = 1return 1if flg[x] != -1:return flg[x]tmp = 1for ix in e[x]:if dist[ix] > dist[x]:tmp += dfs_v2(ix)flg[x] = tmpreturn tmpdfs_v2(0)S = 0for _ in range(Q):p,x = map(int,input().split())p -= 1S += x*flg[p]print(S)# print(flg)