#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5;
int n, q, dp[nx], sz[nx], id[nx], pa[nx], hd[nx], t, u, v, rt, vs[nx], k[nx];
vector<int> d[nx];
struct segtree
{
int a[4*nx], b[4*nx], lz[4*nx];
void pushlz(int l, int r, int i)
{
if (!lz[i]) return;
lz[i]=0;
swap(a[i], b[i]);
if (l!=r) lz[2*i]=!lz[2*i], lz[2*i+1]=!lz[2*i+1];
}
void build(int l, int r, int i)
{
if (l==r)
{
if (k[l]==1) a[i]++;
else b[i]++;
return;
}
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
a[i]=a[2*i]+a[2*i+1];
b[i]=b[2*i]+b[2*i+1];
}
void update(int l, int r, int i, int ql, int qr)
{
pushlz(l, r, i);
if (r<ql||qr<l) return;
if (ql<=l&&r<=qr) return lz[i]=1, pushlz(l, r, i), void();
int md=(l+r)/2;
update(l, md, 2*i, ql, qr);
update(md+1, r, 2*i+1, ql, qr);
a[i]=a[2*i]+a[2*i+1];
b[i]=b[2*i]+b[2*i+1];
}
bool query(int l, int r, int i)
{
pushlz(l, r, i);
if (r!=1) return query(l, (l+r)/2, 2*i);
return a[i]==1;
}
void show(int l, int r, int i)
{
pushlz(l, r, i);
if (l==r) return void(cout<<"show "<<l<<' '<<a[i]<<' '<<b[i]<<'\n');
int md=(l+r)/2;
show(l, md, 2*i);
show(md+1, r, 2*i+1);
}
} s;
void dfs(int u, int p)
{
sz[u]=1;
pa[u]=p;
if (d[u].size()==1) dp[u]++;
for (auto v:d[u]) if (v!=p) dfs(v, u), sz[u]+=sz[v], dp[u]+=dp[v];
dp[u]%=2;
}
void dfshld(int u, int p, int h)
{
id[u]=++t;
hd[u]=h;
pair<int, int> hv={-1, -1};
for (auto v:d[u]) if (v!=p) hv=max(hv, {sz[v], v});
if (hv.second!=-1) dfshld(hv.second, u, h);
for (auto v:d[u]) if (v!=p&&v!=hv.second) dfshld(v, u, v);
}
void flip(int u)
{
//cout<<"update "<<u<<' '<<hd[u]<<' '<<id[u]<<' '<<id[hd[u]]<<'\n';
s.update(1, n, 1, id[hd[u]], id[u]);
if (hd[u]==rt) return;
flip(pa[hd[u]]);
}
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>q;
for (int i=1; i<n; i++) cin>>u>>v, d[u].push_back(v), d[v].push_back(u);
for (int i=1; i<=n; i++) if (d[i].size()>1) rt=i;
dfs(rt, rt);
dfshld(rt, rt, rt);
for (int i=1; i<=n; i++) k[id[i]]=dp[i];
s.build(1, n, 1);
//s.show(1, n, 1);
//for (int i=1; i<=n; i++) cout<<"id "<<i<<' '<<id[i]<<'\n';
//cout<<"here "<<rt<<'\n';
while (q--)
{
cin>>t;
vector<int> vl(t);
for (auto &x:vl) cin>>x;
for (int i=0; i<t; i++)
{
if (d[vl[i]].size()==1&&!vs[vl[i]]) vs[vl[i]]=1;
else flip(vl[i]);
}
//s.show(1, n, 1);
if (s.query(1, n, 1)) cout<<-1<<'\n';
else cout<<s.a[1]+2*s.b[1]+t-2<<'\n';
for (int i=0; i<t; i++)
{
if (d[vl[i]].size()==1&&vs[vl[i]]) vs[vl[i]]=0;
else flip(vl[i]);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
78 ms |
6260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
4956 KB |
Output is correct |
2 |
Correct |
39 ms |
5468 KB |
Output is correct |
3 |
Correct |
21 ms |
13000 KB |
Output is correct |
4 |
Correct |
51 ms |
12512 KB |
Output is correct |
5 |
Correct |
53 ms |
14396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
7516 KB |
Output is correct |
2 |
Correct |
36 ms |
7516 KB |
Output is correct |
3 |
Correct |
36 ms |
20304 KB |
Output is correct |
4 |
Correct |
101 ms |
19792 KB |
Output is correct |
5 |
Correct |
33 ms |
18520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
8280 KB |
Output is correct |
2 |
Correct |
28 ms |
6488 KB |
Output is correct |
3 |
Correct |
7 ms |
8028 KB |
Output is correct |
4 |
Correct |
8 ms |
8428 KB |
Output is correct |
5 |
Correct |
11 ms |
8420 KB |
Output is correct |
6 |
Correct |
38 ms |
7324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
10324 KB |
Output is correct |
2 |
Correct |
164 ms |
10232 KB |
Output is correct |
3 |
Correct |
124 ms |
8456 KB |
Output is correct |
4 |
Correct |
154 ms |
10216 KB |
Output is correct |
5 |
Correct |
151 ms |
10220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
13348 KB |
Output is correct |
2 |
Correct |
80 ms |
18624 KB |
Output is correct |
3 |
Correct |
124 ms |
17824 KB |
Output is correct |
4 |
Correct |
111 ms |
17748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
78 ms |
6260 KB |
Output is correct |
3 |
Correct |
39 ms |
4956 KB |
Output is correct |
4 |
Correct |
39 ms |
5468 KB |
Output is correct |
5 |
Correct |
21 ms |
13000 KB |
Output is correct |
6 |
Correct |
51 ms |
12512 KB |
Output is correct |
7 |
Correct |
53 ms |
14396 KB |
Output is correct |
8 |
Correct |
36 ms |
7516 KB |
Output is correct |
9 |
Correct |
36 ms |
7516 KB |
Output is correct |
10 |
Correct |
36 ms |
20304 KB |
Output is correct |
11 |
Correct |
101 ms |
19792 KB |
Output is correct |
12 |
Correct |
33 ms |
18520 KB |
Output is correct |
13 |
Correct |
63 ms |
8280 KB |
Output is correct |
14 |
Correct |
28 ms |
6488 KB |
Output is correct |
15 |
Correct |
7 ms |
8028 KB |
Output is correct |
16 |
Correct |
8 ms |
8428 KB |
Output is correct |
17 |
Correct |
11 ms |
8420 KB |
Output is correct |
18 |
Correct |
38 ms |
7324 KB |
Output is correct |
19 |
Correct |
103 ms |
10324 KB |
Output is correct |
20 |
Correct |
164 ms |
10232 KB |
Output is correct |
21 |
Correct |
124 ms |
8456 KB |
Output is correct |
22 |
Correct |
154 ms |
10216 KB |
Output is correct |
23 |
Correct |
151 ms |
10220 KB |
Output is correct |
24 |
Correct |
166 ms |
13348 KB |
Output is correct |
25 |
Correct |
80 ms |
18624 KB |
Output is correct |
26 |
Correct |
124 ms |
17824 KB |
Output is correct |
27 |
Correct |
111 ms |
17748 KB |
Output is correct |
28 |
Correct |
109 ms |
11088 KB |
Output is correct |
29 |
Correct |
109 ms |
17576 KB |
Output is correct |
30 |
Correct |
55 ms |
14284 KB |
Output is correct |
31 |
Correct |
88 ms |
21116 KB |
Output is correct |
32 |
Correct |
151 ms |
11428 KB |
Output is correct |
33 |
Correct |
92 ms |
16348 KB |
Output is correct |
34 |
Correct |
128 ms |
16860 KB |
Output is correct |
35 |
Correct |
115 ms |
18152 KB |
Output is correct |