#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma GCC target("popcnt")
using namespace std;
using ll = long long;
using ull = unsigned long long;
using lld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using ii = pair<int,int>;
using pll = pair<ll, ll>;
using vii = vector<ii>;
using vpll = vector<pll>;
#define endl '\n'
#define all(x) x.begin(),x.end()
#define lsb(x) x&(-x)
#define gcd(a,b) __gcd(a,b)
#define sz(x) (int)x.size()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fls cout.flush()
#define fore(i,l,r) for(auto i=l;i<r;i++)
#define fo(i,n) fore(i,0,n)
#define forex(i,r,l) for(auto i=r; i>=l;i--)
#define ffo(i,n) forex(i,n-1,0)
bool cmin(int &a, int b){if(b<a){a=b;return 1;}return 0;}
bool cmax(int &a, int b){if(b>a){a=b;return 1;}return 0;}
void valid(ll in){cout<<((in)?"YES\n":"NO\n");}
ll lcm(ll a, ll b){return (a/gcd(a,b))*b;}
ll gauss(ll n){return (n*(n+1))/2;}
const int N = 1e5 + 7, LOG=20;
vi graph[N];
int r=0,tin[N],timer=0, up[N][LOG], ni[N];
void dfs(int u,int p=0){
tin[u]=timer++;
up[u][0]=p;
fore(i,1,LOG)up[u][i]=up[up[u][i-1]][i-1];
for(int v:graph[u]){
if(v==p)continue;
ni[v]=ni[u]+1;
dfs(v, u);
}
}
bool ord(int i, int j){
return tin[i]<tin[j];
}
int LCA(int u,int v){
if(ni[u]>ni[v])swap(u,v);
ffo(i,LOG)if((ni[v]-ni[u])&(1<<i))v=up[v][i];
ffo(i,LOG)if(up[u][i]!=up[v][i])u=up[u][i],v=up[v][i];
return (u==v?u:up[u][0]);
}
int dis(int u, int v){
return ni[u]+ni[v]-2*ni[LCA(u,v)];
}
void test_case(){
int n,q;
cin>>n>>q;
fo(i,n-1){
int a,b;
cin>>a>>b;
graph[a].pb(b);
graph[b].pb(a);
}
fore(i,1,n+1)if(sz(graph[i])>1)r=i;
while(q--){
timer=0;
int k;
cin>>k;
vi u(k);
int x=n;
fo(i,k){
cin>>u[i];
graph[u[i]].pb(++x);
graph[x].pb(u[i]);
}
dfs(r);
vi leaves;
fore(i,1,x+1)if(sz(graph[i])==1)leaves.pb(i);
if(sz(leaves)&1){
cout<<-1<<endl;
}else{
sort(all(leaves), ord);
int ans=0;
for(int i=1;i<sz(leaves)-1;i+=2)ans+=dis(leaves[i], leaves[i+1]);
ans+=dis(leaves[0],leaves.back());
cout<<ans<<endl;
}
fo(i,k)graph[u[i]].pop_back();
fore(i,n+1,x+1)graph[i].pop_back();
}
}
int main(){cin.tie(0)->sync_with_stdio(0);
int t=1;
// cin >> t;
while(t--)test_case();
}
Compilation message
cleaning.cpp:4: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
4 | #pragma GCC optimization ("O3")
|
cleaning.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization ("unroll-loops")
|
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Execution timed out |
1096 ms |
10260 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
15316 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
27 ms |
15840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
760 ms |
8528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1032 ms |
13708 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1029 ms |
15092 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
7004 KB |
Output is correct |
2 |
Execution timed out |
1096 ms |
10260 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |