結果

問題 No.2325 Skill Tree
ユーザー marblemarble
提出日時 2023-05-28 15:38:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,393 bytes
コンパイル時間 2,194 ms
コンパイル使用メモリ 175,200 KB
実行使用メモリ 12,160 KB
最終ジャッジ日時 2024-06-08 08:10:11
合計ジャッジ時間 12,799 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
権限があれば一括ダウンロードができます

ソースコード

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;
}

void dfs2(int c, int ml){
    vis[c] = true;
    for(auto next : g[c]){
        if(vis[next.first]){
            continue;
        }
        if(next.second<=ml){
            dfs2(next.first, ml);
        }
    }
}

int solve(){
    int x,y;
    cin >> x >> y;
    if(x == 1){
        dfs2(0,y);
        int ans=0;
        for(int i=0;i<N;i++){
            if(vis[i] = true){
                ans++;
            }
            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