結果

問題 No.2325 Skill Tree
ユーザー momoyuu
提出日時 2023-05-28 13:52:37
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 787 ms / 3,000 ms
コード長 1,178 bytes
コンパイル時間 4,486 ms
コンパイル使用メモリ 261,444 KB
実行使用メモリ 17,040 KB
最終ジャッジ日時 2024-12-26 22:09:08
合計ジャッジ時間 30,041 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    int n;
    cin>>n;
    vector<ll> l(n,0),a(n,0);
    vector<vector<int>> g(n);
    for(int i = 1;i<n;i++){
        cin>>l[i]>>a[i];
        a[i]--;
        g[a[i]].push_back(i);
    }
    using dat = pair<ll,int>;
    priority_queue<dat,vector<dat>,greater<dat>> que;
    vector<int> vis(n,0);
    que.push(make_pair(0ll,0));
    while(que.size()){
        dat now = que.top();
        que.pop();
        int ni = now.second;
        vis[ni] = 1;
        for(auto&e:g[ni]){
            l[e] = max(l[e],l[ni]);
            que.push(make_pair(l[e],e));
        }
    }
    vector<int> can;
    for(int i = 0;i<n;i++) if(vis[i])can.push_back(l[i]);
    sort(can.begin(),can.end());
    int q;
    cin>>q;
    while(q--){
        int op;
        cin>>op;
        if(op==1){
            int x;
            cin>>x;
            int ni = upper_bound(can.begin(),can.end(),x) - can.begin();
            cout<<ni<<endl;
        }else{
            int x;
            cin>>x;
            x--;
            if(vis[x]==0) cout<<-1<<endl;
            else cout<<l[x]<<endl;
        }
    }


        

     
}
0