#include <bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vii vector<pii>
#define siz(x) (int)x.size()
#define pb push_back
#define rep(i,x) for(int i=0; i<x; i++)
#define rep1(i,x) for(int i=1; i<=x; i++)
using namespace std;
const int inf = 1e9, maxn = 1e5+5, mod = 1e9+7;
int n,q,ileparzystych,ilelisci;
int liscie[maxn];
bool parzyste[maxn];
int parent[maxn];
vi graf[maxn];
void DFS(int v, int p){
parent[v] = p;
if(siz(graf[v])==1){liscie[v] = 1;ilelisci++;}
for(auto u:graf[v])if(u!=p){
DFS(u,v);
liscie[v]+=liscie[u];
}
parzyste[v] = !(liscie[v]%2);
if(parzyste[v] && v!=1)ileparzystych++;
}
int32_t main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>q;
rep(i,n-1){
int a,b;
cin>>a>>b;
graf[a].pb(b);
graf[b].pb(a);
}
DFS(1,0);
rep(i,q){
int d;
cin>>d;
vi a(d);
int dojeden = 0;
rep(j,d){
cin>>a[j];
int x = a[j];
if(siz(graf[x]) > 1)ilelisci++;
if(x==1){
dojeden++;
continue;
}
while(x>1){
parzyste[x] = !parzyste[x];
if(parzyste[x])ileparzystych++;
else ileparzystych--;
x = parent[x];
}
}
if((ilelisci)&1)cout<<-1<<'\n';
else cout<<n-1+d+ileparzystych<<'\n';
rep(j,d){
int x = a[j];
if(siz(graf[x]) > 1)ilelisci--;
if(x==1){
dojeden--;
continue;
}
while(x>1){
parzyste[x] = !parzyste[x];
if(parzyste[x])ileparzystych++;
else ileparzystych--;
x = parent[x];
}
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
22 ms |
3932 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
3420 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
752 ms |
3932 KB |
Output is correct |
2 |
Correct |
745 ms |
4224 KB |
Output is correct |
3 |
Correct |
393 ms |
14904 KB |
Output is correct |
4 |
Execution timed out |
1050 ms |
14676 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1020 ms |
4440 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
33 ms |
7004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
68 ms |
8472 KB |
Output is correct |
2 |
Execution timed out |
1056 ms |
12436 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Incorrect |
22 ms |
3932 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |