結果
問題 |
No.901 K-ary εxtrεεmε
|
ユーザー |
![]() |
提出日時 | 2020-09-15 23:14:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 212 ms / 3,000 ms |
コード長 | 3,703 bytes |
コンパイル時間 | 2,444 ms |
コンパイル使用メモリ | 223,144 KB |
最終ジャッジ日時 | 2025-01-14 15:07:39 |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 29 |
ソースコード
#define PROBLEM "https://yukicoder.me/problems/3407" #include<bits/stdc++.h> using namespace std; #define call_from_test #ifndef call_from_test #include<bits/stdc++.h> using namespace std; #endif //BEGIN CUT HERE struct LowestCommonAncestor{ int n,h; vector< vector<int> > G,par; vector<int> dep; LowestCommonAncestor(){} LowestCommonAncestor(int n):n(n),G(n),dep(n){ h=1; while((1<<h)<=n) h++; par.assign(h,vector<int>(n,-1)); } void add_edge(int u,int v){ G[u].emplace_back(v); G[v].emplace_back(u); } void dfs(int v,int p,int d){ par[0][v]=p; dep[v]=d; for(int u:G[v]) if(u!=p) dfs(u,v,d+1); } void build(int r=0){ dfs(r,-1,0); for(int k=0;k+1<h;k++) for(int v=0;v<n;v++) if(~par[k][v]) par[k+1][v]=par[k][par[k][v]]; } int lca(int u,int v){ if(dep[u]>dep[v]) swap(u,v); for(int k=0;k<h;k++) if((dep[v]-dep[u])>>k&1) v=par[k][v]; if(u==v) return u; for(int k=h-1;k>=0;k--) if(par[k][u]!=par[k][v]) u=par[k][u],v=par[k][v]; return par[0][u]; } int distance(int u,int v){ return dep[u]+dep[v]-dep[lca(u,v)]*2; } }; //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #ifndef call_from_test #include <bits/stdc++.h> using namespace std; #define call_from_test #include "lowestcommonancestor.cpp" #undef call_from_test #endif //BEGIN CUT HERE struct AuxiliaryTree : LowestCommonAncestor{ using super = LowestCommonAncestor; vector<int> idx; vector<vector<int>> T; AuxiliaryTree(){} AuxiliaryTree(int n):super(n),idx(n),T(n){} void dfs(int v,int p,int &pos){ idx[v]=pos++; for(int u:G[v]) if(u!=p) dfs(u,v,pos); } void build(int r=0){ super::build(r); int pos=0; dfs(r,-1,pos); } void add_aux_edge(int u,int v){ T[u].emplace_back(v); T[v].emplace_back(u); } using super::lca, super::dep; void query(vector<int> &vs){ assert(!vs.empty()); sort(vs.begin(),vs.end(), [&](int a,int b){return idx[a]<idx[b];}); vs.erase(unique(vs.begin(),vs.end()),vs.end()); int k=vs.size(); stack<int> st; st.emplace(vs[0]); for(int i=0;i+1<k;i++){ int w=lca(vs[i],vs[i+1]); if(w!=vs[i]){ int l=st.top();st.pop(); while(!st.empty()&&dep[w]<dep[st.top()]){ add_aux_edge(st.top(),l); l=st.top();st.pop(); } if(st.empty()||st.top()!=w){ st.emplace(w); vs.emplace_back(w); } add_aux_edge(w,l); } st.emplace(vs[i+1]); } while(st.size()>1){ int c=st.top();st.pop(); add_aux_edge(st.top(),c); } } void clear(const vector<int> &ws){ for(int w:ws) T[w].clear(); } }; //END CUT HERE #ifndef call_from_test signed main(){ return 0; } #endif #undef call_from_test signed main(){ cin.tie(0); ios::sync_with_stdio(0); int n; cin>>n; AuxiliaryTree G(n); vector<map<int, int>> ws(n); for(int i=1;i<n;i++){ int u,v,w; cin>>u>>v>>w; G.add_edge(u,v); ws[u][v]=ws[v][u]=w; } G.build(); using ll = long long; vector<ll> dep(n,-1); queue<int> que; dep[0]=0; que.emplace(0); while(!que.empty()){ int v=que.front();que.pop(); for(int u:G.G[v]){ if(~dep[u]) continue; dep[u]=dep[v]+ws[v][u]; que.emplace(u); } } int q; cin>>q; for(int i=0;i<q;i++){ int k; cin>>k; vector<int> vs(k); for(int j=0;j<k;j++) cin>>vs[j]; G.query(vs); ll ans=0; for(int v:vs) for(int u:G.T[v]) ans+=max(dep[u]-dep[v],0LL); cout<<ans<<"\n"; G.clear(vs); } cout<<flush; return 0; }