結果

問題 No.901 K-ary εxtrεεmε
ユーザー beet
提出日時 2020-03-05 18:13:03
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 457 ms / 3,000 ms
コード長 4,601 bytes
コンパイル時間 4,281 ms
コンパイル使用メモリ 224,728 KB
最終ジャッジ日時 2025-01-09 04:33:43
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#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 FastIO{
FastIO(){
cin.tie(0);
ios::sync_with_stdio(0);
}
}fastio_beet;
//END CUT HERE
#ifndef call_from_test
signed main(){
return 0;
}
#endif
#ifndef call_from_test
#include<bits/stdc++.h>
using namespace std;
#endif
//BEGIN CUT HERE
class EulerTourForVertex{
private:
vector<int> ls,rs;
int pos;
void dfs(int v,int p){
ls[v]=pos++;
for(int u:G[v])
if(u!=p) dfs(u,v);
rs[v]=pos;
}
public:
vector< vector<int> > G;
EulerTourForVertex(){}
EulerTourForVertex(int n):ls(n),rs(n),G(n){}
void add_edge(int u,int v){
G[u].emplace_back(v);
G[v].emplace_back(u);
}
void build(int r=0){
pos=0;
dfs(r,-1);
}
int idx(int v){return ls[v];}
template<typename F>
void exec(int v,F f){
f(ls[v],rs[v]);
}
};
//END CUT HERE
#ifndef call_from_test
signed main(){
return 0;
}
#endif
#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 "eulertourforvertex.cpp"
#include "lowestcommonancestor.cpp"
#undef call_from_test
#endif
/**
* @see https://smijake3.hatenablog.com/entry/2019/09/15/200200
*/
//BEGIN CUT HERE
struct AuxiliaryTree : EulerTourForVertex{
using super = EulerTourForVertex;
LowestCommonAncestor lca;
vector<vector<int>> T;
AuxiliaryTree(){}
AuxiliaryTree(int n):super(n),lca(n),T(n){}
void build(int r=0){
super::build(r);
lca.G=super::G;
lca.build(r);
}
void add_aux_edge(int u,int v){
T[u].emplace_back(v);
T[v].emplace_back(u);
}
using super::idx;
void query(vector<int> &vs){
assert(!vs.empty());
sort(vs.begin(),vs.end(),
[&](int a,int b){return idx(a)<idx(b);});
map<int, vector<int>> H;
int k=vs.size();
stack<int> st;
st.emplace(vs[0]);
for(int i=0;i+1<k;i++){
int w=lca.lca(vs[i],vs[i+1]);
if(w!=vs[i]){
int l=st.top();st.pop();
while(!st.empty()&&lca.dep[w]<lca.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(){
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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0