結果
| 問題 |
No.901 K-ary εxtrεεmε
|
| コンテスト | |
| ユーザー |
tko919
|
| 提出日時 | 2022-10-19 23:50:35 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 377 ms / 3,000 ms |
| コード長 | 8,701 bytes |
| コンパイル時間 | 3,028 ms |
| コンパイル使用メモリ | 222,744 KB |
| 最終ジャッジ日時 | 2025-02-08 08:25:06 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 29 |
ソースコード
#line 1 "library/Template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define ALL(v) (v).begin(),(v).end()
using ll=long long int;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
template<typename T>inline bool chmax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T>inline bool chmin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
#line 2 "library/Utility/fastio.hpp"
#include <unistd.h>
class FastIO{
static constexpr int L=1<<16;
char rdbuf[L];
int rdLeft=0,rdRight=0;
inline void reload(){
int len=rdRight-rdLeft;
memmove(rdbuf,rdbuf+rdLeft,len);
rdLeft=0,rdRight=len;
rdRight+=fread(rdbuf+len,1,L-len,stdin);
}
inline bool skip(){
for(;;){
while(rdLeft!=rdRight and rdbuf[rdLeft]<=' ')rdLeft++;
if(rdLeft==rdRight){
reload();
if(rdLeft==rdRight)return false;
}
else break;
}
return true;
}
template<typename T,enable_if_t<is_integral<T>::value,int> =0>inline bool _read(T& x){
if(!skip())return false;
if(rdLeft+20>=rdRight)reload();
bool neg=false;
if(rdbuf[rdLeft]=='-'){
neg=true;
rdLeft++;
}
x=0;
while(rdbuf[rdLeft]>='0' and rdLeft<rdRight){
x=x*10+(neg?-(rdbuf[rdLeft++]^48):(rdbuf[rdLeft++]^48));
}
return true;
}
template<typename T,enable_if_t<is_floating_point<T>::value,int> =0>inline bool _read(T& x){
if(!skip())return false;
if(rdLeft+20>=rdRight)reload();
bool neg=false;
if(rdbuf[rdLeft]=='-'){
neg=true;
rdLeft++;
}
x=0;
while(rdbuf[rdLeft]>='0' and rdbuf[rdLeft]<='9' and rdLeft<rdRight){
x=x*10+(rdbuf[rdLeft++]^48);
}
if(rdbuf[rdLeft]!='.')return true;
rdLeft++;
T base=.1;
while(rdbuf[rdLeft]>='0' and rdbuf[rdLeft]<='9' and rdLeft<rdRight){
x+=base*(rdbuf[rdLeft++]^48);
base*=.1;
}
if(neg)x=-x;
return true;
}
inline bool _read(char& x){
if(!skip())return false;
if(rdLeft+1>=rdRight)reload();
x=rdbuf[rdLeft++];
return true;
}
inline bool _read(string& x){
if(!skip())return false;
for(;;){
int pos=rdLeft;
while(pos<rdRight and rdbuf[pos]>' ')pos++;
x.append(rdbuf+rdLeft,pos-rdLeft);
if(rdLeft==pos)break;
rdLeft=pos;
if(rdLeft==rdRight)reload();
else break;
}
return true;
}
template<typename T>inline bool _read(vector<T>& v){
for(auto& x:v){
if(!_read(x))return false;
}
return true;
}
char wtbuf[L],tmp[50];
int wtRight=0;
inline void flush(){
fwrite(wtbuf,1,wtRight,stdout);
wtRight=0;
}
inline void _write(const char& x){
if(wtRight>L-32)flush();
wtbuf[wtRight++]=x;
}
inline void _write(const string& x){
for(auto& c:x)_write(c);
}
template<typename T,enable_if_t<is_integral<T>::value,int> =0>inline void _write(T x){
if(wtRight>L-32)flush();
if(x==0){
_write('0');
return;
}
else if(x<0){
_write('-');
if (__builtin_expect(x == std::numeric_limits<T>::min(), 0)) {
switch (sizeof(x)) {
case 2: _write("32768"); return;
case 4: _write("2147483648"); return;
case 8: _write("9223372036854775808"); return;
}
}
x=-x;
}
int pos=0;
while(x!=0){
tmp[pos++]=char((x%10)|48);
x/=10;
}
rep(i,0,pos)wtbuf[wtRight+i]=tmp[pos-1-i];
wtRight+=pos;
}
template<typename T>inline void _write(const vector<T>& v){
rep(i,0,v.size()){
if(i)_write(' ');
_write(v[i]);
}
}
public:
FastIO(){}
~FastIO(){flush();}
inline void read(){}
template <typename Head, typename... Tail>inline void read(Head& head,Tail&... tail){
assert(_read(head));
read(tail...);
}
template<bool ln=true,bool space=false>inline void write(){if(ln)_write('\n');}
template <bool ln=true,bool space=false,typename Head, typename... Tail>inline void write(const Head& head,const Tail&... tail){
if(space)_write(' ');
_write(head);
write<ln,true>(tail...);
}
};
/**
* @brief Fast IO
*/
#line 3 "sol.cpp"
#line 2 "library/Graph/lca.hpp"
struct LCA{
LCA(int _n=0):n(_n),g(_n),depth(_n+1,inf),start(_n){}
void add_edge(int u,int v){
g[u].push_back(v);
g[v].push_back(u);
}
void run(int root=0){
depth[root]=0;
dfs(root,-1);
N=1;
while(N<int(euler.size()))N<<=1;
tree.resize(N*2,n);
rep(i,0,euler.size())tree[N+i]=euler[i];
for(int i=N-1;i>0;i--)tree[i]=op(tree[i*2],tree[i*2+1]);
}
int lca(int u,int v){
int a=start[u],b=start[v];
if(a>b)swap(a,b);
b++;
int res=n;
for(int T=b-a;T>=1;T=b-a){
int x=a|((1U<<31)>>__builtin_clz(T));
int y=x&-x,k=__builtin_ctz(x);
res=op(res,tree[(N|a)>>k]);
a+=y;
}
return res;
}
private:
int n,N;
vector<vector<int>> g;
vector<int> depth,start,euler,tree;
void dfs(int v,int p){
start[v]=euler.size();
euler.push_back(v);
for(auto& to:g[v])if(to!=p){
depth[to]=depth[v]+1;
dfs(to,v);
euler.push_back(v);
}
}
int op(int u,int v){
if(depth[u]<depth[v])return u;
else return v;
}
};
/**
* @brief Lowest Common Ancestor
*/
#line 2 "library/Graph/auxiliarytree.hpp"
#line 4 "library/Graph/auxiliarytree.hpp"
struct AuxiliaryTree{
int n,pos;
LCA lca;
vector<int> in,dep;
vector<vector<int>> _g,g;
AuxiliaryTree(int _n):n(_n),pos(0),lca(n),in(n),dep(n),_g(n),g(n){}
void add_edge(int u,int v){
lca.add_edge(u,v);
_g[u].push_back(v);
_g[v].push_back(u);
}
void run(int root=0){
lca.run(root);
dfs(root,-1);
}
void query(vector<int>& vs){
sort(ALL(vs),[&](int u,int v){return in[u]<in[v];});
vs.erase(unique(ALL(vs)),vs.end());
int m=vs.size();
stack<int> st;
st.push(vs[0]);
rep(i,0,m-1){
int w=lca.lca(vs[i],vs[i+1]);
if(w!=vs[i]){
int cur=st.top();
st.pop();
while(!st.empty() and dep[w]<dep[st.top()]){
add(st.top(),cur);
cur=st.top();
st.pop();
}
if(st.empty() or st.top()!=w){
st.push(w);
vs.push_back(w);
}
add(w,cur);
}
st.push(vs[i+1]);
}
while(st.size()>1){
int c=st.top();
st.pop();
add(st.top(),c);
}
}
void clear(vector<int>& vs){
for(auto& w:vs)g[w].clear();
}
private:
void dfs(int v,int p){
in[v]=pos++;
for(auto& to:_g[v])if(to!=p){
dep[to]=dep[v]+1;
dfs(to,v);
}
}
void add(int u,int v){
g[u].push_back(v);
g[v].push_back(u);
}
};
/**
* @brief Auxiliary Tree(Virtual Tree)
*/
#line 6 "sol.cpp"
FastIO io;
int main(){
int n;
io.read(n);
AuxiliaryTree g(n);
LCA lca(n);
using P=pair<int,int>;
vector tree(n,vector<P>());
rep(_,0,n-1){
int u,v,w;
io.read(u,v,w);
g.add_edge(u,v);
lca.add_edge(u,v);
tree[u].push_back({v,w});
tree[v].push_back({u,w});
}
g.run();
lca.run();
vector<ll> dep(n);
auto dfs=[&](auto& dfs,int v,int p)->void{
for(auto& [to,w]:tree[v])if(to!=p){
dep[to]=dep[v]+w;
dfs(dfs,to,v);
}
};
dfs(dfs,0,-1);
auto dist=[&](int u,int v)->ll{
return dep[u]+dep[v]-2*dep[lca.lca(u,v)];
};
int q;
io.read(q);
while(q--){
int k;
io.read(k);
vector<int> vs(k);
io.read(vs);
g.query(vs);
ll ret=0;
for(auto& v:vs)for(auto& to:g.g[v])ret+=dist(v,to);
ret/=2;
io.write(ret);
g.clear(vs);
}
return 0;
}
tko919