#include <bits/stdc++.h>
using namespace std;
const int N=100005;
int leaves=0;
int n,q;
struct segtree{
struct info{
int cnt0=0,cnt1=0,lz=0;
int rcnt0(){return (lz?cnt1:cnt0);}
int rcnt1(){return (lz?cnt0:cnt1);}
}v[N<<2];
info pull(info a,info b){
return {a.rcnt0()+b.rcnt0(),a.rcnt1()+b.rcnt1(),0};
}
void push(int id){
v[id<<1].lz^=v[id].lz;
v[id<<1|1].lz^=v[id].lz;
v[id].lz=0;
}
void build(int id,int l,int r){
if(r-l==1){
v[id].cnt0=1;
return;
}
int m=(l+r)>>1;
build(id<<1,l,m);
build(id<<1|1,m,r);
v[id]=pull(v[id<<1],v[id<<1|1]);
}
void add(int id,int a,int b,int l,int r){
//cout<<id<<' '<<a<<' '<<b<<' '<<l<<' '<<r<<endl;
if(a<=l&&b>=r){
v[id].lz^=1;
return;
}
int m=(l+r)>>1;
push(id);
if(a<m)add(id<<1,a,b,l,m);
if(b>m)add(id<<1|1,a,b,m,r);
v[id]=pull(v[id<<1],v[id<<1|1]);
}
int ask(){
return v[1].rcnt0();
}
}tr;
vector<int> adj[N],big(N,-1),sz(N,0),pa(N),deg(N,0),gp(N,0);
vector<int> path,place(N,0);
void dfs(int k){
if(deg[k]==1)sz[k]=1;
for(int j:adj[k]){
if(j==pa[k])continue;
pa[j]=k;
dfs(j);
sz[k]+=sz[j];
big[k]=(big[k]==-1||sz[big[k]]<sz[j]?j:big[k]);
}
}
void dfs2(int k){
place[k]=path.size();
path.push_back(k);
if(big[k]!=-1){
gp[big[k]]=gp[k];
dfs2(big[k]);
}
for(int j:adj[k]){
if(j!=big[k]&&j!=pa[k]){
gp[j]=j;
dfs2(j);
}
}
}
void add1(int p,int x){
for(int i=p;i>0;i=pa[gp[i]]){
tr.add(1,place[gp[i]],place[i]+1,0,n);
}
/*for(int i=p;i>0;i=pa[i]){
//cout<<place[i]<<endl;
tr.add(1,place[i],place[i]+1,0,n);
}*/
}
void add(int p,int x){
if(x==1&°[p]==1){
leaves--;
return;
}
if(x==-1&°[p]==1){
leaves++;
return;
}
add1(p,x);
}
int32_t main(){
cin>>n>>q;
tr.build(1,0,n);
for(int i=1;i<n;i++){
int a,b;cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int rt=1;
for(int i=1;i<=n;i++){
deg[i]=adj[i].size();
if(deg[i]>=2){
rt=i;
}
else{
leaves++;
}
}
gp[rt]=rt;
dfs(rt);
dfs2(rt);
for(int i=1;i<=n;i++){
if(deg[i]==1){
add1(i,1);
}
}
for(int i=0;i<q;i++){
int t;cin>>t;
vector<int> a(t);
for(int j=0;j<t;j++){
cin>>a[j];
add(a[j],1);
deg[a[j]]++;
}
int ans=n-2+t+tr.ask();
int leaf=leaves+t;
for(int j=0;j<t;j++){
deg[a[j]]--;
add(a[j],-1);
}
if(leaf&1){
cout<<-1<<endl;
}
else{
cout<<ans<<endl;
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
107 ms |
7512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
6064 KB |
Output is correct |
2 |
Correct |
54 ms |
5980 KB |
Output is correct |
3 |
Correct |
88 ms |
13256 KB |
Output is correct |
4 |
Correct |
96 ms |
12412 KB |
Output is correct |
5 |
Correct |
111 ms |
13868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
6652 KB |
Output is correct |
2 |
Correct |
46 ms |
6488 KB |
Output is correct |
3 |
Correct |
68 ms |
20688 KB |
Output is correct |
4 |
Correct |
178 ms |
20444 KB |
Output is correct |
5 |
Correct |
59 ms |
19664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
7960 KB |
Output is correct |
2 |
Correct |
45 ms |
7004 KB |
Output is correct |
3 |
Correct |
20 ms |
7000 KB |
Output is correct |
4 |
Correct |
17 ms |
7260 KB |
Output is correct |
5 |
Correct |
24 ms |
7508 KB |
Output is correct |
6 |
Correct |
53 ms |
7760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
10620 KB |
Output is correct |
2 |
Correct |
211 ms |
10232 KB |
Output is correct |
3 |
Correct |
158 ms |
8080 KB |
Output is correct |
4 |
Correct |
207 ms |
10480 KB |
Output is correct |
5 |
Correct |
206 ms |
10480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
403 ms |
14184 KB |
Output is correct |
2 |
Correct |
283 ms |
17160 KB |
Output is correct |
3 |
Correct |
315 ms |
16488 KB |
Output is correct |
4 |
Correct |
298 ms |
16980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4952 KB |
Output is correct |
2 |
Correct |
107 ms |
7512 KB |
Output is correct |
3 |
Correct |
46 ms |
6064 KB |
Output is correct |
4 |
Correct |
54 ms |
5980 KB |
Output is correct |
5 |
Correct |
88 ms |
13256 KB |
Output is correct |
6 |
Correct |
96 ms |
12412 KB |
Output is correct |
7 |
Correct |
111 ms |
13868 KB |
Output is correct |
8 |
Correct |
70 ms |
6652 KB |
Output is correct |
9 |
Correct |
46 ms |
6488 KB |
Output is correct |
10 |
Correct |
68 ms |
20688 KB |
Output is correct |
11 |
Correct |
178 ms |
20444 KB |
Output is correct |
12 |
Correct |
59 ms |
19664 KB |
Output is correct |
13 |
Correct |
79 ms |
7960 KB |
Output is correct |
14 |
Correct |
45 ms |
7004 KB |
Output is correct |
15 |
Correct |
20 ms |
7000 KB |
Output is correct |
16 |
Correct |
17 ms |
7260 KB |
Output is correct |
17 |
Correct |
24 ms |
7508 KB |
Output is correct |
18 |
Correct |
53 ms |
7760 KB |
Output is correct |
19 |
Correct |
236 ms |
10620 KB |
Output is correct |
20 |
Correct |
211 ms |
10232 KB |
Output is correct |
21 |
Correct |
158 ms |
8080 KB |
Output is correct |
22 |
Correct |
207 ms |
10480 KB |
Output is correct |
23 |
Correct |
206 ms |
10480 KB |
Output is correct |
24 |
Correct |
403 ms |
14184 KB |
Output is correct |
25 |
Correct |
283 ms |
17160 KB |
Output is correct |
26 |
Correct |
315 ms |
16488 KB |
Output is correct |
27 |
Correct |
298 ms |
16980 KB |
Output is correct |
28 |
Correct |
162 ms |
10196 KB |
Output is correct |
29 |
Correct |
169 ms |
16008 KB |
Output is correct |
30 |
Correct |
117 ms |
13868 KB |
Output is correct |
31 |
Correct |
128 ms |
20428 KB |
Output is correct |
32 |
Correct |
209 ms |
10264 KB |
Output is correct |
33 |
Correct |
137 ms |
15840 KB |
Output is correct |
34 |
Correct |
174 ms |
16048 KB |
Output is correct |
35 |
Correct |
172 ms |
16756 KB |
Output is correct |