結果

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

ソースコード

diff #

import sys
from sys import stdin

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

    adj = [set() for _ in range(N)]
    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]))

    state = [0] * N  # 0: initial, 1: active as center, 2: deactivated
    current_value = [0] * N
    for i in range(N):
        current_value[i] = A[i]

    for x in queries:
        sum_val = 0

        # Process x itself
        if state[x] != 2:
            sum_val += current_value[x]
        state[x] = 2  # Temporarily mark as deactivated

        # Process direct neighbors
        for y in adj[x]:
            if state[y] != 2:
                sum_val += current_value[y]
            state[y] = 2

        # Process neighbors of neighbors
        adj_x = adj[x]
        for y in adj[x]:
            for z in adj[y]:
                if z == x or z in adj_x:
                    continue
                if state[z] != 2:
                    sum_val += current_value[z]
                state[z] = 2

        # Reactivate x and set its value
        state[x] = 1
        current_value[x] = sum_val

        print(sum_val)

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