結果

問題 No.1038 TreeAddQuery
ユーザー gew1fw
提出日時 2025-06-12 18:10:40
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,115 bytes
コンパイル時間 144 ms
コンパイル使用メモリ 82,200 KB
実行使用メモリ 79,024 KB
最終ジャッジ日時 2025-06-12 18:12:37
合計ジャッジ時間 8,588 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 5 TLE * 1 -- * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    ptr = 0
    n = int(input[ptr])
    ptr += 1
    q = int(input[ptr])
    ptr += 1

    adj = [[] for _ in range(n + 1)]
    for _ in range(n - 1):
        a = int(input[ptr])
        ptr += 1
        b = int(input[ptr])
        ptr += 1
        adj[a].append(b)
        adj[b].append(a)

    values = [0] * (n + 1)

    for _ in range(q):
        x = int(input[ptr])
        ptr += 1
        y = int(input[ptr])
        ptr += 1
        z = int(input[ptr])
        ptr += 1

        # Output the current value of x
        print(values[x])

        # BFS to update all nodes within y distance
        visited = [False] * (n + 1)
        queue = deque()
        queue.append((x, 0))
        visited[x] = True
        while queue:
            u, d = queue.popleft()
            if d > y:
                break
            values[u] += z
            for v in adj[u]:
                if not visited[v]:
                    visited[v] = True
                    queue.append((v, d + 1))

if __name__ == "__main__":
    main()
0