結果
問題 |
No.1038 TreeAddQuery
|
ユーザー |
![]() |
提出日時 | 2025-04-16 15:29:57 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 1,426 bytes |
コンパイル時間 | 597 ms |
コンパイル使用メモリ | 81,520 KB |
実行使用メモリ | 253,648 KB |
最終ジャッジ日時 | 2025-04-16 15:33:19 |
合計ジャッジ時間 | 8,508 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 5 TLE * 1 -- * 18 |
ソースコード
import sys from collections import deque def main(): sys.setrecursionlimit(1 << 25) input = sys.stdin.read().split() ptr = 0 N, Q = int(input[ptr]), int(input[ptr+1]) ptr +=2 # Build adjacency list adj = [[] for _ in range(N+1)] for _ in range(N-1): a = int(input[ptr]) b = int(input[ptr+1]) adj[a].append(b) adj[b].append(a) ptr +=2 # Read queries queries = [] for _ in range(Q): X = int(input[ptr]) Y = int(input[ptr+1]) Z = int(input[ptr+2]) queries.append((X, Y, Z)) ptr +=3 # Process each query ans = [] node_vals = [0] * (N+1) # 1-based for X, Y, Z in queries: # Output current value of X ans.append(str(node_vals[X])) # BFS to find all nodes within Y distance visited = [False] * (N+1) q = deque() q.append((X, 0)) visited[X] = True nodes_to_update = [] while q: u, d = q.popleft() nodes_to_update.append(u) if d >= Y: continue for v in adj[u]: if not visited[v]: visited[v] = True q.append((v, d+1)) # Add Z to all nodes in nodes_to_update for u in nodes_to_update: node_vals[u] += Z print('\n'.join(ans)) if __name__ == '__main__': main()