#include<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include <cassert>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
#include <iomanip>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
//#define p push
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#pragma GCC optimize ("03,unroll-loops")
#define int long long
const int mxn=1e5+5,inf=1e9,lg=30,mod=1e9+7;
int n,q;
vector<int>adj[mxn+10];
int val[mxn+10],ans=0,up[mxn+10],cnt=0;
bool leaf[mxn+10];
void dfs(int cur,int p){
for(auto i:adj[cur]){
if(i==p)continue;
dfs(i,cur);
up[i]=cur;
val[cur]+=val[i];
}
if(val[cur]==0)leaf[cur]=true,cnt++;
if(val[cur]==0||val[cur]%2)val[cur]=1;
else val[cur]=2;
if(cur!=1)ans+=val[cur];
}
int32_t main(){
fastio
cin>>n>>q;
for(int i=0;i<n-1;i++){
int a,b;cin>>a>>b;
adj[a].pb(b);
adj[b].pb(a);
}
dfs(1,-1);
if(adj[1].size()==1)cnt++,leaf[1]=true;
while(q--){// brute force check idea
int m;cin>>m;
vector<int>v(m);
for(int i=0;i<m;i++){
cin>>v[i];
int a=v[i];
if(!leaf[a]){
cnt++;
while(a!=1){
if(val[a]==1)val[a]=2,ans++;
else val[a]=1,ans--;
a=up[a];
}
}
}
cout<<((cnt%2)?-1:ans+m)<<'\n';
for(auto i:v){
int a=i;
if(!leaf[a]){
cnt--;
while(a!=1){
if(val[a]==1)val[a]=2,ans++;
else val[a]=1,ans--;
a=up[a];
}
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Incorrect |
22 ms |
5468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
5276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1033 ms |
5868 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1055 ms |
6516 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
8068 KB |
Output is correct |
2 |
Incorrect |
35 ms |
8276 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
9964 KB |
Output is correct |
2 |
Execution timed out |
1068 ms |
12628 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4188 KB |
Output is correct |
2 |
Incorrect |
22 ms |
5468 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |