結果

問題 No.901 K-ary εxtrεεmε
ユーザー dyktr_06dyktr_06
提出日時 2024-03-20 20:57:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 3,000 ms
コード長 6,462 bytes
コンパイル時間 3,049 ms
コンパイル使用メモリ 226,416 KB
実行使用メモリ 22,016 KB
最終ジャッジ日時 2024-03-20 20:57:34
合計ジャッジ時間 9,025 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
22,016 KB
testcase_01 AC 2 ms
6,548 KB
testcase_02 AC 3 ms
6,548 KB
testcase_03 AC 3 ms
6,548 KB
testcase_04 AC 3 ms
6,548 KB
testcase_05 AC 3 ms
6,548 KB
testcase_06 AC 3 ms
6,548 KB
testcase_07 AC 136 ms
18,816 KB
testcase_08 AC 172 ms
18,816 KB
testcase_09 AC 144 ms
18,816 KB
testcase_10 AC 143 ms
18,816 KB
testcase_11 AC 139 ms
18,816 KB
testcase_12 AC 143 ms
18,816 KB
testcase_13 AC 134 ms
18,816 KB
testcase_14 AC 138 ms
18,816 KB
testcase_15 AC 140 ms
18,816 KB
testcase_16 AC 151 ms
18,816 KB
testcase_17 AC 147 ms
18,816 KB
testcase_18 AC 149 ms
18,816 KB
testcase_19 AC 144 ms
18,816 KB
testcase_20 AC 150 ms
18,688 KB
testcase_21 AC 161 ms
18,816 KB
testcase_22 AC 148 ms
19,072 KB
testcase_23 AC 152 ms
19,072 KB
testcase_24 AC 144 ms
19,072 KB
testcase_25 AC 142 ms
19,072 KB
testcase_26 AC 143 ms
19,072 KB
testcase_27 AC 116 ms
16,768 KB
testcase_28 AC 110 ms
16,768 KB
testcase_29 AC 110 ms
16,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

class HeavyLightDecomposition{
protected:
    int V;
    vector<vector<int>> G;
    vector<int> stsize, parent, pathtop, depth, in, reverse_in, out;
    int root;

private:
    // Subtree Size
    void buildStsize(int curr, int prev){
        stsize[curr] = 1, parent[curr] = prev;
        for(int &v : G[curr]){
            if(v == prev){
                if(v == G[curr].back()) break;
                else swap(v, G[curr].back());
            }
            buildStsize(v, curr);
            stsize[curr] += stsize[v];
            if(stsize[v] > stsize[G[curr][0]]){
                swap(v, G[curr][0]);
            }
        }
    }

    void buildPath(int curr, int prev, int &t){
        in[curr] = t++;
        reverse_in[in[curr]] = curr;
        for(int v : G[curr]){
            if(v == prev) continue;

            if(v == G[curr][0]){
                pathtop[v] = pathtop[curr];
            } else{
                pathtop[v] = v;
            }
            depth[v] = depth[curr] + 1;
            buildPath(v, curr, t);
        }
        out[curr] = t;
    }

public:
    HeavyLightDecomposition(int node_size) : V(node_size), G(V), stsize(V, 0), parent(V, -1),
        pathtop(V, -1), depth(V, 0), in(V, -1), reverse_in(V, -1), out(V, -1){}

    void add_edge(int u, int v){
        G[u].push_back(v);
        G[v].push_back(u);
    }

    void build(int _root = 0){
        root = _root;
        int t = 0;
        buildStsize(root, -1);
        pathtop[root] = root;
        buildPath(root, -1, t);
    }

    inline int get(int a){
        return in[a];
    }

    int la(int a, int k) {
        while(true){
            int u = pathtop[a];
            if(in[a] - k >= in[u]) return reverse_in[in[a] - k];
            k -= in[a] - in[u] + 1;
            a = parent[u];
        }
    }

    int lca(int a, int b){
        int pa = pathtop[a], pb = pathtop[b];
        while(pathtop[a] != pathtop[b]){
            if(in[pa] > in[pb]){
                a = parent[pa], pa = pathtop[a];
            } else{
                b = parent[pb], pb = pathtop[b];
            }
        }
        if(in[a] > in[b]) swap(a, b);
        return a;
    }

    int dist(int a, int b){ return depth[a] + depth[b] - 2 * depth[lca(a, b)]; }

