結果

問題 No.2325 Skill Tree
ユーザー みどみど
提出日時 2023-05-28 16:25:08
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 2,728 bytes
コンパイル時間 4,232 ms
コンパイル使用メモリ 226,172 KB
実行使用メモリ 48,976 KB
最終ジャッジ日時 2023-08-27 13:56:45
合計ジャッジ時間 24,861 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 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 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 506 ms
10,852 KB
testcase_08 AC 441 ms
17,060 KB
testcase_09 AC 601 ms
15,460 KB
testcase_10 AC 792 ms
25,384 KB
testcase_11 AC 968 ms
21,916 KB
testcase_12 TLE -
testcase_13 AC 2,406 ms
39,304 KB
testcase_14 AC 2,374 ms
39,432 KB
testcase_15 AC 2,559 ms
39,828 KB
testcase_16 TLE -
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>
#define mrep(i, s, n) for (ll i = (s); i < (ll)(n); i++)
#define rep(i, n) mrep(i, 0, n)
using namespace std;
using ll = long long;
const ll INF = ll(1e+9)+1;
const ll INFL = ll(1e+18)+1;

ll N;
vector<ll> L, A;
vector<vector<pair<ll,ll>>> graph, graph_rev;

vector<ll> memo_L, memo_A;
unordered_map<ll, ll> memo0, memo1;

int main() {
    cin >> N;
    L.resize(N+2, 0), A.resize(N+2, 1);
    rep(i, N-1) {cin >> L[i+2] >> A[i+2];}
    graph = vector<vector<pair<ll, ll>>>(N+2);
    graph_rev = vector<vector<pair<ll, ll>>>(N+2);

    rep(i, N) {
        ll u = i+2, v = A[i+2];
        graph[v].push_back(make_pair(u, L[i+2]));
        graph_rev[u].push_back(make_pair(v, L[i+2]));
    }

    vector<ll> level_sorted(N);
    copy(L.begin()+2, L.end(), level_sorted.begin());
    sort(level_sorted.begin(), level_sorted.end());

    memo_L.resize(N, 0);
    memo_A.resize(N, 0);
    for (ll i = 0; i<N; i++) {
        ll lvl = level_sorted[i];
        ll ans = 0;
        queue<ll> q;
        q.push(1);
        while(!q.empty()) {
            ans++;
            ll s = q.front(); q.pop();
            // 隣接している頂点を追加
            for (auto un: graph[s]) {
                auto [u, l] = un;
                if (lvl < l) {continue;} // レベル不足
                q.push(u);
            }
        }
        memo_L[i] = lvl;
        memo_A[i] = ans;
    }
    memo_L.push_back(INFL);
    

    ll Q;
    cin >> Q;
    rep(_, Q) {
        ll op, val;
        cin >> op >> val;
        if (op == 1) {
            if (memo0.count(val)) {
                cout << memo0[val] << endl;
                continue;
            }
            auto it = lower_bound(memo_L.begin(), memo_L.end(), ll(val));
            
            if (*it != val) {it--;}
            ll idx = it-memo_L.begin();
            memo0[val] = memo_A[idx] - 1;
            cout << memo_A[idx]-1 << endl;
        } else {
            if (memo1.count(val)) {
                cout << memo1[val] << endl;
                continue;
            }
            bool ac = false;
            vector<bool> pushed(N+2, false);
            pushed[val] = true;
            ll ans = -1;
            queue<ll> q;
            q.push(val);
            while(!q.empty()) {
                ll s = q.front(); q.pop();
                if (s == 1) {ac = true; break;}
                for(auto ul: graph_rev[s]) {
                    auto [u, l] = ul;
                    if (pushed[u]) {continue;}
                    ans = max(ans, l);
                    if(l) {q.push(u); pushed[u] = true;}
                }
            }
            if (!ac) {ans = -1;}
            memo1[val] = ans;
            cout << ans << endl;
        }
    }

}
0