結果

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

ソースコード

diff #

import sys
from sys import stdin
from collections import defaultdict

def main():
    sys.setrecursionlimit(1 << 25)
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    ptr += 1

    adj = defaultdict(set)
    for _ in range(N-1):
        u = int(input[ptr])
        v = int(input[ptr+1])
        adj[u].add(v)
        adj[v].add(u)
        ptr += 2

    A = list(map(int, input[ptr:ptr+N]))
    ptr += N

    Q = int(input[ptr])
    ptr += 1
    queries = list(map(int, input[ptr:ptr+Q]))

    active = A.copy()
    is_active = [True] * N

    for x in queries:
        sum_val = 0
        if is_active[x]:
            sum_val = active[x]
        is_active[x] = False  # Temporarily deactivate to avoid processing x in the loops

        # Process distance 1 and 2 nodes
        for y in adj[x]:
            if is_active[y]:
                sum_val += active[y]
                is_active[y] = False
            # Check neighbors of y (distance 2)
            for z in adj[y]:
                if z != x and z not in adj[x]:
                    if is_active[z]:
                        sum_val += active[z]
                        is_active[z] = False

        # Update x's active value and status
        active[x] = sum_val
        is_active[x] = True
        print(sum_val)

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