#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ar array
#define ld long double
const int N = 2e5 + 20;
const int INF = 1e16;
const int MOD = 1e9 + 7;
const ld EPS = 1e-9;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,bmi,bmi2,lzcnt,popcnt")
int n, A[N], in[N], dep[N], par[N], top[N], sz[N], T;
vector<int> g[N];
namespace SGT{
int s[4 * N], lz[4 * N];
void push(int k,int tl, int tr){
if(!lz[k])return;
s[k] = tr - tl + 1 - s[k];
if(tl != tr){
lz[k * 2] ^= lz[k];
lz[k * 2 + 1] ^= lz[k];
}
lz[k] = 0;
}
void upd(int k,int tl,int tr,int l,int r){
push(k, tl, tr);
if(tl >r || l > tr)return;
if(l <= tl && tr <= r){
lz[k] ^= 1;
push(k, tl, tr);
return;
}
int tm = (tl + tr) / 2;
upd(k * 2, tl, tm, l,r );
upd(k *2 + 1, tm + 1, tr, l, r);
s[k] = s[k * 2] + s[k * 2 + 1];
}
int get(){
push(1, 1, n);
return n - s[1];
}
}
void dfs1(int x,int p){
vector<int> v;
sz[x] = 1;
par[x] = p;
for(auto u: g[x]){
if(u == p)continue;
v.push_back(u);
dep[u] = dep[x] + 1;
dfs1(u, x);
sz[x] += sz[u];
}
g[x] = v;
for(int i = 1;i < g[x].size();i++){
if(sz[g[x][i]] > sz[g[x][0]])swap(g[x][0], g[x][i]);
}
}
void dfs2(int x,bool t){
if(!t)top[x] = top[par[x]];
else top[x] = x;
in[x] = ++T;
int p = 0;
for(auto u: g[x])dfs2(u, p++);
}
void upd(int u,int v){
while(top[u] != top[v]){
if(dep[top[u]] < dep[top[v]])swap(u, v);
SGT::upd(1, 1, n, in[top[u]], in[u]);
u = par[top[u]];
}
if(dep[u] > dep[v])swap(u, v);
SGT::upd(1, 1, n, in[u], in[v]);
}
signed main(){ios_base::sync_with_stdio(false);cin.tie(0);
int q;
cin>>n>>q;
for(int i = 1;i < n;i++){
int u, v;
cin>>u>>v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
int r = 0;
for(int i = 0;i < n;i++){
if(g[i].size() >= 2){
r = i;
break;
}
}
dfs1(r, r);
dfs2(r, 1);
int cnt = 0;
for(int i = 0;i <n;i++){
if(g[i].empty())upd(i, r), cnt++;
}
//cout<<cnt<<'\n';
while(q--){
int k;
cin>>k;
set<int> s;
for(int i = 0;i < k;i++){
int u;
cin>>u;
--u;
s.insert(u);
A[u] ^= 1;
}
int nc = cnt;
for(auto u: s){
if(g[u].empty())--nc;
}
if((nc + k) % 2){
cout<<-1<<'\n';;
for(auto u: s)A[u] = 0;
continue;
}
for(auto u: s){
if(g[u].empty() != A[u])upd(u, r);
}
cout<<n + k + SGT::get() - 2<<'\n';
for(auto u: s){
if(g[u].empty() != A[u])upd(u, r);
}
for(auto u: s)A[u] = 0;
}
}
Compilation message
cleaning.cpp: In function 'void dfs1(long long int, long long int)':
cleaning.cpp:62:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for(int i = 1;i < g[x].size();i++){
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
84 ms |
17232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
11100 KB |
Output is correct |
2 |
Correct |
11 ms |
11352 KB |
Output is correct |
3 |
Correct |
51 ms |
23244 KB |
Output is correct |
4 |
Correct |
74 ms |
23576 KB |
Output is correct |
5 |
Correct |
99 ms |
26172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
13916 KB |
Output is correct |
2 |
Correct |
13 ms |
14356 KB |
Output is correct |
3 |
Correct |
41 ms |
40016 KB |
Output is correct |
4 |
Correct |
77 ms |
38424 KB |
Output is correct |
5 |
Correct |
36 ms |
35108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
16220 KB |
Output is correct |
2 |
Correct |
37 ms |
16984 KB |
Output is correct |
3 |
Correct |
14 ms |
14940 KB |
Output is correct |
4 |
Correct |
11 ms |
16220 KB |
Output is correct |
5 |
Correct |
14 ms |
16728 KB |
Output is correct |
6 |
Correct |
43 ms |
16472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
19000 KB |
Output is correct |
2 |
Correct |
193 ms |
20052 KB |
Output is correct |
3 |
Correct |
135 ms |
16464 KB |
Output is correct |
4 |
Correct |
194 ms |
20048 KB |
Output is correct |
5 |
Correct |
180 ms |
20072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
20932 KB |
Output is correct |
2 |
Correct |
83 ms |
31056 KB |
Output is correct |
3 |
Correct |
108 ms |
30700 KB |
Output is correct |
4 |
Correct |
74 ms |
29528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
10584 KB |
Output is correct |
2 |
Correct |
84 ms |
17232 KB |
Output is correct |
3 |
Correct |
12 ms |
11100 KB |
Output is correct |
4 |
Correct |
11 ms |
11352 KB |
Output is correct |
5 |
Correct |
51 ms |
23244 KB |
Output is correct |
6 |
Correct |
74 ms |
23576 KB |
Output is correct |
7 |
Correct |
99 ms |
26172 KB |
Output is correct |
8 |
Correct |
13 ms |
13916 KB |
Output is correct |
9 |
Correct |
13 ms |
14356 KB |
Output is correct |
10 |
Correct |
41 ms |
40016 KB |
Output is correct |
11 |
Correct |
77 ms |
38424 KB |
Output is correct |
12 |
Correct |
36 ms |
35108 KB |
Output is correct |
13 |
Correct |
44 ms |
16220 KB |
Output is correct |
14 |
Correct |
37 ms |
16984 KB |
Output is correct |
15 |
Correct |
14 ms |
14940 KB |
Output is correct |
16 |
Correct |
11 ms |
16220 KB |
Output is correct |
17 |
Correct |
14 ms |
16728 KB |
Output is correct |
18 |
Correct |
43 ms |
16472 KB |
Output is correct |
19 |
Correct |
76 ms |
19000 KB |
Output is correct |
20 |
Correct |
193 ms |
20052 KB |
Output is correct |
21 |
Correct |
135 ms |
16464 KB |
Output is correct |
22 |
Correct |
194 ms |
20048 KB |
Output is correct |
23 |
Correct |
180 ms |
20072 KB |
Output is correct |
24 |
Correct |
163 ms |
20932 KB |
Output is correct |
25 |
Correct |
83 ms |
31056 KB |
Output is correct |
26 |
Correct |
108 ms |
30700 KB |
Output is correct |
27 |
Correct |
74 ms |
29528 KB |
Output is correct |
28 |
Correct |
125 ms |
19284 KB |
Output is correct |
29 |
Correct |
96 ms |
31312 KB |
Output is correct |
30 |
Correct |
75 ms |
26180 KB |
Output is correct |
31 |
Correct |
74 ms |
38228 KB |
Output is correct |
32 |
Correct |
200 ms |
20096 KB |
Output is correct |
33 |
Correct |
119 ms |
27984 KB |
Output is correct |
34 |
Correct |
123 ms |
30800 KB |
Output is correct |
35 |
Correct |
136 ms |
30804 KB |
Output is correct |