結果
問題 | No.901 K-ary εxtrεεmε |
ユーザー | Haar |
提出日時 | 2019-10-04 22:43:22 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 322 ms / 3,000 ms |
コード長 | 6,416 bytes |
コンパイル時間 | 2,456 ms |
コンパイル使用メモリ | 221,752 KB |
実行使用メモリ | 26,496 KB |
最終ジャッジ日時 | 2024-10-04 06:24:49 |
合計ジャッジ時間 | 9,217 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 88 ms
26,496 KB |
testcase_01 | AC | 2 ms
5,248 KB |
testcase_02 | AC | 3 ms
5,248 KB |
testcase_03 | AC | 3 ms
5,248 KB |
testcase_04 | AC | 3 ms
5,248 KB |
testcase_05 | AC | 4 ms
5,248 KB |
testcase_06 | AC | 3 ms
5,248 KB |
testcase_07 | AC | 214 ms
23,040 KB |
testcase_08 | AC | 233 ms
22,912 KB |
testcase_09 | AC | 240 ms
22,912 KB |
testcase_10 | AC | 222 ms
23,040 KB |
testcase_11 | AC | 224 ms
22,892 KB |
testcase_12 | AC | 195 ms
22,912 KB |
testcase_13 | AC | 186 ms
22,912 KB |
testcase_14 | AC | 202 ms
22,912 KB |
testcase_15 | AC | 191 ms
22,784 KB |
testcase_16 | AC | 188 ms
22,940 KB |
testcase_17 | AC | 322 ms
22,912 KB |
testcase_18 | AC | 280 ms
22,912 KB |
testcase_19 | AC | 275 ms
22,784 KB |
testcase_20 | AC | 271 ms
22,784 KB |
testcase_21 | AC | 272 ms
23,036 KB |
testcase_22 | AC | 137 ms
22,912 KB |
testcase_23 | AC | 136 ms
22,912 KB |
testcase_24 | AC | 147 ms
22,912 KB |
testcase_25 | AC | 138 ms
22,912 KB |
testcase_26 | AC | 147 ms
22,912 KB |
testcase_27 | AC | 227 ms
22,912 KB |
testcase_28 | AC | 218 ms
22,912 KB |
testcase_29 | AC | 223 ms
22,912 KB |
ソースコード
#include <bits/stdc++.h> #define LLI long long int #define FOR(v, a, b) for(LLI v = (a); v < (b); ++v) #define FORE(v, a, b) for(LLI v = (a); v <= (b); ++v) #define REP(v, n) FOR(v, 0, n) #define REPE(v, n) FORE(v, 0, n) #define REV(v, a, b) for(LLI v = (a); v >= (b); --v) #define ALL(x) (x).begin(), (x).end() #define RALL(x) (x).rbegin(), (x).rend() #define ITR(it, c) for(auto it = (c).begin(); it != (c).end(); ++it) #define RITR(it, c) for(auto it = (c).rbegin(); it != (c).rend(); ++it) #define EXIST(c,x) ((c).find(x) != (c).end()) #define fst first #define snd second #define popcount __builtin_popcount #define UNIQ(v) (v).erase(unique(ALL(v)), (v).end()) #define bit(i) (1LL<<(i)) #ifdef DEBUG #include <misc/C++/Debug.cpp> #else #define dump(...) ((void)0) #endif #define gcd __gcd using namespace std; template <class T> constexpr T lcm(T m, T n){return m/gcd(m,n)*n;} template <typename I> void join(ostream &ost, I s, I t, string d=" "){for(auto i=s; i!=t; ++i){if(i!=s)ost<<d; ost<<*i;}ost<<endl;} template <typename T> istream& operator>>(istream &is, vector<T> &v){for(auto &a : v) is >> a; return is;} template <typename T, typename U> bool chmin(T &a, const U &b){return (a>b ? a=b, true : false);} template <typename T, typename U> bool chmax(T &a, const U &b){return (a<b ? a=b, true : false);} template <typename T, size_t N, typename U> void fill_array(T (&a)[N], const U &v){fill((U*)a, (U*)(a+N), v);} struct Init{ Init(){ cin.tie(0); ios::sync_with_stdio(false); } }init; template <typename Cost = int> class Edge{ public: int from,to; Cost cost; Edge() {} Edge(int to, Cost cost): to(to), cost(cost){} Edge(int from, int to, Cost cost): from(from), to(to), cost(cost){} Edge rev() const {return Edge(to,from,cost);} friend ostream& operator<<(ostream &os, const Edge &e){ os << "(FROM: " << e.from << "," << "TO: " << e.to << "," << "COST: " << e.cost << ")"; return os; } }; template <typename T> using Graph = vector<vector<Edge<T>>>; template <typename T> using Tree = vector<vector<Edge<T>>>; template <typename C, typename T> void add_edge(C &g, int from, int to, T w){ g[from].push_back(Edge<T>(from, to, w)); } template <typename C, typename T> void add_undirected(C &g, int a, int b, T w){ g[a].push_back(Edge<T>(a, b, w)); g[b].push_back(Edge<T>(b, a, w)); } template <typename T> class HLDecomposition{ Tree<T> tree; int n; vector<int> sub, // subtree size par, // parent id head, // chain head id id, // id[original id] = hld id rid, // rid[hld id] = original id next, // next node in a chain end; // int dfs_sub(int cur, int p){ par[cur] = p; int t = 0; for(auto &e : tree[cur]){ if(e.to == p) continue; sub[cur] += dfs_sub(e.to, cur); if(sub[e.to] > t){ t = sub[e.to]; next[cur] = e.to; swap(e, tree[cur][0]); } } return sub[cur]; } void dfs_build(int cur, int &i){ id[cur] = i; rid[i] = cur; ++i; for(auto &e : tree[cur]){ if(e.to == par[cur]) continue; head[e.to] = (e.to == tree[cur][0].to ? head[cur] : e.to); dfs_build(e.to, i); } end[cur] = i; } public: HLDecomposition(const Tree<T> &tree): tree(tree), n(tree.size()), sub(n,1), par(n,-1), head(n), id(n), rid(n), next(n,-1), end(n, -1){ dfs_sub(0, -1); int i=0; dfs_build(0, i); } void path_query_vertex(int x, int y, const function<void(int,int)> &f){ while(1){ if(id[x] > id[y]) swap(x, y); f(max(id[head[y]], id[x]), id[y]+1); if(head[x] == head[y]) return; y = par[head[y]]; } } void path_query_edge(int x, int y, const function<void(int,int)> &f){ while(1){ if(id[x] > id[y]) swap(x, y); if(head[x] == head[y]){ if(x != y) f(id[x]+1, id[y]+1); return; } f(id[head[y]], id[y]+1); y = par[head[y]]; } } void subtree_query_edge(int x, const function<void(int,int)> &f){ f(id[x]+1, end[x]); } void subtree_query_vertex(int x, const function<void(int,int)> &f){ f(id[x], end[x]); } int get_edge_id(int u, int v){ // 辺に対応するid if(par[u] == v){ return id[u]; }else if(par[v] == u){ return id[v]; } return -1; } int parent(int x){return par[x];}; int lca(int u, int v){ while(1){ if(id[u] > id[v]) swap(u, v); if(head[u] == head[v]) return u; v = par[head[v]]; } } int get_id(int x){ return id[x]; } }; template <typename T> class SegmentTreeRangeSumQuery{ private: int size; vector<T> vec; T e; inline T aux(int x, int y, int i, int l, int r){ if(r<=x || y<=l) return e; else if(x<=l && r<=y) return vec[i]; else return aux(x,y,i*2+1,l,(l+r)/2) + aux(x,y,i*2+2,(l+r)/2,r); }; public: SegmentTreeRangeSumQuery(int n, const T &e): e(e){ size = 1; while(size < n) size *= 2; size = size*2-1; vec = vector<T>(size, e); } inline void update(int i, const T &x){ int j = i+(size+1)/2-1; vec[j] = x; --j; while(j>=0){ vec[j/2] = vec[(j/2)*2+1] + vec[(j/2)*2+2]; (j /= 2) -= 1; } } inline T at(int i){ return vec[(size+1)/2 + i - 1]; } inline T get(int x, int y){ // [x,y) return aux(x,y,0,0,(size+1)/2); } }; int preorder[100100]; void dfs(int cur, int par, const Tree<LLI> &tree, int &i){ preorder[cur] = i; ++i; for(auto &e : tree[cur]){ if(e.to == par) continue; dfs(e.to, cur, tree, i); } } int main(){ int N; cin >> N; Tree<LLI> tree(N); REP(i,N-1){ int u,v; cin >> u >> v; LLI w; cin >> w; add_undirected(tree, u, v, w); } HLDecomposition<LLI> hld(tree); SegmentTreeRangeSumQuery<LLI> seg(N, 0); REP(i,N){ for(auto &e : tree[i]){ if(e.from < e.to){ seg.update(hld.get_edge_id(e.from, e.to), e.cost); } } } { fill_array(preorder, -1); int i = 0; dfs(0, -1, tree, i); } int Q; cin >> Q; while(Q--){ int k; cin >> k; vector<int> x(k); cin >> x; sort(ALL(x), [&](int i, int j){return preorder[i] < preorder[j];}); dump(x); LLI ans = 0; auto f = [&](int p, int q){ ans += seg.get(p,q); }; REP(i,k){ hld.path_query_edge(x[i], x[(i+1)%k], f); } ans /= 2; cout << ans << endl; } return 0; }