#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
const int maxn=1e5+5, Log=17, pot=(1<<Log);
struct tournament{
int t[pot*2];
bool prop[pot*2];
int vel[pot*2];
tournament(){
for(int i=pot; i<pot*2; i++){
vel[i]=1;
}
for(int i=pot-1; i>0; i--){
vel[i]=vel[i*2]*2;
}
}
void propagate(int x){
if(prop[x]){
t[x]=vel[x]-t[x];
if(x<pot){
prop[x*2]^=1;
prop[x*2+1]^=1;
}
prop[x]=0;
}
}
void update(int x, int val){
t[x]=val;
for(x/=2; x>0; x/=2){
propagate(x*2);
propagate(x*2+1);
t[x]=t[x*2]+t[x*2+1];
}
}
void update2(int L, int D, int x, int l, int d){
propagate(x);
if(L>=l && D<=d){
// cout << "flip " << L << ' ' << D << endl;
update(x, vel[x]-t[x]);
if(x<pot){
prop[x*2]^=1;
prop[x*2+1]^=1;
}
return;
}
if((L+D)/2>=l){
update2(L, (L+D)/2, x*2, l, d);
}
if((L+D)/2+1<=d){
update2((L+D)/2+1, D, x*2+1, l, d);
}
}
int query(int L, int D, int x, int l, int d){
propagate(x);
if(L>=l && D<=d){
return t[x];
}
int lijeva=0, desna=0;
if((L+D)/2>=l){
lijeva=query(L, (L+D)/2, x*2, l, d);
}
if((L+D)/2+1<=d){
desna=query((L+D)/2+1, D, x*2+1, l, d);
}
return lijeva+desna;
}
};
tournament T;
vector < int > ms[maxn];
int novi[maxn];
bool p[maxn];
int list;
int root;
int djeca[maxn];
int parent[maxn];
int dfs2(int x, int prosl){
parent[x]=prosl;
djeca[x]=1;
for(int i=0; i<(int)ms[x].size(); i++){
if(ms[x][i]!=prosl){
djeca[x]+=dfs2(ms[x][i], x);
}
}
return djeca[x];
}
int gornji[maxn];
int pos[maxn];
int boja[maxn];
int tren, koji;
void odredi(int x, int prosl){
boja[x]=tren;
pos[x]=koji;
koji++;
if(gornji[tren]==-1){
gornji[tren]=x;
}
for(int i=0; i<(int)ms[x].size(); i++){
if(ms[x][i]!=prosl && djeca[ms[x][i]]*2>=djeca[x]){
odredi(ms[x][i], x);
}
}
for(int i=0; i<(int)ms[x].size(); i++){
if(ms[x][i]!=prosl && djeca[ms[x][i]]*2<djeca[x]){
tren++;
odredi(ms[x][i], x);
}
}
}
int dfs(int x, int prosl){
int br=0;
for(int i=0; i<(int)ms[x].size(); i++){
if(ms[x][i]!=prosl){
br+=dfs(ms[x][i], x);
}
}
if(x==prosl){
if(br&1){
// cout << x << " je " << pos[x] << endl;
T.update(pos[x]+pot, 1);
}
return br;
}
if(!br){
list++;
br++;
}
if(!(br&1)){
// cout << x << " je " << pos[x] << endl;
T.update(pos[x]+pot, 1);
}
return br;
}
void scan(int &a){
a=0;
char c=getchar_unlocked();
while(c>='0' && c<='9'){
a*=10;
a+=c-'0';
c=getchar_unlocked();
}
}
void update(int x){
// cout << "za " << x << " je " << pos[gornji[boja[x]]] << ' ' << pos[x] << endl;
T.update2(0, pot-1, 1, pos[gornji[boja[x]]], pos[x]);
if(parent[gornji[boja[x]]]==gornji[boja[x]]){
return;
}
update(parent[gornji[boja[x]]]);
}
int main(){
int n, q;
scan(n);
scan(q);
int a, b;
for(int i=0; i<n-1; i++){
scan(a);
scan(b);
a--;
b--;
ms[a].push_back(b);
ms[b].push_back(a);
}
for(int i=0; i<n; i++){
if(ms[i].size()>1){
root=i;
break;
}
}
memset(gornji, -1, sizeof(gornji));
dfs2(root, root);
odredi(root, root);
dfs(root, root);
int m;
int liste;
for(int i=0; i<q; i++){
liste=list;
scan(m);
for(int j=0; j<m; j++){
scan(a);
a--;
novi[j]=a;
if(ms[a].size()==1 && !p[a]){
p[a]=1;
}
else{
liste++;
update(a);
}
}
// cout << "ost " << T.t[1] << endl;
if(liste&1){
printf("-1\n");
}
else{
// cout << "aha " << T.query(0, pot-1, 1, 0, 0) << endl;
printf("%d\n", n-1+m+T.t[1]);
}
for(int j=0; j<m; j++){
if(ms[novi[j]].size()==1 && p[novi[j]]){
p[novi[j]]=0;
}
else{
update(novi[j]);
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
122 ms |
5948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
4888 KB |
Output is correct |
2 |
Correct |
52 ms |
5008 KB |
Output is correct |
3 |
Correct |
18 ms |
10316 KB |
Output is correct |
4 |
Correct |
55 ms |
8944 KB |
Output is correct |
5 |
Correct |
56 ms |
10764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
5480 KB |
Output is correct |
2 |
Correct |
78 ms |
5512 KB |
Output is correct |
3 |
Correct |
66 ms |
17804 KB |
Output is correct |
4 |
Correct |
109 ms |
16912 KB |
Output is correct |
5 |
Correct |
33 ms |
16364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
99 ms |
6468 KB |
Output is correct |
2 |
Correct |
40 ms |
5704 KB |
Output is correct |
3 |
Correct |
9 ms |
5464 KB |
Output is correct |
4 |
Correct |
14 ms |
6124 KB |
Output is correct |
5 |
Correct |
12 ms |
6264 KB |
Output is correct |
6 |
Correct |
70 ms |
6200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
8672 KB |
Output is correct |
2 |
Correct |
275 ms |
8088 KB |
Output is correct |
3 |
Correct |
267 ms |
6856 KB |
Output is correct |
4 |
Correct |
265 ms |
9148 KB |
Output is correct |
5 |
Correct |
287 ms |
9264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
248 ms |
11080 KB |
Output is correct |
2 |
Correct |
114 ms |
13120 KB |
Output is correct |
3 |
Correct |
162 ms |
13580 KB |
Output is correct |
4 |
Correct |
224 ms |
12964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4052 KB |
Output is correct |
2 |
Correct |
122 ms |
5948 KB |
Output is correct |
3 |
Correct |
53 ms |
4888 KB |
Output is correct |
4 |
Correct |
52 ms |
5008 KB |
Output is correct |
5 |
Correct |
18 ms |
10316 KB |
Output is correct |
6 |
Correct |
55 ms |
8944 KB |
Output is correct |
7 |
Correct |
56 ms |
10764 KB |
Output is correct |
8 |
Correct |
56 ms |
5480 KB |
Output is correct |
9 |
Correct |
78 ms |
5512 KB |
Output is correct |
10 |
Correct |
66 ms |
17804 KB |
Output is correct |
11 |
Correct |
109 ms |
16912 KB |
Output is correct |
12 |
Correct |
33 ms |
16364 KB |
Output is correct |
13 |
Correct |
99 ms |
6468 KB |
Output is correct |
14 |
Correct |
40 ms |
5704 KB |
Output is correct |
15 |
Correct |
9 ms |
5464 KB |
Output is correct |
16 |
Correct |
14 ms |
6124 KB |
Output is correct |
17 |
Correct |
12 ms |
6264 KB |
Output is correct |
18 |
Correct |
70 ms |
6200 KB |
Output is correct |
19 |
Correct |
152 ms |
8672 KB |
Output is correct |
20 |
Correct |
275 ms |
8088 KB |
Output is correct |
21 |
Correct |
267 ms |
6856 KB |
Output is correct |
22 |
Correct |
265 ms |
9148 KB |
Output is correct |
23 |
Correct |
287 ms |
9264 KB |
Output is correct |
24 |
Correct |
248 ms |
11080 KB |
Output is correct |
25 |
Correct |
114 ms |
13120 KB |
Output is correct |
26 |
Correct |
162 ms |
13580 KB |
Output is correct |
27 |
Correct |
224 ms |
12964 KB |
Output is correct |
28 |
Correct |
152 ms |
8960 KB |
Output is correct |
29 |
Correct |
165 ms |
14780 KB |
Output is correct |
30 |
Correct |
69 ms |
11716 KB |
Output is correct |
31 |
Correct |
142 ms |
18276 KB |
Output is correct |
32 |
Correct |
293 ms |
9136 KB |
Output is correct |
33 |
Correct |
179 ms |
12816 KB |
Output is correct |
34 |
Correct |
193 ms |
15012 KB |
Output is correct |
35 |
Correct |
163 ms |
14768 KB |
Output is correct |