結果

問題 No.899 γatheree
ユーザー qwewe
提出日時 2025-05-14 13:21:22
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,387 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 82,212 KB
実行使用メモリ 270,448 KB
最終ジャッジ日時 2025-05-14 13:23:52
合計ジャッジ時間 9,799 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20 TLE * 1 -- * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    N = int(sys.stdin.readline())
    
    adj = [[] for _ in range(N)]
    for _ in range(N - 1):
        u, v = map(int, sys.stdin.readline().split())
        adj[u].append(v)
        adj[v].append(u)

    fairies = list(map(int, sys.stdin.readline().split()))

    Q_count = int(sys.stdin.readline())
    
    output_lines = []

    for _ in range(Q_count):
        x_query = int(sys.stdin.readline())
        
        nodes_to_affect = set()
        
        # Add x_query (distance 0)
        nodes_to_affect.add(x_query)
        
        # Add direct neighbors of x_query (distance 1)
        # Also, for each direct neighbor, add its neighbors (these form distance 2 candidates)
        for neighbor_of_x in adj[x_query]:
            nodes_to_affect.add(neighbor_of_x)
            # Add neighbors of this neighbor_of_x
            for neighbor_of_neighbor in adj[neighbor_of_x]:
                nodes_to_affect.add(neighbor_of_neighbor)
        
        total_fairies_at_x_query_after_move = 0
        for node_v in nodes_to_affect:
            total_fairies_at_x_query_after_move += fairies[node_v]
            fairies[node_v] = 0
        
        fairies[x_query] = total_fairies_at_x_query_after_move
        
        output_lines.append(str(fairies[x_query]))

    sys.stdout.write("\n".join(output_lines) + "\n")

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