結果

問題 No.2325 Skill Tree
ユーザー mugcap
提出日時 2023-05-28 14:31:54
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 668 ms / 3,000 ms
コード長 2,314 bytes
コンパイル時間 3,104 ms
コンパイル使用メモリ 184,664 KB
実行使用メモリ 19,148 KB
最終ジャッジ日時 2024-12-27 01:52:56
合計ジャッジ時間 22,085 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/dsu>
// #include <atcoder/modint>
using namespace std;
// using namespace atcoder;
#define SZ(x) (int)(x).size()
#define dout(x) printf("%.10f\n", (double)(x))
#define ALL(s) (s).begin(), (s).end()
#define so(V) sort(ALL(V))
#define rev(V) reverse(ALL(V))
typedef pair<int, int> P;
typedef pair<long, long> Pl;
typedef vector<vector<int>> vvi;
typedef vector<vector<long>> vvl;

long gcd(long a, long b)
{
    if (b == 0)
    {
        return a;
    }
    else
    {
        return gcd(b, a % b);
    }
}

/*  lcm (a, b) : 2整数版
    入力:整数 a, b
    出力:aとbの最小公倍数
*/
long lcm(long a, long b)
{
    long d = gcd(a, b);
    return a / d * b;
}

int main()
{
    long N, Q, a, b;
    cin >> N;
    vector<vector<Pl>> waza(N);
    vector<long> saisyou(N, -1), canln(N, 1);
    for (int i = 1; i < N; i++)
    {
        cin >> a >> b;
        b--;
        waza[b].push_back({i, a});
    }
    saisyou[0] = 0;
    canln[0] = 0;
    queue<Pl> que;
    que.push({0, 0});
    while (!que.empty())
    {
        long nx = que.front().first;
        long ny = que.front().second;
        que.pop();
        saisyou[nx] = ny;
        for (int i = 0; i < SZ(waza[nx]); i++)
        {
            if (canln[waza[nx][i].first] == 1)
            {
                canln[waza[nx][i].first] = 0;
                que.push({waza[nx][i].first, max(ny, waza[nx][i].second)});
            }
        }
    }

    map<long, long> level, anslevel;
    vector<long> anslevelmin;
    for (int i = 0; i < N; i++)
    {
        if (saisyou[i] == -1)
        {
            continue;
        }
        if (level.count(saisyou[i]))
        {
            level[saisyou[i]]++;
        }
        else
        {
            level[saisyou[i]] = 1;
        }
    }
    long ans = 0;
    for (auto val : level)
    {
        ans += val.second;
        anslevel[val.first] = ans;
        anslevelmin.push_back(val.first);
    }
    cin >> Q;

    for (int i = 0; i < Q; i++)
    {
        cin >> a >> b;
        if (a == 1)
        {
            auto itl = upper_bound(ALL(anslevelmin), b);
            itl--;
            cout << anslevel[*itl] << endl;
        }
        else
        {
            b--;
            cout << saisyou[b] << endl;
        }
    }

    return 0;
}
0