結果

問題 No.3660 LIS on Tree
コンテスト
ユーザー MM
提出日時 2026-08-30 14:00:49
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 260 ms / 2,000 ms
+ 499µs
コード長 1,369 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,535 ms
コンパイル使用メモリ 390,508 KB
実行使用メモリ 22,188 KB
最終ジャッジ日時 2026-08-30 14:01:09
合計ジャッジ時間 7,667 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#include<atcoder/all>
#define chmin(x,y) (x) = min((x),(y))
#define chmax(x,y) (x) = max((x),(y))
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define vec vector
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back 
#define eb emplace_back

using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
const ll mod = 998244353;
using mint = modint998244353;
const vector<int> dx = {1,0,-1,0}, dy = {0,1,0,-1};
// using Graph = vector<vector<pair<int,ll>>>;
using Graph = vector<vector<int>>;

int main(){
  // input + prep
  int N;
  cin >> N;
  vec<int> S(N+2,0); // 超頂点:0=start, N+1=end
  rep(i,N) cin >> S[i+1];
  Graph G(N+2);
  rep(i,N-1){
    int a,b;
    cin >> a >> b;
    if(S[a] > S[b]) G[b].pb(a);
    if(S[a] < S[b]) G[a].pb(b);
  }
  rep(i,N){
    G[0].pb(i+1);
    G[i+1].pb(N+1);
  }
  
  // solve
  vec<ll> dist(N+2,-1);
  priority_queue<pair<ll,int>> pq;
  dist[0] = 0;
  pq.emplace(0,0);
  while(!pq.empty()){
    auto[score, pos] = pq.top();
    pq.pop();
    if(dist[pos] > score) continue;

    for(auto nxt : G[pos]){
      ll score_nxt = score + S[nxt];
      if(dist[nxt] < score_nxt){
        dist[nxt] = score_nxt;
        pq.emplace(score_nxt, nxt);
      }
    }
  }
  
  // output
  ll ans = dist.back();
  cout << ans << endl;
}
0