結果

問題 No.2325 Skill Tree
ユーザー Ryoga.exeRyoga.exe
提出日時 2023-05-28 15:16:37
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 579 ms / 3,000 ms
コード長 1,829 bytes
コンパイル時間 2,315 ms
コンパイル使用メモリ 211,768 KB
実行使用メモリ 14,340 KB
最終ジャッジ日時 2023-08-27 11:49:10
合計ジャッジ時間 21,631 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 261 ms
4,612 KB
testcase_08 AC 175 ms
7,588 KB
testcase_09 AC 294 ms
6,336 KB
testcase_10 AC 225 ms
10,632 KB
testcase_11 AC 299 ms
8,312 KB
testcase_12 AC 552 ms
14,096 KB
testcase_13 AC 561 ms
14,092 KB
testcase_14 AC 559 ms
14,120 KB
testcase_15 AC 555 ms
14,340 KB
testcase_16 AC 554 ms
14,124 KB
testcase_17 AC 551 ms
14,056 KB
testcase_18 AC 541 ms
14,000 KB
testcase_19 AC 540 ms
14,328 KB
testcase_20 AC 542 ms
14,316 KB
testcase_21 AC 536 ms
14,048 KB
testcase_22 AC 551 ms
13,980 KB
testcase_23 AC 563 ms
14,092 KB
testcase_24 AC 561 ms
14,120 KB
testcase_25 AC 554 ms
14,076 KB
testcase_26 AC 554 ms
14,180 KB
testcase_27 AC 567 ms
13,208 KB
testcase_28 AC 562 ms
13,388 KB
testcase_29 AC 566 ms
13,276 KB
testcase_30 AC 565 ms
13,208 KB
testcase_31 AC 569 ms
13,136 KB
testcase_32 AC 552 ms
13,248 KB
testcase_33 AC 550 ms
13,264 KB
testcase_34 AC 558 ms
13,212 KB
testcase_35 AC 575 ms
13,268 KB
testcase_36 AC 570 ms
13,272 KB
testcase_37 AC 579 ms
13,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct UnionFind {
    std::vector<int> par;
    inline UnionFind(const size_t n) : par(n, -1) {}
    inline void reset(const size_t n) { par.assign(n, -1); }
    inline int root(const size_t x) noexcept { return (par[x] < 0 ? x : par[x] = root(par[x])); }
    inline bool unite(size_t x, size_t y) noexcept {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) std::swap(x, y);
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    inline bool same(const size_t x, const size_t y) noexcept { return root(x) == root(y); }
    inline int size(const size_t x) noexcept { return -par[root(x)]; };
};

int main() {
    int n;
    cin >> n;
    UnionFind uf(n);
    vector<vector<int>> g(n);
    vector<int> type2(n);
    for (int i = 1; i < n; i++) {
        int l, a;
        cin >> l >> a;
        type2[i] = l;
        g[a - 1].push_back(i);
        uf.unite(a - 1, i);
    }
    queue<int> que;
    que.push(0);
    type2[0] = 0;
    while (!que.empty()) {
        auto cur = que.front();
        que.pop();
        for (auto to : g[cur]) {
            if (type2[to] < type2[cur]) {
                type2[to] = type2[cur];
            }
            que.push(to);
        }
    }
    int exclude = 0;
    for (int i = 0; i < n; i++) {
        if (!uf.same(0, i)) {
            exclude++;
            type2[i] = -1;
        }
    }
    auto type1 = type2;
    sort(type1.begin(), type1.end());
    int q;
    cin >> q;
    while (q--) {
        int type, x;
        cin >> type >> x;
        if (type == 1) {
            cout << (upper_bound(type1.begin(), type1.end(), x) - type1.begin()) - exclude << endl;
        }
        else {
            cout << type2[x - 1] << endl;
        }
    }
}
0