結果

問題 No.2325 Skill Tree
ユーザー marble
提出日時 2023-05-28 15:30:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,499 bytes
コンパイル時間 2,570 ms
コンパイル使用メモリ 178,424 KB
実行使用メモリ 27,008 KB
最終ジャッジ日時 2024-12-27 07:09:34
合計ジャッジ時間 125,324 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 7 TLE * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<vector<pair<int,int>>> g;
vector<int> vis;
int N;
int dfs(int c,int ml,int last){
    if(c == last){
        return ml;
    }
    int ans=-1;
    vis[c] = true;
    for(auto next : g[c]){
        if(vis[next.first]){
            continue;
        }
        ans = dfs(next.first,max(ml,next.second), last);
        if(ans != -1){
            break;
        }
    }
    return ans;
}

int solve(){
    int x,y;
    cin >> x >> y;
    if(x == 1){
        deque<int> co;
        ll ans=1;
        co.push_back(0);
        while(!co.empty()){
            int p=co.front();
            co.pop_front();
            vis[p] = true;
            for(auto next:g[p]){
                if(next.second<=y && !vis[next.first]){
                    ans++;
                    co.push_back(next.first);
                    vis[next.first] = true;
                }
            }
        }
        for(int i=0;i<N;i++){
            vis[i] = false;
        }
        return ans;
    }else{
        ll ans = dfs(0,1,y-1);
        for(int i=0;i<N;i++){
            vis[i] = false;
        }
        return ans;   
    }
}

int main(){
    cin >> N;
    vis.resize(N);
    g.resize(N);
    for(int i=1;i<N;i++){
        int L,A;
        cin >> L >> A;
        A--;
        g[A].push_back({i,L});
        g[i].push_back({A,L});
    }
    int Q;
    cin >> Q;
    for(int i=0;i<Q;i++){
        int ans = solve();
        cout << ans << endl;
    }
}
0