結果

問題 No.2325 Skill Tree
ユーザー おうどんおうどん
提出日時 2023-05-28 17:42:15
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 541 ms / 3,000 ms
コード長 4,836 bytes
コンパイル時間 3,919 ms
コンパイル使用メモリ 259,720 KB
実行使用メモリ 7,332 KB
最終ジャッジ日時 2023-08-27 14:44:29
合計ジャッジ時間 21,978 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 259 ms
4,380 KB
testcase_08 AC 163 ms
4,440 KB
testcase_09 AC 287 ms
4,380 KB
testcase_10 AC 201 ms
5,112 KB
testcase_11 AC 287 ms
4,580 KB
testcase_12 AC 522 ms
6,280 KB
testcase_13 AC 515 ms
6,216 KB
testcase_14 AC 516 ms
6,264 KB
testcase_15 AC 516 ms
6,284 KB
testcase_16 AC 517 ms
6,172 KB
testcase_17 AC 506 ms
6,204 KB
testcase_18 AC 515 ms
6,208 KB
testcase_19 AC 501 ms
6,224 KB
testcase_20 AC 508 ms
6,288 KB
testcase_21 AC 503 ms
6,316 KB
testcase_22 AC 517 ms
6,268 KB
testcase_23 AC 515 ms
6,348 KB
testcase_24 AC 520 ms
6,348 KB
testcase_25 AC 517 ms
6,216 KB
testcase_26 AC 517 ms
6,164 KB
testcase_27 AC 531 ms
7,324 KB
testcase_28 AC 531 ms
7,264 KB
testcase_29 AC 529 ms
7,148 KB
testcase_30 AC 531 ms
7,148 KB
testcase_31 AC 533 ms
7,264 KB
testcase_32 AC 511 ms
7,196 KB
testcase_33 AC 513 ms
7,320 KB
testcase_34 AC 513 ms
7,328 KB
testcase_35 AC 528 ms
7,268 KB
testcase_36 AC 534 ms
7,332 KB
testcase_37 AC 541 ms
7,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <chrono>
using namespace std;
using namespace chrono;
#define rep(i,n) for(int i = 0 ; i < (n) ; i++)
#define rep1(i,n) for(int i = 1 ; i <= (n) ; i++)
#define rrep(i,n) for(int i = (n) - 1 ; i >= 0 ; i--)
#define rrep1(i,n) for(int i = (n) ; i > 0 ; i--)
using ll = int64_t;
using P = pair<int, int>;
using PL = pair<ll,ll>;
using PD = pair<double,double>;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using vP = vector<P>;
template<typename T> using v = vector<T>;
template<typename T> using vv = vector<vector<T>>;
template<typename T> using vvv = vector<vector<vector<T>>>;
template<typename T> using p_q = priority_queue<T, v<T>, greater<T>>;
const int INF = 1001001001;
const int INTMAX = (1ll<<31)-1;
const ll LINF = (1ll<<62);
const ll LLMAX = (1ll<<62) + ((1ll<<62)-1);
#define line cout << "================================" << endl;
#define Yn(x) ((x) ? "Yes" : "No")
#define yn(x) ((x) ? "yes" : "no")
#define debug(x) cout << #x" : " << (x) << endl;
#define output(x) cout << (x) << endl;
#define outs(x) cout << #x << endl;
#define mod(n) %(n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define m_p(a, b) make_pair(a, b)
#define set_time(x) steady_clock::time_point (x) = steady_clock::now()
#define p_time(x) duration_cast<milliseconds>(x).count()
#define show_time(s, e) cout << "実行時間" << duration_cast<milliseconds>(e-s).count() << "ms\n"
template <typename T> bool chmin(T& a, T b){if(a>b){a=b;return 1;}return 0;}
template <typename T> bool chmax(T& a, T b){if(a<b){a=b;return 1;}return 0;}
ostream& operator<< (ostream& os, P a) {return os << a.first << " , " << a.second;}

