結果

問題 No.3660 LIS on Tree
コンテスト
ユーザー pia019
提出日時 2026-08-30 14:56:51
言語 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  
実行時間 67 ms / 2,000 ms
+ 121µs
コード長 1,629 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,996 ms
コンパイル使用メモリ 378,216 KB
実行使用メモリ 17,540 KB
最終ジャッジ日時 2026-08-30 14:56:59
合計ジャッジ時間 6,173 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
typedef long long ll;
const int INF = 1<<30;
const ll INFLL = 1LL<<60;
const ll MOD = 998244353;
const double INFD = 1.0E10;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, -1, 0, 1};
//const int dx[8] = {1, 1, 0, -1, -1, -1, 0, 1};
//const int dy[8] = {0, 1, 1, 1, 0, -1, -1, -1};
using Pair = pair<ll, ll>;
using Graph = vector<vector<int>>;
using mint = atcoder::modint998244353;


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);
    
    ll n; cin >> n;
    vector<ll> s(n);
    for (int i = 0; i < n; i++) cin >> s[i];
    vector<vector<ll>> G(n);
    vector<int> deg(n);
    for (int i = 0; i < n - 1; i++){
        int u, v; cin >> u >> v;
        u--; v--;
        if (s[u] == s[v]) continue;
        if (s[u] < s[v]){
            G[u].push_back(v);
            deg[v]++;
        }
        else{
            G[v].push_back(u);
            deg[u]++;
        }
    }
    
    vector<int> ord;
    for (int i = 0; i < n; i++){
        if (deg[i] == 0) ord.push_back(i);
    }
    
    for (int i = 0; i < n; i++){
        for (auto j : G[ord[i]]){
            deg[j]--;
            if (deg[j] == 0) ord.push_back(j);
        }
    }
    
    
    
    ll ans = 0;
    vector<ll> dp(n);
    for (int i = 0; i < n; i++){
        dp[ord[i]] += s[ord[i]];
        // cerr << ord[i] << " : " << dp[ord[i]] << endl;
        ans = max(ans, dp[ord[i]]);
        for (auto j : G[ord[i]]){
            dp[j] = max(dp[j], dp[ord[i]]);
        }
    }
    
    cout << ans << endl;
    
    return 0;
}
0