結果

問題 No.3660 LIS on Tree
コンテスト
ユーザー 𝘪𝘣𝘵𝘰𝘴𝘮-𝟙𝟛𝟞𝟙
提出日時 2026-09-01 19:39:20
言語 Python3
(3.14.7 + numpy 2.5.2 + scipy 1.18.0 + ACL)
コンパイル:
python3 -mpy_compile _filename_
実行:
python3 _filename_
結果
AC  
実行時間 958 ms / 2,000 ms
+ 419µs
コード長 651 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 286 ms
コンパイル使用メモリ 21,336 KB
実行使用メモリ 53,908 KB
最終ジャッジ日時 2026-09-01 19:39:41
合計ジャッジ時間 10,777 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from collections import deque

N = int(input())
S = list(map(int, input().split()))

G = [[] for _ in range(N)]
indeg = [0] * N
for _ in range(N - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    if S[a] > S[b]:
        G[b].append(a)
        indeg[a] += 1
    if S[a] < S[b]:
        G[a].append(b)
        indeg[b] += 1

ret = [0] * N
que = deque([])
for i in range(N):
    if indeg[i] == 0:
        que.append(i)
        ret[i] = S[i]

while que:
    x = que.popleft()
    for nx in G[x]:
        ret[nx] = max(ret[nx], ret[x] + S[nx])
        indeg[nx] -= 1
        if indeg[nx] == 0:
            que.append(nx)

print(max(ret))
0