/**
* author: wxhtzdy
* created: 01.07.2022 19:07:12
**/
#include <bits/stdc++.h>
using namespace std;
string to_string(string s) {
return '"' + s + '"';
}
string to_string(const char* s) {
return to_string((string) s);
}
string to_string(bool b) {
return (b ? "true" : "false");
}
template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
const int MAX = 200000;
vector<vector<int>> g(MAX);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
vector<int> cnt(n);
int ans = 0;
const int L = 20;
vector<vector<int>> jump(n, vector<int>(L));
vector<int> dep(n);
vector<int> tin(n);
int T = 0;
function<void(int, int)> Dfs = [&](int v, int pr) {
tin[v] = ++T;
dep[v] = dep[pr] + 1;
jump[v][0] = pr;
if ((int) g[v].size() <= 1) {
cnt[v] += 1;
}
for (int u : g[v]) {
if (u != pr) {
Dfs(u, v);
cnt[v] += cnt[u];
}
}
if (pr != v) {
ans += 2 - (cnt[v] % 2);
}
};
int root;
for (int i = 0; i < n; i++) {
if ((int) g[i].size() > 1) {
root = i;
}
}
Dfs(root, root);
for (int j = 1; j < L; j++) {
for (int i = 0; i < n; i++) {
jump[i][j] = jump[jump[i][j - 1]][j - 1];
}
}
auto LCA = [&](int v, int u) {
if (dep[u] > dep[v]) {
swap(u, v);
}
for (int i = L - 1; i >= 0; i--) {
if (dep[jump[v][i]] >= dep[u]) {
v = jump[v][i];
}
}
for (int i = L - 1; i >= 0; i--) {
if (jump[u][i] != jump[v][i]) {
u = jump[u][i];
v = jump[v][i];
}
}
return u == v ? v : jump[v][0];
};
vector<vector<int>> cc(n, vector<int>(2));
Dfs = [&](int v, int pr) {
cc[v] = cc[pr];
cc[v][cnt[v] % 2] += 1;
for (int u : g[v]) {
if (u != pr) {
Dfs(u, v);
}
}
};
Dfs(root, root);
int tot = 0;
for (int i = 0; i < n; i++) {
if ((int) g[i].size() <= 1) {
tot += 1;
}
}
vector<int> par(MAX);
vector<int> sub(MAX);
while (q--) {
int d;
cin >> d;
vector<int> a(d);
for (int i = 0; i < d; i++) {
cin >> a[i];
--a[i];
}
sort(a.begin(), a.end(), [&](int i, int j) {
return tin[i] < tin[j];
});
int ntot = tot + d;
for (int i = 0; i < d; i++) {
if (i == 0 || a[i] != a[i - 1]) {
if ((int) g[a[i]].size() == 1) {
ntot -= 1;
sub[a[i]] -= 1;
}
}
}
vector<int> xs(1, root);
for (int i = 0; i < d; i++) {
xs.push_back(a[i]);
if (i > 0) {
xs.push_back(LCA(a[i], a[i - 1]));
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
sort(xs.begin(), xs.end(), [&](int i, int j) {
return tin[i] < tin[j];
});
vector<int> stk(1, root);
for (int i = 0; i < (int) xs.size(); i++) {
while (!stk.empty() && LCA(stk.back(), xs[i]) != stk.back()) {
stk.pop_back();
}
par[xs[i]] = stk.back();
stk.push_back(xs[i]);
}
for (int i = 0; i < d; i++) {
sub[a[i]] += 1;
}
for (int i = (int) xs.size() - 1; i >= 0; i--) {
if (xs[i] != root) {
sub[par[xs[i]]] += sub[xs[i]];
}
}
int nans = ans + d;
for (int i = 0; i < (int) xs.size(); i++) {
if (sub[xs[i]] % 2 == 1) {
int c0 = cc[xs[i]][0] - cc[par[xs[i]]][0];
int c1 = cc[xs[i]][1] - cc[par[xs[i]]][1];
nans -= c0;
nans += c1;
}
}
if (ntot % 2 == 0) {
cout << nans << '\n';
} else {
cout << -1 << '\n';
}
for (int i : xs) {
sub[i] = 0;
}
}
return 0;
}
Compilation message
cleaning.cpp: In function 'int main()':
cleaning.cpp:178:7: warning: 'root' may be used uninitialized in this function [-Wmaybe-uninitialized]
178 | if (xs[i] != root) {
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
117 ms |
11028 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
8728 KB |
Output is correct |
2 |
Correct |
32 ms |
8728 KB |
Output is correct |
3 |
Correct |
80 ms |
28544 KB |
Output is correct |
4 |
Correct |
119 ms |
23384 KB |
Output is correct |
5 |
Correct |
173 ms |
29932 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
9680 KB |
Output is correct |
2 |
Correct |
31 ms |
9684 KB |
Output is correct |
3 |
Correct |
93 ms |
38740 KB |
Output is correct |
4 |
Correct |
140 ms |
37276 KB |
Output is correct |
5 |
Correct |
73 ms |
35724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
11608 KB |
Output is correct |
2 |
Correct |
51 ms |
10952 KB |
Output is correct |
3 |
Correct |
17 ms |
10836 KB |
Output is correct |
4 |
Correct |
18 ms |
11624 KB |
Output is correct |
5 |
Correct |
18 ms |
11600 KB |
Output is correct |
6 |
Correct |
76 ms |
11820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
129 ms |
20980 KB |
Output is correct |
2 |
Correct |
219 ms |
20868 KB |
Output is correct |
3 |
Correct |
134 ms |
14604 KB |
Output is correct |
4 |
Correct |
182 ms |
22148 KB |
Output is correct |
5 |
Correct |
243 ms |
22080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
28620 KB |
Output is correct |
2 |
Correct |
208 ms |
32624 KB |
Output is correct |
3 |
Correct |
276 ms |
31692 KB |
Output is correct |
4 |
Correct |
231 ms |
32420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
117 ms |
11028 KB |
Output is correct |
3 |
Correct |
37 ms |
8728 KB |
Output is correct |
4 |
Correct |
32 ms |
8728 KB |
Output is correct |
5 |
Correct |
80 ms |
28544 KB |
Output is correct |
6 |
Correct |
119 ms |
23384 KB |
Output is correct |
7 |
Correct |
173 ms |
29932 KB |
Output is correct |
8 |
Correct |
30 ms |
9680 KB |
Output is correct |
9 |
Correct |
31 ms |
9684 KB |
Output is correct |
10 |
Correct |
93 ms |
38740 KB |
Output is correct |
11 |
Correct |
140 ms |
37276 KB |
Output is correct |
12 |
Correct |
73 ms |
35724 KB |
Output is correct |
13 |
Correct |
88 ms |
11608 KB |
Output is correct |
14 |
Correct |
51 ms |
10952 KB |
Output is correct |
15 |
Correct |
17 ms |
10836 KB |
Output is correct |
16 |
Correct |
18 ms |
11624 KB |
Output is correct |
17 |
Correct |
18 ms |
11600 KB |
Output is correct |
18 |
Correct |
76 ms |
11820 KB |
Output is correct |
19 |
Correct |
129 ms |
20980 KB |
Output is correct |
20 |
Correct |
219 ms |
20868 KB |
Output is correct |
21 |
Correct |
134 ms |
14604 KB |
Output is correct |
22 |
Correct |
182 ms |
22148 KB |
Output is correct |
23 |
Correct |
243 ms |
22080 KB |
Output is correct |
24 |
Correct |
209 ms |
28620 KB |
Output is correct |
25 |
Correct |
208 ms |
32624 KB |
Output is correct |
26 |
Correct |
276 ms |
31692 KB |
Output is correct |
27 |
Correct |
231 ms |
32420 KB |
Output is correct |
28 |
Correct |
178 ms |
20860 KB |
Output is correct |
29 |
Correct |
267 ms |
33320 KB |
Output is correct |
30 |
Correct |
156 ms |
31212 KB |
Output is correct |
31 |
Correct |
140 ms |
38832 KB |
Output is correct |
32 |
Correct |
190 ms |
22128 KB |
Output is correct |
33 |
Correct |
270 ms |
29816 KB |
Output is correct |
34 |
Correct |
322 ms |
33112 KB |
Output is correct |
35 |
Correct |
372 ms |
34320 KB |
Output is correct |