#include <bits/stdc++.h>
using namespace std;
int main() {
// take inputs
cin.tie(0)->sync_with_stdio(0);
int N, Q;
cin >> N >> Q;
vector<vector<int>> adj(2e5);
for (int i=0; i<N-1; i++) {
int u, v;
cin >> u >> v;
u--, v--;
adj[u].push_back(v);
adj[v].push_back(u);
}
// analyse tree
vector<int> parent(2e5, -1);
vector<int> depth(2e5, 0);
vector<int> parity(2e5, 0);
vector<int> cost(2e5, 0);
set<int> upd;
auto dfs = [&](auto &self, int x) -> void {
for (int y : adj[x]) {
if (y != parent[x]) {
parent[y] = x;
depth[y] = depth[x] + 1;
self(self, y);
parity[x] ^= parity[y];
cost[x] += cost[y] + 1;
if (parity[y] == 0) cost[x] += 1;
}
}
if (adj[x].size() == 1) parity[x] ^= 1;
};
dfs(dfs, 0);
// check for subtask 6
bool subtask_6 = true;
vector<vector<int>> queries(Q);
for (int i=0; i<Q; i++) {
int D;
cin >> D;
if (D != 1) subtask_6 = false;
for (int j=0; j<D; j++) {
int a;
cin >> a;
queries[i].push_back(a);
}
}
if (subtask_6) {
vector<int> n_odd(N), n_even(N);
auto dfs = [&](auto &self, int x) -> void {
if (parity[x] == 1) n_odd[x] += 1;
if (parity[x] == 0) n_even[x] += 1;
for (int y : adj[x]) {
if (y != parent[x]) {
n_odd[y] += n_odd[x];
n_even[y] += n_even[x];
self(self, y);
}
}
};
dfs(dfs, 0);
for (int i=0; i<Q; i++) {
int x = queries[i][0];
x--;
if (adj[x].size() == 1) {
if (parity[0] == 1) {
cout << "-1\n";
} else {
cout << cost[0] + 1 << "\n";
}
} else {
if (parity[0] == 0) {
cout << "-1\n";
} else {
cout << cost[0] + n_odd[x] - n_even[x] << "\n";
}
}
}
return 0;
}
// handle queries
for (int i=0; i<Q; i++) {
set<pair<int,int>> upd;
for (int j=0; j<queries[i].size(); j++) {
int a = queries[i][j];
a--;
adj[a].push_back(N + j);
parity[N + j] = 1;
cost[N + j] = 0;
upd.insert({depth[a], a});
}
set<pair<int,int>> upd_copy(upd);
while (!upd.empty()) {
auto it = prev(upd.end());
int d = it->first, x = it->second;
upd.erase(it);
if (x == -1) continue;
parity[x] = 0;
cost[x] = 0;
for (int y : adj[x]) {
if (y != parent[x]) {
parity[x] ^= parity[y];
cost[x] += cost[y] + 1;
if (parity[y] == 0) cost[x] += 1;
}
}
if (adj[x].size() == 1) parity[x] ^= 1;
upd.insert({d - 1, parent[x]});
}
// if the root of the tree has odd parity, it is impossible to clean, otherwise output its cost
if (parity[0] == 1) {
cout << "-1\n";
} else {
cout << cost[0] << "\n";
}
if (i == Q - 1) continue; // no need to reverse changes on last query
// reverse changes
while (!upd_copy.empty()) {
auto it = prev(upd_copy.end());
int d = it->first, x = it->second;
upd_copy.erase(it);
if (x == -1) continue;
while (adj[x].back() >= N) adj[x].pop_back();
parity[x] = 0;
cost[x] = 0;
for (int y : adj[x]) {
if (y != parent[x]) {
parity[x] ^= parity[y];
cost[x] += cost[y] + 1;
if (parity[y] == 0) cost[x] += 1;
}
}
if (adj[x].size() == 1) parity[x] ^= 1;
upd_copy.insert({d - 1, parent[x]});
}
}
return 0;
}
Compilation message
cleaning.cpp: In function 'int main()':
cleaning.cpp:91:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int j=0; j<queries[i].size(); j++) {
| ~^~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
131 ms |
10076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
9172 KB |
Output is correct |
2 |
Correct |
15 ms |
9176 KB |
Output is correct |
3 |
Correct |
17 ms |
12260 KB |
Output is correct |
4 |
Correct |
40 ms |
15852 KB |
Output is correct |
5 |
Correct |
53 ms |
17772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
9948 KB |
Output is correct |
2 |
Correct |
16 ms |
9944 KB |
Output is correct |
3 |
Correct |
27 ms |
16648 KB |
Output is correct |
4 |
Correct |
52 ms |
21508 KB |
Output is correct |
5 |
Correct |
24 ms |
15704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
398 ms |
10068 KB |
Output is correct |
2 |
Correct |
48 ms |
9308 KB |
Output is correct |
3 |
Correct |
6 ms |
9048 KB |
Output is correct |
4 |
Correct |
7 ms |
9308 KB |
Output is correct |
5 |
Correct |
356 ms |
9308 KB |
Output is correct |
6 |
Correct |
406 ms |
9808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
14828 KB |
Output is correct |
2 |
Correct |
201 ms |
12708 KB |
Output is correct |
3 |
Correct |
156 ms |
11348 KB |
Output is correct |
4 |
Correct |
208 ms |
12884 KB |
Output is correct |
5 |
Correct |
196 ms |
12708 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
18768 KB |
Output is correct |
2 |
Correct |
51 ms |
21844 KB |
Output is correct |
3 |
Correct |
43 ms |
21504 KB |
Output is correct |
4 |
Correct |
44 ms |
22096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8280 KB |
Output is correct |
2 |
Correct |
131 ms |
10076 KB |
Output is correct |
3 |
Correct |
14 ms |
9172 KB |
Output is correct |
4 |
Correct |
15 ms |
9176 KB |
Output is correct |
5 |
Correct |
17 ms |
12260 KB |
Output is correct |
6 |
Correct |
40 ms |
15852 KB |
Output is correct |
7 |
Correct |
53 ms |
17772 KB |
Output is correct |
8 |
Correct |
16 ms |
9948 KB |
Output is correct |
9 |
Correct |
16 ms |
9944 KB |
Output is correct |
10 |
Correct |
27 ms |
16648 KB |
Output is correct |
11 |
Correct |
52 ms |
21508 KB |
Output is correct |
12 |
Correct |
24 ms |
15704 KB |
Output is correct |
13 |
Correct |
398 ms |
10068 KB |
Output is correct |
14 |
Correct |
48 ms |
9308 KB |
Output is correct |
15 |
Correct |
6 ms |
9048 KB |
Output is correct |
16 |
Correct |
7 ms |
9308 KB |
Output is correct |
17 |
Correct |
356 ms |
9308 KB |
Output is correct |
18 |
Correct |
406 ms |
9808 KB |
Output is correct |
19 |
Correct |
25 ms |
14828 KB |
Output is correct |
20 |
Correct |
201 ms |
12708 KB |
Output is correct |
21 |
Correct |
156 ms |
11348 KB |
Output is correct |
22 |
Correct |
208 ms |
12884 KB |
Output is correct |
23 |
Correct |
196 ms |
12708 KB |
Output is correct |
24 |
Correct |
38 ms |
18768 KB |
Output is correct |
25 |
Correct |
51 ms |
21844 KB |
Output is correct |
26 |
Correct |
43 ms |
21504 KB |
Output is correct |
27 |
Correct |
44 ms |
22096 KB |
Output is correct |
28 |
Correct |
195 ms |
12880 KB |
Output is correct |
29 |
Execution timed out |
1043 ms |
15436 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |