結果

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

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read
    data = input().split()
    idx = 0
    N = int(data[idx])
    idx +=1
    Q = int(data[idx])
    idx +=1

    # Build the tree
    adj = [[] for _ in range(N+1)]
    for _ in range(N-1):
        a = int(data[idx])
        idx +=1
        b = int(data[idx])
        idx +=1
        adj[a].append(b)
        adj[b].append(a)

    # Initialize weights
    weights = [0] * (N+1)

    for _ in range(Q):
        X = int(data[idx])
        idx +=1
        Y = int(data[idx])
        idx +=1
        Z = int(data[idx])
        idx +=1

        # Output current weight of X
        print(weights[X])

        # BFS to find all nodes within Y distance
        visited = [False] * (N+1)
        q = deque()
        q.append((X, 0))
        visited[X] = True

        while q:
            u, d = q.popleft()
            if d > Y:
                continue
            weights[u] += Z
            for v in adj[u]:
                if not visited[v] and d + 1 <= Y:
                    visited[v] = True
                    q.append((v, d+1))

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