結果

問題 No.3660 LIS on Tree
コンテスト
ユーザー t98slider
提出日時 2026-08-30 14:32:29
言語 C++23
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 41 ms / 2,000 ms
+ 975µs
コード長 1,916 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,316 ms
コンパイル使用メモリ 343,644 KB
実行使用メモリ 8,896 KB
最終ジャッジ日時 2026-08-30 14:32:39
合計ジャッジ時間 4,196 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

template<class T> istream& operator >> (istream& is, vector<T>& vec) {
    for(T& x : vec) is >> x;
    return is;
}

// atcoder::internal::csr の模倣
template <class T> struct csr {
    struct Node {
        csr* g;
        int u;
        template<class... Args>
        void emplace_back(Args&&... args){
            g->add_edge(u, T(std::forward<Args>(args)...));
        }
        auto begin(){ return g->E.begin() + g->start[u]; }
        auto end(){ return g->E.begin() + g->start[u + 1]; }
        int size(){ return g->start[u + 1] - g->start[u]; }
        T& operator[](int p){ return *(begin() + p); }
    };
    int N;
    std::vector<int> start;
    std::vector<T> E;
    std::vector<std::pair<int,T>> edge;
    csr(int n) : N(n), start(n + 1) {edge.reserve(n);}
    void add_edge(int u, T v){
		assert(0 <= u && u < N);
        start[u + 1]++;
        edge.emplace_back(u, v);
    }
    void build(){
        E.resize(edge.size());
        for(int i = 0; i < N; i++) start[i + 1] += start[i];
        auto cnt = start;
        for(auto [u, v] : edge) E[cnt[u]++] = v;
    }
	const int size() {return N;}
    Node operator[](int u) {return Node{this, u};}
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> a(n);
    cin >> a;
    csr<int> g(n);
    for(int i = 1; i < n; i++){
        int u, v;
        cin >> u >> v;
        u--, v--;
        if(a[u] < a[v]) g[u].emplace_back(v);
        if(a[v] < a[u]) g[v].emplace_back(u);
    }
    g.build();
    vector<ll> dp(n, -1);
    auto dfs = [&](auto dfs, int v) -> ll {
        if(dp[v] != -1) return dp[v];
        ll mx = 0;
        for(auto &&u : g[v]) mx = max(mx, dfs(dfs, u));
        return dp[v] = mx + a[v];
    };
    ll ans = 0;
    for(int i = 0; i < n; i++) ans = max(ans, dfs(dfs, i));
    cout << ans << '\n';
}
0