結果

問題 No.2325 Skill Tree
ユーザー MMMM
提出日時 2023-05-28 13:54:16
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 722 ms / 3,000 ms
コード長 1,193 bytes
コンパイル時間 2,096 ms
コンパイル使用メモリ 180,672 KB
実行使用メモリ 14,776 KB
最終ジャッジ日時 2024-12-26 22:15:01
合計ジャッジ時間 23,152 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define chmin(x,y) (x) = min((x),(y))
#define chmax(x,y) (x) = max((x),(y))
using namespace std;
using ll = long long;
const ll mod = 998244353;
const vector<int> dx = {1,0,-1,0}, dy = {0,1,0,-1};
using Graph = vector<vector<int>>;

int main(){
  // input
  int N; cin >> N;
  Graph G(N);
  vector<int> L(N),A(N);
  for (int i = 1; i < N; i++){
    cin >> L[i] >> A[i];
    A[i]--; // manage skills ID by 0-indexed
    G[A[i]].push_back(i);
  }
  
  // prep: dijkstra
  vector<int> v,lv(N,2e9);
  priority_queue<pair<int,int>> pq;
  
  pq.emplace(0,0); lv[0] = 0;
  
  while(!pq.empty()){
    int cur_l = pq.top().first, cur_s = pq.top().second;
    pq.pop();
    for(auto nxt : G[cur_s]){
      int nec_l = max(L[nxt],-cur_l);
      if(lv[nxt] > nec_l){
        lv[nxt] = nec_l;
        pq.emplace(-nec_l,nxt);
      }
    }
  }
  
  for(int i = 0; i < N; i++)
    if(lv[i] < 2e9)
      v.push_back(lv[i]);
  sort(v.begin(),v.end());
  
  // solve + output
  int Q; cin >> Q;
  while(Q--){
    int q,x; cin >> q >> x;
    if(q == 1)
      cout << upper_bound(v.begin(),v.end(),x) - v.begin() << endl;
    else
      cout << (lv[x-1] < 2e9? lv[x-1] : -1) << endl;
  }
}
0