結果

問題 No.2325 Skill Tree
ユーザー t98slidert98slider
提出日時 2023-05-28 14:04:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 176 ms / 3,000 ms
コード長 2,522 bytes
コンパイル時間 1,879 ms
コンパイル使用メモリ 183,976 KB
実行使用メモリ 20,652 KB
最終ジャッジ日時 2023-08-27 08:58:05
合計ジャッジ時間 10,635 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 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,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 43 ms
5,900 KB
testcase_08 AC 55 ms
10,236 KB
testcase_09 AC 61 ms
8,120 KB
testcase_10 AC 91 ms
15,128 KB
testcase_11 AC 80 ms
11,792 KB
testcase_12 AC 170 ms
20,368 KB
testcase_13 AC 166 ms
20,404 KB
testcase_14 AC 175 ms
20,452 KB
testcase_15 AC 176 ms
20,464 KB
testcase_16 AC 175 ms
20,332 KB
testcase_17 AC 174 ms
20,560 KB
testcase_18 AC 174 ms
20,456 KB
testcase_19 AC 169 ms
20,324 KB
testcase_20 AC 175 ms
20,652 KB
testcase_21 AC 171 ms
20,348 KB
testcase_22 AC 163 ms
20,544 KB
testcase_23 AC 163 ms
20,456 KB
testcase_24 AC 164 ms
20,548 KB
testcase_25 AC 166 ms
20,340 KB
testcase_26 AC 163 ms
20,588 KB
testcase_27 AC 166 ms
20,344 KB
testcase_28 AC 166 ms
20,320 KB
testcase_29 AC 162 ms
20,384 KB
testcase_30 AC 164 ms
20,340 KB
testcase_31 AC 173 ms
20,352 KB
testcase_32 AC 160 ms
20,384 KB
testcase_33 AC 159 ms
20,392 KB
testcase_34 AC 163 ms
20,288 KB
testcase_35 AC 168 ms
20,136 KB
testcase_36 AC 164 ms
20,284 KB
testcase_37 AC 164 ms
20,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

struct Kruskal_dsu {
    public:
    vector<vector<pair<int,int>>> pa;
    Kruskal_dsu() : _n(0) {}
    Kruskal_dsu(int n) : _n(n), parent_or_size(n, -1), dat(n, 1 << 30), pa(n) {
        for(int i = 0; i < n; i++){
            pa[i].emplace_back(0, 1);
        }
    }

    bool merge(int u, int v, int w) {
        assert(0 <= u && u < _n);
        assert(0 <= v && v < _n);
        int x = leader(u), y = leader(v);
        if (x == y) return false;
        if (-parent_or_size[x] < -parent_or_size[y]) std::swap(x, y);
        parent_or_size[x] += parent_or_size[y];
        parent_or_size[y] = x;
        pa[x].emplace_back(w, -parent_or_size[x]);
        dat[y] = w;
        return true;
    }

    bool same(int v, int u) {
        assert(0 <= v && v < _n);
        assert(0 <= u && u < _n);
        return leader(v) == leader(u);
    }

    int leader(int v) {
        assert(0 <= v && v < _n);
        while(parent_or_size[v] >= 0)v = parent_or_size[v];
        return v;
    }

    int size(int v, int w){
        //cerr << v << '\n';
        while(dat[v] <= w) v = parent_or_size[v];
        //cerr << v << '\n';
        pair<int,int> tmp = {w + 1, -1};
        int j = lower_bound(pa[v].begin(), pa[v].end(), tmp) - pa[v].begin() - 1;
        return pa[v][j].second;
    }

    int size(int v) {
        assert(0 <= v && v < _n);
        return -parent_or_size[leader(v)];
    }

    int max_edge(int u, int v){
        int ans = 0;
        while(u != v){
            if (dat[u] > dat[v]) std::swap(u, v);
            ans = dat[u], u = parent_or_size[u];
            if(u < 0)return -1;
        }
        return ans;
    }

    private:
    int _n;
    // root node: -1 * component size
    // otherwise: parent
    std::vector<int> parent_or_size, dat;
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    vector<tuple<int,int,int>> a(N - 1);
    for(int i = 0; i + 1 < N; i++){
        int l, v;
        cin >> l >> v;
        v--;
        a[i] = make_tuple(l, v, i + 1);
    }
    sort(a.begin(), a.end());
    Kruskal_dsu uf(N);
    for(int i = 0; i + 1 < N; i++){
        int u, v, l;
        tie(l, u, v) = a[i];
        uf.merge(u, v, l);
    }

    int Q;
    cin >> Q;
    while(Q--){
        int cmd, x;
        cin >> cmd >> x;
        if(cmd == 1){
            cout << uf.size(0, x) << '\n';
        }else{
            x--;
            cout << uf.max_edge(0, x) << '\n';
        }
    }
}
0