//#include "arithmetics.h"
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
int N, Q, leafcnt, pre[200005], order[200005], seg[800005], root, head[200005], sz[200005], t, deg[200005], odx, dp[200005];
pii io[200005];
bool lazy[800005];
vector<int> E[200005];
int getval(int i, int L, int R)
{
if (lazy[i])
return (R - L + 1) - seg[i];
else
return seg[i];
}
void modify(int mL, int mR, int i = 1, int L = 1, int R = N)
{
if (mL <= L && R <= mR)
lazy[i] ^= 1;
else if (R < mL || mR < L)
return;
else
{
int mid = (L + R) / 2;
lazy[i * 2] ^= lazy[i];
lazy[i * 2 + 1] ^= lazy[i];
lazy[i] = 0;
modify(mL, mR, i * 2, L, mid);
modify(mL, mR, i * 2 + 1, mid + 1, R);
seg[i] = getval(i * 2, L, mid) + getval(i * 2 + 1, mid + 1, R);
}
}
int getans()
{
return getval(1, 1, N);
}
void dfs(int k)
{
for (int i : E[k])
if (pre[k] != i)
{
pre[i] = k;
dfs(i);
sz[k] += sz[i];
}
sz[k]++;
}
void ordering(int k, int h)
{
io[k].F = ++t;
order[k] = ++odx;
head[k] = h;
if (E[k].size() >= 1)
ordering(E[k][0], h);
else
dp[k] = 1;
for (int i = 1; i < E[k].size(); i++)
ordering(E[k][i], E[k][i]);
for (int i = 0; i < E[k].size(); i++)
dp[k] ^= dp[E[k][i]];
io[k].S = t;
}
signed main()
{
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N >> Q;
for (int i = 1; i < N; i++)
{
int u, v;
cin >> u >> v;
E[u].emplace_back(v);
E[v].emplace_back(u);
}
for (int i = 1; i <= N; i++)
{
deg[i] = E[i].size();
if (E[i].size() > 1)
root = i;
else
leafcnt++;
}
dfs(root);
for (int i = 1; i <= N; i++)
E[i].clear();
for (int i = 1; i <= N; i++)
if (i != root)
E[pre[i]].emplace_back(i);
for (int i = 1; i <= N; i++)
sort(E[i].begin(), E[i].end(), [&](int i1, int i2)
{ return sz[i1] > sz[i2]; });
ordering(root, root);
//for (int i = 1; i <= N; i++)
// cout << dp[i] << " \n"[i == N];
//for (int i = 1; i <= N; i++)
// cout << order[i] << " \n"[i == N];
//for (int i = 1; i <= N; i++)
// cout << head[i] << " \n"[i == N];
for (int i = 1; i <= N; i++)
if(dp[i])
modify(order[i], order[i]);
for (int i = 1; i <= Q; i++)
{
int D;
vector<int> v;
cin >> D;
v.resize(D);
for (int j = 0; j < D; j++)
cin >> v[j];
for (int j = 0; j < D; j++)
{
if(deg[v[j]] != 1)
{
int u = v[j];
while(u != 0)
{
modify(order[head[u]], order[u]);
u = pre[head[u]];
}
leafcnt++;
}
deg[v[j]]++;
}
//cout << "ans " << getans() << '\n';
if(leafcnt % 2 == 0)
cout << (N - 1) * 2 + D - getans() << '\n';
else
cout << "-1\n";
for (int j = 0; j < D; j++)
{
deg[v[j]]--;
if(deg[v[j]] != 1)
{
int u = v[j];
while(u != 0)
{
modify(order[head[u]], order[u]);
u = pre[head[u]];
}
leafcnt--;
}
}
}
}
Compilation message
cleaning.cpp: In function 'void ordering(int, int)':
cleaning.cpp:67:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | for (int i = 1; i < E[k].size(); i++)
| ~~^~~~~~~~~~~~~
cleaning.cpp:69:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
69 | for (int i = 0; i < E[k].size(); i++)
| ~~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
111 ms |
7304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
63 ms |
5964 KB |
Output is correct |
2 |
Correct |
60 ms |
5964 KB |
Output is correct |
3 |
Correct |
54 ms |
13284 KB |
Output is correct |
4 |
Correct |
93 ms |
11848 KB |
Output is correct |
5 |
Correct |
93 ms |
14104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
6476 KB |
Output is correct |
2 |
Correct |
49 ms |
6564 KB |
Output is correct |
3 |
Correct |
77 ms |
20804 KB |
Output is correct |
4 |
Correct |
134 ms |
20300 KB |
Output is correct |
5 |
Correct |
64 ms |
19476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
7716 KB |
Output is correct |
2 |
Correct |
43 ms |
6920 KB |
Output is correct |
3 |
Correct |
14 ms |
6732 KB |
Output is correct |
4 |
Correct |
15 ms |
7244 KB |
Output is correct |
5 |
Correct |
21 ms |
7116 KB |
Output is correct |
6 |
Correct |
54 ms |
7620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
11128 KB |
Output is correct |
2 |
Correct |
227 ms |
10820 KB |
Output is correct |
3 |
Correct |
182 ms |
8332 KB |
Output is correct |
4 |
Correct |
218 ms |
10836 KB |
Output is correct |
5 |
Correct |
212 ms |
10820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
243 ms |
14528 KB |
Output is correct |
2 |
Correct |
130 ms |
17348 KB |
Output is correct |
3 |
Correct |
238 ms |
16692 KB |
Output is correct |
4 |
Correct |
186 ms |
17172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4940 KB |
Output is correct |
2 |
Correct |
111 ms |
7304 KB |
Output is correct |
3 |
Correct |
63 ms |
5964 KB |
Output is correct |
4 |
Correct |
60 ms |
5964 KB |
Output is correct |
5 |
Correct |
54 ms |
13284 KB |
Output is correct |
6 |
Correct |
93 ms |
11848 KB |
Output is correct |
7 |
Correct |
93 ms |
14104 KB |
Output is correct |
8 |
Correct |
46 ms |
6476 KB |
Output is correct |
9 |
Correct |
49 ms |
6564 KB |
Output is correct |
10 |
Correct |
77 ms |
20804 KB |
Output is correct |
11 |
Correct |
134 ms |
20300 KB |
Output is correct |
12 |
Correct |
64 ms |
19476 KB |
Output is correct |
13 |
Correct |
89 ms |
7716 KB |
Output is correct |
14 |
Correct |
43 ms |
6920 KB |
Output is correct |
15 |
Correct |
14 ms |
6732 KB |
Output is correct |
16 |
Correct |
15 ms |
7244 KB |
Output is correct |
17 |
Correct |
21 ms |
7116 KB |
Output is correct |
18 |
Correct |
54 ms |
7620 KB |
Output is correct |
19 |
Correct |
145 ms |
11128 KB |
Output is correct |
20 |
Correct |
227 ms |
10820 KB |
Output is correct |
21 |
Correct |
182 ms |
8332 KB |
Output is correct |
22 |
Correct |
218 ms |
10836 KB |
Output is correct |
23 |
Correct |
212 ms |
10820 KB |
Output is correct |
24 |
Correct |
243 ms |
14528 KB |
Output is correct |
25 |
Correct |
130 ms |
17348 KB |
Output is correct |
26 |
Correct |
238 ms |
16692 KB |
Output is correct |
27 |
Correct |
186 ms |
17172 KB |
Output is correct |
28 |
Correct |
184 ms |
10632 KB |
Output is correct |
29 |
Correct |
167 ms |
16256 KB |
Output is correct |
30 |
Correct |
93 ms |
14080 KB |
Output is correct |
31 |
Correct |
128 ms |
20292 KB |
Output is correct |
32 |
Correct |
234 ms |
10824 KB |
Output is correct |
33 |
Correct |
129 ms |
15460 KB |
Output is correct |
34 |
Correct |
191 ms |
16028 KB |
Output is correct |
35 |
Correct |
175 ms |
16956 KB |
Output is correct |