結果
| 問題 |
No.899 γatheree
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 19:11:03 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 1,559 bytes |
| コンパイル時間 | 314 ms |
| コンパイル使用メモリ | 82,124 KB |
| 実行使用メモリ | 595,344 KB |
| 最終ジャッジ日時 | 2025-06-12 19:11:19 |
| 合計ジャッジ時間 | 14,866 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 20 MLE * 1 -- * 2 |
ソースコード
import sys
from collections import defaultdict, deque
def main():
sys.setrecursionlimit(1 << 25)
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
adj = [[] for _ in range(N)]
for _ in range(N-1):
u = int(input[ptr])
v = int(input[ptr+1])
adj[u].append(v)
adj[v].append(u)
ptr += 2
# Precompute S(x) for each x
S = [set() for _ in range(N)]
for x in range(N):
S[x].add(x)
for y in adj[x]:
if y not in S[x]:
S[x].add(y)
for z in adj[y]:
if z != x and z not in S[x]:
S[x].add(z)
# Convert to list to avoid modification during iteration
S[x] = list(S[x])
A_initial = list(map(int, input[ptr:ptr+N]))
ptr += N
Q = int(input[ptr])
ptr += 1
queries = [int(input[ptr + i]) for i in range(Q)]
processed = set()
activated = dict()
for x in queries:
sum_total = 0
to_remove = []
for node in S[x]:
if node not in processed:
sum_total += A_initial[node]
if node in activated:
sum_total += activated[node]
to_remove.append(node)
# Remove nodes from activated
for node in to_remove:
del activated[node]
# Add all nodes in S[x] to processed
for node in S[x]:
processed.add(node)
activated[x] = sum_total
print(sum_total)
if __name__ == "__main__":
main()
gew1fw