結果

問題 No.2325 Skill Tree
ユーザー FplusFplusF
提出日時 2023-05-28 14:24:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 231 ms / 3,000 ms
コード長 1,355 bytes
コンパイル時間 2,852 ms
コンパイル使用メモリ 208,572 KB
最終ジャッジ日時 2025-02-13 11:24:59
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
vector<vector<ll>> g;
vector<ll> l;
vector<bool> vis;
void dfs(ll x,ll m){
    for(auto &i:g[x]){
        if(vis[i]) continue;
        vis[i]=true;
        chmax(l[i],m);
        dfs(i,l[i]);
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n;
    cin >> n;
    g.resize(n);
    l.resize(n);
    l[0]=1;
    rep(i,n-1){
        ll a;
        cin >> l[i+1] >> a;
        a--;
        g[a].push_back(i+1);
    }
    vis.resize(n,false);
    vis[0]=true;
    dfs(0,1);
    vector<ll> v;
    rep(i,n){
        if(!vis[i]) continue;
        v.push_back(l[i]);
    }
    sort(all(v));
    ll q;
    cin >> q;
    while(q--){
        ll t;
        cin >> t;
        if(t==1){
            ll x;
            cin >> x;
            cout << (ll)(upper_bound(all(v),x)-v.begin()) << '\n';
        }
        if(t==2){
            ll y;
            cin >> y;
            y--;
            if(!vis[y]) cout << "-1\n";
            else cout << l[y] << '\n';
        }
    }
}
0