結果

問題 No.901 K-ary εxtrεεmε
ユーザー dyktr_06dyktr_06
提出日時 2024-03-20 20:55:09
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 3,000 ms
コード長 7,835 bytes
コンパイル時間 2,765 ms
コンパイル使用メモリ 225,384 KB
実行使用メモリ 20,992 KB
最終ジャッジ日時 2024-09-30 06:46:59
合計ジャッジ時間 8,531 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 86 ms
20,992 KB
testcase_01 AC 2 ms
6,820 KB
testcase_02 AC 3 ms
6,816 KB
testcase_03 AC 3 ms
6,820 KB
testcase_04 AC 3 ms
6,816 KB
testcase_05 AC 3 ms
6,816 KB
testcase_06 AC 3 ms
6,816 KB
testcase_07 AC 131 ms
17,792 KB
testcase_08 AC 131 ms
17,792 KB
testcase_09 AC 128 ms
17,676 KB
testcase_10 AC 132 ms
17,792 KB
testcase_11 AC 133 ms
17,904 KB
testcase_12 AC 128 ms
17,860 KB
testcase_13 AC 127 ms
17,928 KB
testcase_14 AC 129 ms
17,920 KB
testcase_15 AC 132 ms
17,920 KB
testcase_16 AC 127 ms
17,920 KB
testcase_17 AC 137 ms
17,664 KB
testcase_18 AC 135 ms
17,792 KB
testcase_19 AC 135 ms
17,792 KB
testcase_20 AC 148 ms
17,792 KB
testcase_21 AC 134 ms
17,792 KB
testcase_22 AC 118 ms
18,048 KB
testcase_23 AC 114 ms
18,048 KB
testcase_24 AC 118 ms
18,176 KB
testcase_25 AC 116 ms
18,048 KB
testcase_26 AC 119 ms
18,152 KB
testcase_27 AC 87 ms
15,908 KB
testcase_28 AC 83 ms
15,876 KB
testcase_29 AC 91 ms
15,872 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();
        }
    }
};

template <typename T>
struct BinaryIndexedTree{
    int N;
    vector<T> BIT;
    BinaryIndexedTree(const int &N): N(N), BIT(N + 1, 0){
    }

    T get(int i){
        return sum(i + 1) - sum(i);
    }

    void add(int i, T x){
        i++;
        while(i <= N){
            BIT[i] += x;
            i += i & -i;
        }
    }

    T sum(int i) const {
        T ans = 0;
        while(i > 0){
            ans += BIT[i];
            i -= i & -i;
        }
        return ans;
    }

    T sum(int L, int R) const {
        return sum(R) - sum(L);
    }

    int lower_bound(T x) const {
        if(x <= 0){
            return 0;
        }else{
            int v = 0, r = 1;
            while(r < N) r = r << 1;
            for(int len = r; len > 0; len = len >> 1){
                if(v + len < N && BIT[v + len] < x){
                    x -= BIT[v + len];
                    v += len;
                }
            }
            return v;
        }
    }

    int upper_bound(T x) const {
        if(x < 0){
            return 0;
        }else{
            int v = 0, r = 1;
            while(r <= N) r = r << 1;
            for(int len = r; len > 0; len = len >> 1){
                if(v + len <= N && BIT[v + len] <= x){
                    x -= BIT[v + len];
                    v += len;
                }
            }
            return v;
        }
    }

    T operator [](int i) const {
        return sum(i, i + 1);
    }
};

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();

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

    long long ans = 0;
    auto query = [&](int l, int r){
        ans += BIT.sum(l, r);
    };
    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