結果

問題 No.2325 Skill Tree
ユーザー marble
提出日時 2023-05-28 15:59:58
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,252 bytes
コンパイル時間 2,554 ms
コンパイル使用メモリ 180,664 KB
実行使用メモリ 30,720 KB
最終ジャッジ日時 2024-12-27 10:26:41
合計ジャッジ時間 68,630 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 2
other AC * 5 WA * 19 TLE * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int solve(){
    int x,y;
    cin >> x >> y;
    map<int,int> vis;
    if(x == 1){
        ans2=0;
        dfs2(0,y,vis);
        return ans2;
    }else{
        ll ans = dfs(0,1,y-1,vis);
        return ans;   
    }
}

int main(){
    cin >> 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