結果
問題 |
No.1637 Easy Tree Query
|
ユーザー |
|
提出日時 | 2024-01-10 12:05:04 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 852 bytes |
コンパイル時間 | 175 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 28,800 KB |
最終ジャッジ日時 | 2024-09-27 20:19:44 |
合計ジャッジ時間 | 25,851 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 32 RE * 1 |
ソースコード
from collections import * from itertools import * from functools import * from heapq import * import sys,math # input = sys.stdin.readline 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)