#ifdef t9unkubj #include"template.h" //#include"template_no_debug.h" #else #undef _GLIBCXX_DEBUG #pragma GCC optimize("O3") #define dbg(...) 199958 using namespace std; #include using uint=unsigned; using ll=long long; using ull=unsigned long long; using ld=long double; using pii=pair; using pll=pair; templateusing vc=vector; templateusing vvc=vc>; templateusing vvvc=vvc>; using vi=vc; using vvi=vc; using vvvi=vc; using vl=vc; using vvl=vc; using vvvl=vc; templateusing smpq=priority_queue,greater>; templateusing bipq=priority_queue; #define rep(i,n) for(ll i=0;i<(ll)(n);i++) #define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++) #define DREP(i,n,m) for(ll i=(n);i>=(m);i--) #define drep(i,n) for(ll i=((n)-1);i>=0;i--) #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define mp make_pair #define pb push_back #define eb emplace_back #define fi first #define se second #define is insert #define bg begin() #define ed end() void scan(int&a) { cin >> a; } void scan(ll&a) { cin >> a; } void scan(string&a) { cin >> a; } void scan(char&a) { cin >> a; } void scan(uint&a) { cin >> a; } void scan(ull&a) { cin >> a; } void scan(bool&a) { cin >> a; } void scan(ld&a){ cin>> a;} template void scan(vector&a) { for(auto&x:a) scan(x); } void read() {} template void read(Head&head, Tail&... tail) { scan(head); read(tail...); } #define INT(...) int __VA_ARGS__; read(__VA_ARGS__); #define LL(...) ll __VA_ARGS__; read(__VA_ARGS__); #define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__); #define STR(...) string __VA_ARGS__; read(__VA_ARGS__); #define CHR(...) char __VA_ARGS__; read(__VA_ARGS__); #define DBL(...) double __VA_ARGS__; read(__VA_ARGS__); #define LD(...) ld __VA_ARGS__; read(__VA_ARGS__); #define VC(type, name, ...) vector name(__VA_ARGS__); read(name); #define VVC(type, name, size, ...) vector> name(size, vector(__VA_ARGS__)); read(name); void print(int a) { cout << a; } void print(ll a) { cout << a; } void print(string a) { cout << a; } void print(char a) { cout << a; } void print(uint a) { cout << a; } void print(bool a) { cout << a; } void print(ull a) { cout << a; } void print(double a) { cout << a; } void print(ld a){ cout<< a; } template void print(vectora) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout< void PRT(T a) { print(a); cout < void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; } template bool chmin(T &x, F y){ if(x>y){ x=y; return true; } return false; } template bool chmax(T &x, F y){ if(x int popcount(T n){ return __builtin_popcountll(n); } template T sum(vc&a){ return accumulate(all(a),T(0)); } template T max(vc&a){ return *max_element(all(a)); } template T min(vc&a){ return *min_element(all(a)); } template void unique(vc&a){ a.erase(unique(all(a)),a.end()); } vvi readgraph(int n,int m,int off = -1){ vvi g(n); rep(i, m){ int u,v; cin>>u>>v; u+=off,v+=off; g[u].push_back(v); g[v].push_back(u); } return g; } vvi readtree(int n,int off=-1){ return readgraph(n,n-1,off); } template vc presum(vc &a){ vc ret(a.size()+1); rep(i,a.size())ret[i+1]=ret[i]+a[i]; return ret; } template vc &operator+=(vc &a,F b){ for (auto&v:a)v += b; return a; } template vc &operator-=(vc&a,F b){ for (auto&v:a)v-=b; return a; } template vc &operator*=(vc&a,F b){ for (auto&v:a)v*=b; return a; } #endif double pass_time=0; #include #include #include struct HLD{ int n; std::vectordepth,head,siz_,par,in,out; std::vector>g,c; HLD(int n):n(n),depth(n),head(n),siz_(n,1),in(n),out(n),g(n),c(n){} HLD(int n,std::vector>g):n(n),depth(n),head(n),siz_(n,1),in(n),out(n),g(g),c(n){} void add_edge(int u,int v,bool d=0){ g[u].emplace_back(v); if(d)g[v].emplace_back(u); } void init_dfs1(int now,int pre){ for(int &x:g[now]){ if(x==pre)continue; c[now].emplace_back(x); depth[x]=depth[now]+1; par[x]=now; init_dfs1(x,now); siz_[now]+=siz_[x]; } } void init_dfs2(int now,int pre,int&order){ in[now]=order++; for(int i=0;i<(int)c[now].size();i++){ if(siz_[c[now][0]](n,root); int order{}; init_dfs1(root,-1); init_dfs2(root,-1,order); } int lca(int u,int v){//O(logN) u,vのlcaを取得する if(head[u]==head[v])return (depth[u]>depth[v]?v:u); if(depth[head[u]]>depth[head[v]])std::swap(u,v); v=par[head[v]]; return lca(u,v); } int f1(int a){//入った時間を返すO(1) return in[a]; } int f2(int a){//出た時間を返すO(1) return out[a]; } int dis(int u,int v){//uとvの最短距離を求めるO(logN) assert(0<=u&&u void set(int u,T&seg,G x){ seg.set(f1(u),x); } template G prod(int u,int v,bool edge,T&seg){//[u,v]の演算結果を取得する,G>ret T>seg木 O(log^2N) G sml=seg.e,smr=seg.e; while(1){ if(in[u]>in[v])swap(u,v),swap(sml,smr); if(head[u]==head[v])break; smr=seg.op(smr,seg.prod(in[head[v]],in[v]+1)); v=par[head[v]]; } return seg.op(sml,seg.op(smr,seg.prod(in[u]+edge,in[v]+1))); } }; void solve(){ INT(n,k); vvcg(n); HLD hld(n); rep(i,n-1){ INT(a,b,c); g[--a].pb({--b,c}); g[b].pb({a,c}); hld.add_edge(a,b,1); } hld.init(); vvi vs(k); vi p(k); rep(i,k){ INT(m); cin>>p[i]; rep(j,m){ INT(x); vs[i].pb(--x); } } vvccost(k,vc(k)); vl md1(n); functiondfs=[&](int u,int v){ for(auto&[x,w]:g[u]){ if(x==v)continue; md1[x]=md1[u]+w; dfs(x,u); } }; dfs(0,-1); vvl md(k); rep(i,k){ smpqque; vl md_(n,1e18); for(auto x:vs[i]){ que.push({0,x}); md_[x]=0; } while(que.size()){ auto [p,q]=que.top();que.pop(); if(md_[q]!=p)continue; for(auto&[x,w]:g[q]){ if(chmin(md_[x],p+w))que.push({md_[x],x}); } } md[i]=md_; rep(j,k){ ll res=1e18; for(auto x:vs[j]){ chmin(res,md_[x]); } cost[i][j]=res; } } auto Dist=[&](int u,int v){ return md1[u]+md1[v]-md1[hld.lca(u,v)]*2; }; INT(q); while(q--){ INT(u,v); --u,--v; vl md2(k+2,1e18); md2[k]=0; smpqque; que.push({0,k}); while(que.size()){ auto [dist,q]=que.top();que.pop(); if(md2[q]!=dist)continue; rep(i,k){ if(q==k){ ll cost=md[i][u]+p[i]; if(chmin(md2[i],dist+cost))que.push({md2[i],i}); }else if(q==k+1){ continue; }else{ ll cost1=cost[i][q]+p[i]; if(chmin(md2[i],dist+cost1))que.push({md2[i],i}); } } if(q==k){ if(chmin(md2[k+1],dist+Dist(u,v)))que.push({md2[k+1],k+1}); } if(qsync_with_stdio(0); pass_time=clock(); int t=1; //cin>>t; while(t--)solve(); pass_time=clock()-pass_time; dbg(pass_time/CLOCKS_PER_SEC); }