#include<bits/stdc++.h>
using namespace std;
const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
// #define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
const int maxn = 1e5+10;
int n, q, gr[maxn], p[maxn], sbl[maxn];
vector<int> g[maxn];
int cntl, ans;
int tr[4*maxn], lz[4*maxn], tin[maxn], tout[maxn], timer;
int sbsz[maxn], phld[maxn];
void build(int no, int l, int r) {
lz[no] = 0;
if(l == r) {
tr[no] = 0;
return;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
build(lc,l,mid);
build(rc,mid+1,r);
tr[no] = tr[lc]+tr[rc];
}
void flush(int no, int l, int r) {
if(!lz[no]) return;
tr[no] = (r-l+1)-tr[no];
if(l != r) {
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
lz[lc]^= 1;
lz[rc]^= 1;
}
lz[no] = 0;
}
void upd(int no, int l, int r, int L, int R) {
flush(no,l,r);
if(l > R || r < L) return;
if(l >= L && r <= R) {
lz[no] = 1;
flush(no,l,r);
return;
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
upd(lc,l,mid,L,R);
upd(rc,mid+1,r,L,R);
tr[no] = tr[lc]+tr[rc];
}
int qry(int no, int l, int r, int L, int R) {
flush(no,l,r);
if(l > R || r < L) return 0;
if(l >= L && r <= R) {
return tr[no];
}
int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
return qry(lc,l,mid,L,R)+qry(rc,mid+1,r,L,R);
}
void dfs(int u, int ant) {
p[u] = ant;
tin[u] = ++timer;
if(gr[u] == 1) {
cntl++;
sbl[u] = 1;
}
else sbl[u] = 0;
sbsz[u] = 1;
for(auto v : g[u]) if(v != ant) {
dfs(v,u);
sbl[u]^= sbl[v];
sbsz[u]+= sbsz[v];
}
tout[u] = timer;
}
void dfshld(int u, int ant) {
tin[u] = ++timer;
if(gr[u] == 1) {
tout[u] = timer;
return;
}
if(g[u][0] == ant) {
swap(g[u][0],g[u][1]);
}
for(int i = 1; i < g[u].size(); i++) if(g[u][i] != ant) {
if(sbsz[g[u][i]] > sbsz[g[u][0]]) swap(g[u][i],g[u][0]);
}
phld[g[u][0]] = phld[u];
dfs(g[u][0],u);
for(int i = 1; i < g[u].size(); i++) if(g[u][i] != ant) {
int v = g[u][i];
phld[v] = v;
dfs(v,u);
}
tout[u] = timer;
}
void change(int v) {
while(v != -1) {
if(sbl[v] == 1) ans--;
sbl[v]^= 1;
if(sbl[v] == 1) ans++;
upd(1,1,n,tin[v],tin[v]);
v = p[v];
}
}
void solve() {
cin >> n >> q;
for(int i = 1; i <= n-1; i++) {
int u,v; cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
gr[u]++;
gr[v]++;
}
int rt;
for(int i = 1; i <= n; i++) {
if(gr[i] != 1) {
rt = i;
break;
}
}
dfs(rt,-1);
build(1,1,n);
for(int i = 1; i <= n; i++) {
if(sbl[i] == 1) ans++;
if(sbl[i] == 1) upd(1,1,n,tin[i],tin[i]);
}
while(q--) {
int D; cin >> D;
vector<int> as;
for(int i = 0; i < D; i++) {
int v; cin >> v;
as.pb(v);
if(gr[v] == 1) {
cntl--;
change(v);
}
gr[v]++;
cntl++;
change(v);
}
if(cntl%2 == 1) {
cout << -1 << endl;
}
else {
int q1 = tr[1]+D;
int q2 = n-1-tr[1];
cout << q1 + 2*q2 << endl;
}
for(int i = 0; i < D; i++) {
int v = as[i];
gr[v]--;
if(gr[v] == 1) {
cntl++;
change(v);
}
cntl--;
change(v);
}
}
}
int32_t main() {
ios::sync_with_stdio(false); cin.tie(0);
// freopen("in.in", "r", stdin);
// freopen("out.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--) {
solve();
}
}
Compilation message
cleaning.cpp: In function 'void flush(int, int, int)':
cleaning.cpp:39:31: warning: unused variable 'mid' [-Wunused-variable]
39 | int lc=2*no,rc=2*no+1,mid=(l+r)>>1;
| ^~~
cleaning.cpp: In function 'void dfshld(int, int)':
cleaning.cpp:96:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | for(int i = 1; i < g[u].size(); i++) if(g[u][i] != ant) {
| ~~^~~~~~~~~~~~~
cleaning.cpp:102:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
102 | for(int i = 1; i < g[u].size(); i++) if(g[u][i] != ant) {
| ~~^~~~~~~~~~~~~
cleaning.cpp: In function 'void solve()':
cleaning.cpp:137:8: warning: 'rt' may be used uninitialized in this function [-Wmaybe-uninitialized]
137 | dfs(rt,-1);
| ~~~^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
810 ms |
4484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
3540 KB |
Output is correct |
2 |
Correct |
54 ms |
3488 KB |
Output is correct |
3 |
Correct |
59 ms |
10616 KB |
Output is correct |
4 |
Correct |
163 ms |
9516 KB |
Output is correct |
5 |
Correct |
181 ms |
11252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
3496 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1083 ms |
4948 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
540 ms |
7600 KB |
Output is correct |
2 |
Correct |
917 ms |
7312 KB |
Output is correct |
3 |
Correct |
736 ms |
5068 KB |
Output is correct |
4 |
Correct |
851 ms |
7488 KB |
Output is correct |
5 |
Correct |
863 ms |
7444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1094 ms |
10676 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2644 KB |
Output is correct |
2 |
Correct |
810 ms |
4484 KB |
Output is correct |
3 |
Correct |
62 ms |
3540 KB |
Output is correct |
4 |
Correct |
54 ms |
3488 KB |
Output is correct |
5 |
Correct |
59 ms |
10616 KB |
Output is correct |
6 |
Correct |
163 ms |
9516 KB |
Output is correct |
7 |
Correct |
181 ms |
11252 KB |
Output is correct |
8 |
Execution timed out |
1092 ms |
3496 KB |
Time limit exceeded |
9 |
Halted |
0 ms |
0 KB |
- |