class alucrex{
    public:
    void vin(vi& a){for(auto& i:a)cin>>i;}
    void vin(vl& a){for(auto& i:a)cin>>i;}
    void vin(vP& a){for(auto& x:a)cin>>x.first>>x.second;}
    void vin1(vi& a){rep1(i,a.size()-1)cin>>a[i];}
    void vvin(vvi& a){for(auto& i:a)for(auto& j:i)cin>>j;}
    void vvin(vvl& a){for(auto& i:a)for(auto& j:i)cin>>j;}
    void vvdes(vvi& a){for(auto& i:a){for(auto& j:i){
        cout << j << " ";}puts("");
    }}
    void vvdes(vvl& a){for(auto& i:a){for(auto& j:i){
        cout << j << " ";}puts("");
    }}
    template<typename T>
    void vvd(vv<T>& a){rep(i,a.size()){rep(j,a[i].size()){
        cout << a[i][j] << " ";}cout<<endl;
    }}
    bool TLE(steady_clock::time_point s, int time = 9800){
        steady_clock::time_point e = steady_clock::now();
        if(duration_cast<milliseconds>(e - s).count() >= time){puts("TLE");return true;}
        return false;
    }
    ll rui(ll n, int r, ll m = LLMAX) {
        n %= m;
        ll res = (r < 2 ? 1 : n);
        ll cnt = 1;
        if(r >= 2)for( ; cnt *2 <= r ; cnt *= 2) {
            res *= res;
            res %= m;
        }
        else cnt = 0;
        for(int i = 0 ; i < (r - cnt) ; i++) {res *= n; res %= m;}
        return res;
    }
    ll kai(ll n) { //O(N)
        ll res = 1;
        for(int i = 2 ; i <= n ; i++) res *= i;
        return res;
    }
};
const int MOD = 1000000007;

vi dp;
int dfs(int p, vP &vec) {
    if(p == 0) return 0;
    if(dp[p] != INF) return dp[p];
    dp[p] = vec[p].first;
    // debug(m_p(p, dp[p]));
    return dp[p] = max(dp[p], dfs(vec[p].second, vec));
}

struct unionFind {
    vector<int> par;

    int root(int x) {
        if(par[x] == -1) return x;
        return par[x] = root(par[x]);
    }
    void unite(int x, int y) {
        x = root(x);
        y = root(y);
        if(x != y) par[x] = y;
    }
    bool is_same(int x, int y) {
        return root(x) == root(y);
    }
    unionFind(int x) : par(x, -1) {}
};

int main(){
    //set_time(__start__);
    // alucrex al;
    int n; cin >> n;
    unionFind uf(n);
    vP vec(n, {0, 0});
    dp.resize(n, INF); dp[0] = 0;
    rep1(i, n-1) {
        // debug(i);
        cin >> vec[i].first >> vec[i].second;
        // debug(vec[1].first);

        vec[i].second--;
        // debug(vec[1].first)
        uf.unite(i, vec[i].second);
        // debug(vec[1].first);
    }
    // line;for(auto f : vec) cout << f << endl;line;




    rep1(i, n-1) dfs(i, vec);

    vi lev;
    rep(i, n-1) if(uf.is_same(0, i+1)) lev.emplace_back(dp[i+1]);
    sort(all(lev));

    // line; rep1(i, n-1) {
    //     cout << dp[i] << " , " << uf.is_same(0, i) << endl;
    // }line;

    int Q; cin >> Q;
    rep(q, Q) {
        int t, x; cin >> t >> x;
        if(t == 1) {
            auto itr = upper_bound(all(lev), x);
            cout << itr - lev.begin() + 1 << endl;
        }
        if(t == 2) {
            x--;
            if(uf.is_same(0, x)) cout << dp[x] << endl;
            else cout << -1 << endl;
        }
    }
    //set_time(__end__);
    //show_time(__start__ , __end__);
}
0