#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long int lld;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define trav(a,v) for(auto a:v)
void DFS(int node, vector<vector<pair<int,lld> > >&nei, vector<int> &sz, vector<bool> &used){
sz[node]=1;
trav(a,nei[node]){
if(sz[a.first]==-1 && !used[a.first]){
DFS(a.first,nei,sz,used);
sz[node]+=sz[a.first];
}
}
}
int findcentroid(int node, int tree_sz, vector<vector<pair<int,lld> > >&nei, vector<int> &sz, vector<bool> &used){
trav(a,nei[node]){
if(!used[a.first] && 2*sz[a.first]>tree_sz && sz[a.first]<sz[node]){
return findcentroid(a.first,tree_sz,nei,sz,used);
}
}
return node;
}
void DFS2(int node, lld dist, int cent, vector<vector<pair<int,lld> > >&nei,vector<bool> &visited, vector<bool> &used,vector<vector<pair<int,lld> > > &dists){
dists[node].push_back({cent,dist});
visited[node]=true;
trav(a,nei[node]){
if(!visited[a.first] && !used[a.first]){
DFS2(a.first,dist+a.second,cent,nei,visited,used,dists);
}
}
}
const lld INF=1e18;
void solve(){
int n,m;
cin>>n>>m;
vector<vector<pair<int,lld>> >nei(n);
rep(i,0,n-1){
int x,y;
lld z;
cin>>x>>y>>z;
nei[x].push_back({y,z});
nei[y].push_back({x,z});
}
vector<vector<pair<int,lld>> >dists(n);
vector<int> sz;
vector<bool> used(n,false);
vector<bool> visited;
rep(t,0,25){
sz.clear();
sz.resize(n,-1);
visited.clear();
visited.resize(n,false);
rep(i,0,n){
if(!used[i] && sz[i]==-1){
DFS(i,nei,sz,used);
int cent=findcentroid(i,sz[i],nei,sz,used);
DFS2(cent,0,cent,nei,visited,used,dists);
used[cent]=true;
}
}
}
//~ rep(i,0,n){
//~ cout<<i<<":";
//~ trav(a,dists[i]){
//~ cout<<a.first<<","<<a.second<<" ";
//~ }cout<<endl;
//~ }
vector<lld> ans(n,INF);
queue<int> q;
rep(i,0,m){
int s,t;
cin>>s>>t;
rep(i,0,s){
int x;
cin>>x;
trav(a,dists[x]){
q.push(a.first);
ans[a.first]=min(ans[a.first],a.second);
}
}
lld finans=INF;
rep(i,0,t){
int x;
cin>>x;
trav(a,dists[x]){
finans=min(finans,a.second+ans[a.first]);
}
}
cout<<finans<<"\n";
while(!q.empty()){
int u=q.front();
q.pop();
ans[u]=INF;
}
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int tt=1;
//cin>>tt;
while(tt--){
solve();
}
}
Compilation message
/usr/bin/ld: /tmp/cchvEUeI.o: in function `main':
grader.cpp:(.text.startup+0x0): multiple definition of `main'; /tmp/cck4K7WJ.o:factories.cpp:(.text.startup+0x0): first defined here
/usr/bin/ld: /tmp/cchvEUeI.o: in function `main':
grader.cpp:(.text.startup+0x37d): undefined reference to `Init(int, int*, int*, int*)'
/usr/bin/ld: grader.cpp:(.text.startup+0x412): undefined reference to `Query(int, int*, int, int*)'
collect2: error: ld returned 1 exit status