結果

問題 No.2325 Skill Tree
ユーザー Nichi10pNichi10p
提出日時 2023-05-28 14:33:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 143 ms / 3,000 ms
コード長 1,589 bytes
コンパイル時間 1,207 ms
コンパイル使用メモリ 113,636 KB
実行使用メモリ 8,404 KB
最終ジャッジ日時 2023-08-27 10:05:27
合計ジャッジ時間 8,254 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 38 ms
4,376 KB
testcase_08 AC 34 ms
4,404 KB
testcase_09 AC 46 ms
4,376 KB
testcase_10 AC 49 ms
5,196 KB
testcase_11 AC 52 ms
4,560 KB
testcase_12 AC 101 ms
6,120 KB
testcase_13 AC 100 ms
6,228 KB
testcase_14 AC 102 ms
6,140 KB
testcase_15 AC 103 ms
6,284 KB
testcase_16 AC 104 ms
6,216 KB
testcase_17 AC 103 ms
6,192 KB
testcase_18 AC 99 ms
6,160 KB
testcase_19 AC 103 ms
6,200 KB
testcase_20 AC 99 ms
6,256 KB
testcase_21 AC 97 ms
6,256 KB
testcase_22 AC 101 ms
6,256 KB
testcase_23 AC 102 ms
6,420 KB
testcase_24 AC 101 ms
6,256 KB
testcase_25 AC 101 ms
6,120 KB
testcase_26 AC 101 ms
6,124 KB
testcase_27 AC 140 ms
8,320 KB
testcase_28 AC 143 ms
8,276 KB
testcase_29 AC 137 ms
8,052 KB
testcase_30 AC 135 ms
8,036 KB
testcase_31 AC 139 ms
8,052 KB
testcase_32 AC 113 ms
7,484 KB
testcase_33 AC 126 ms
8,040 KB
testcase_34 AC 126 ms
8,404 KB
testcase_35 AC 130 ms
7,468 KB
testcase_36 AC 124 ms
7,264 KB
testcase_37 AC 142 ms
8,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
#include <iterator>
#include <functional>
#include <optional>

struct Data {
    int L, A;
};

int main() {
    std::ios_base::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int N;
    std::cin >> N;
    auto G = std::vector<Data>(N + 1);
    for (int i = 2; i <= N; ++i)
        std::cin >> G[i].L >> G[i].A;

    std::map<int, int> _x;  // レベルi になって覚えられる技の種類
    auto y = std::vector(N + 1, std::optional<int>());  // 技i を覚えられるレベルの最小値
    _x[0] = 1;
    y[1] = 0;
    auto cycle = std::vector(N + 1, false);
    std::function<int(int)> f = [&](const int i) {  // 技i を覚えられるレベルの最小値
        if (y[i])
            return *y[i];
        if (cycle[i])
            return *(y[i] = -1);
        cycle[i] = true;
        int level = f(G[i].A);
        if (level < 0)
            return *(y[i] = -1);
        if (level < G[i].L)
            level = G[i].L;
        ++_x[level];
        return *(y[i] = level);
    };
    for (int i = 2; i <= N; ++i)
        f(i);
    std::map<int, int> x = _x;  // レベルi までに覚えられる技の種類
    for (auto i = next(x.begin()); i != x.end(); ++i)
        i->second += prev(i)->second;

    int Q;
    std::cin >> Q;
    while (Q--) {
        int q, i;
        std::cin >> q >> i;
        switch (q) {
        case 1:
            std::cout << prev(x.upper_bound(i))->second << "\n";
            break;
        case 2:
            std::cout << *y[i] << "\n";
            break;
        }
    }
}
0