    int jump(int from, int to, int k) {
        if(!k) return from;
        int l = lca(from, to);
        int d = dist(from, to);
        if(d < k) return -1;
        if(depth[from] - depth[l] >= k) return la(from, k);
        k -= depth[from] - depth[l];
        return la(to, depth[to] - depth[l] - k);
    }

    void subtree_query(int a, const function<void(int, int)> &func){
        func(in[a], out[a]);
    }

    void path_query(int a, int b, const function<void(int, int)> &func, bool include_root = true, bool reverse_path = false){
        vector<pair<int, int>> path;
        int pa = pathtop[a], pb = pathtop[b];
        while(pathtop[a] != pathtop[b]){
            if(in[pa] > in[pb]){
                path.emplace_back(in[pa], in[a] + 1);
                a = parent[pa], pa = pathtop[a];
            } else{
                path.emplace_back(in[pb], in[b] + 1);
                b = parent[pb], pb = pathtop[b];
            }
        }
        if(in[a] > in[b]) swap(a, b);

        if(include_root) path.emplace_back(in[a], in[b] + 1);
        else path.emplace_back(in[a] + 1, in[b] + 1);

        if(!reverse_path) reverse(path.begin(), path.end());
        else for(auto &p : path) p = make_pair(V - p.second, V - p.first);

        for(auto [u, v] : path){
            func(u, v);
        }
    }

    void path_noncommutative_query(int a, int b, const function<void(int, int)> &func, const function<void(int, int)> &func2){
        int l = lca(a, b);
        path_query(a, l, func2, false, true);
        path_query(l, b, func, true, false);
    }
};

struct AuxiliaryTree : HeavyLightDecomposition{
    using super = HeavyLightDecomposition;

private:
    void add_aux_edge(int u, int v){
        T[u].emplace_back(v);
        T[v].emplace_back(u);
    }

public:
    vector<vector<int>> T;
    AuxiliaryTree(int n) : super(n), T(n){}

    void build(int _root = 0){
        super::build(_root);
    }

    void query(vector<int> &vs){
        assert(!vs.empty());
        sort(vs.begin(), vs.end(),
            [&](int a, int b){ return in[a] < in[b]; });
        vs.erase(unique(vs.begin(), vs.end()), vs.end());

        int k = vs.size();
        vector<int> st;
        st.emplace_back(vs[0]);
        for(int i = 0; i + 1 < k; i++){
            int p = lca(vs[i], vs[i + 1]);
            if(p != vs[i]){
                int l = st.back(); st.pop_back();
                while(!st.empty() && depth[p] < depth[st.back()]){
                    add_aux_edge(st.back(), l);
                    l = st.back(); st.pop_back();
                }
                if(st.empty() || st.back() != p){
                    st.emplace_back(p);
                    vs.emplace_back(p);
                }
                add_aux_edge(p, l);
            }
            st.emplace_back(vs[i + 1]);
        }

        while(st.size() > 1){
            int c = st.back(); st.pop_back();
            add_aux_edge(st.back(), c);
        }
    }

    void clear(const vector<int> &ws){
        for(int w : ws){
            T[w].clear();
        }
    }
};

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

    int n; cin >> n;
    AuxiliaryTree G(n);
    vector<int> u(n - 1), v(n - 1), w(n - 1);
    for(int i = 0; i < n - 1; i++){
        cin >> u[i] >> v[i] >> w[i];
        G.add_edge(u[i], v[i]);
    }
    G.build();

    vector<long long> a(n);
    for(int i = 0; i < n - 1; i++){
        int idx = max(G.get(u[i]), G.get(v[i]));
        a[idx] = w[i];
    }
    vector<long long> cs(n + 1);
    for(int i = 0; i < n; i++){
        cs[i + 1] = cs[i] + a[i];
    }

    long long ans = 0;
    auto query = [&](int l, int r){
        ans += cs[r] - cs[l];
    };
    auto dfs = [&](auto self, int curr, int prev) -> void {
        for(auto v : G.T[curr]){
            if(v == prev) continue;
            G.path_query(curr, v, query, false, false);
            self(self, v, curr);
        }
    };

    int q; cin >> q;
    while(q--){
        int k; cin >> k;
        vector<int> x(k);
        for(int i = 0; i < k; i++) cin >> x[i];
        G.query(x);
        ans = 0;
        dfs(dfs, x[0], -1);
        G.clear(x);
        cout << ans << "\n";
    }
}
0