結果

問題 No.3660 LIS on Tree
コンテスト
ユーザー LyricalMaestro
提出日時 2026-09-07 01:29:10
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 598 ms / 2,000 ms
+ 228µs
コード長 1,948 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 61 ms
コンパイル使用メモリ 82,884 KB
実行使用メモリ 183,664 KB
最終ジャッジ日時 2026-09-07 01:29:36
合計ジャッジ時間 6,120 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

# https://yukicoder.me/problems/no/3660

from collections import deque

def main():
    N = int(input())
    S = list(map(int, input().split()))
    next_nodes = [[] for _ in range(N)]
    for _ in range(N - 1):
        a, b = map(int ,input().split())
        next_nodes[a - 1].append(b - 1)
        next_nodes[b - 1].append(a - 1)

    # 全方位木dp

    stack = deque()
    parents = [-2] * N
    nexts = [{} for _ in range(N)]
    stack.append((0, 0))
    parents[0] = -1
    while len(stack) > 0:
        v, index = stack.pop()

        while index < len(next_nodes[v]):
            w = next_nodes[v][index]
            if w == parents[v]:
                index += 1
                continue

            parents[w] = v
            stack.append((v, index + 1))
            stack.append((w, 0))
            break

        if index == len(next_nodes[v]):
            p = parents[v]
            if p != -1:
                s = S[v]
                max_v = 0
                for c, v0 in nexts[v].items():
                    s0 = S[c]
                    if s < s0:
                        max_v = max(max_v, v0)
                nexts[p][v] = max_v + s
    
    queue = deque()
    queue.append((0, 0))
    while len(queue) > 0:
        v, value = queue.popleft()
        if parents[v] != -1:
            p = parents[v]
            nexts[v][p] = value

        max_v = 0
        for c, value in nexts[v].items():
            s = S[c]
            if s > S[v]:
                max_v = max(max_v, value)

        for c in next_nodes[v]:
            if c == parents[v]:
                continue

            if S[c] < S[v]:
                queue.append((c, S[v] + max_v))
            else:
                queue.append((c, 0))

    answer = 0
    for i in range(N):
        for v, value in nexts[i].items():
            if S[v] > S[i]:
                answer = max(answer, value + S[i])
    print(answer)





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