結果

問題 No.3660 LIS on Tree
コンテスト
ユーザー Unbakedbread
提出日時 2026-08-30 15:04:41
言語 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  
実行時間 69 ms / 2,000 ms
+ 25µs
コード長 1,095 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,315 ms
コンパイル使用メモリ 341,208 KB
実行使用メモリ 18,688 KB
最終ジャッジ日時 2026-08-30 15:04:47
合計ジャッジ時間 4,491 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

#define int long long
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)

int solve(void) {
    int N;
    cin >> N;
    vector<int> S(N);
    rep(i, N) cin >> S[i];
    
    vector<int> deg(N, 0);
    vector<vector<int>> G(N);
    rep(_, N - 1) {
    	int U, V;
    	cin >> U >> V, --U, --V;
    	if(S[U] < S[V]) swap(U, V);
    	if(S[U] == S[V]) continue;
    	
    	G[U].push_back(V);
    	deg[V] += 1;
    }
    
    vector<int> ans(N, 0);
    queue<int> que;
    rep(v, N) if(deg[v] == 0) que.push(v);
    
    while(!que.empty()) {
    	int v = que.front(); que.pop();
    	ans[v] += S[v];
    	for(auto nv : G[v]) {
    		deg[nv] -= 1;
    		ans[nv] = max(ans[nv], ans[v]);
    		if(deg[nv] == 0) que.push(nv);
    	}
    }
    
    int res = 0;
    rep(v, N) res = max(res, ans[v]);
    
    cout << res << "\n";
    
    return 0;
}

signed main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    int Testcases = 1;
    //cin >> Testcases;
    while(Testcases--) solve();
    
    return 0;
}

0