結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | dyktr_06 |
提出日時 | 2024-03-20 20:57:24 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 122 ms / 3,000 ms |
コード長 | 6,462 bytes |
コンパイル時間 | 3,157 ms |
コンパイル使用メモリ | 225,112 KB |
実行使用メモリ | 21,888 KB |
最終ジャッジ日時 | 2024-09-30 06:47:08 |
合計ジャッジ時間 | 7,224 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 79 ms
21,888 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 2 ms
5,248 KB |
testcase_03 | AC | 2 ms
5,248 KB |
testcase_04 | AC | 3 ms
5,248 KB |
testcase_05 | AC | 3 ms
5,248 KB |
testcase_06 | AC | 2 ms
5,248 KB |
testcase_07 | AC | 117 ms
18,560 KB |
testcase_08 | AC | 112 ms
18,688 KB |
testcase_09 | AC | 113 ms
18,688 KB |
testcase_10 | AC | 118 ms
18,688 KB |
testcase_11 | AC | 121 ms
18,688 KB |
testcase_12 | AC | 118 ms
18,688 KB |
testcase_13 | AC | 116 ms
18,688 KB |
testcase_14 | AC | 116 ms
18,688 KB |
testcase_15 | AC | 117 ms
18,560 KB |
testcase_16 | AC | 114 ms
18,688 KB |
testcase_17 | AC | 119 ms
18,688 KB |
testcase_18 | AC | 122 ms
18,688 KB |
testcase_19 | AC | 115 ms
18,688 KB |
testcase_20 | AC | 114 ms
18,688 KB |
testcase_21 | AC | 114 ms
18,688 KB |
testcase_22 | AC | 113 ms
18,816 KB |
testcase_23 | AC | 111 ms
18,944 KB |
testcase_24 | AC | 114 ms
18,944 KB |
testcase_25 | AC | 110 ms
18,944 KB |
testcase_26 | AC | 113 ms
18,944 KB |
testcase_27 | AC | 85 ms
16,640 KB |
testcase_28 | AC | 85 ms
16,640 KB |
testcase_29 | AC | 84 ms
16,640 KB |
ソースコード
#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"; } }