#include <iostream>
#include <vector>
using namespace std;
struct node {
int num = -2;
vector<node*> conns;
};
node empty_node;
int check_subtree(node* root, int parent_num) {
int ans = 0;
int odd_children_num = 0;
for (auto child : root->conns) {
if (child->num == parent_num) continue;
int child_cost = check_subtree(child, root->num);
if (child_cost >= 0) {
ans += (child_cost + 1);
odd_children_num++;
}
else ans += (2 - child_cost);
}
if (odd_children_num % 2 == 1) return ans;
else return -ans;
}
void answer(node* root) {
if (root->conns.size() == 1) {
if (root->conns[0]->conns.size() == 1) cout << 1 << endl;
else answer(root->conns[0]);
return;
}
int total_cost = check_subtree(root, -1);
if (total_cost < 0) cout << -total_cost;
else cout << -1;
cout << endl;
}
int main()
{
int n, q;
cin >> n >> q;
vector<node> vec(n);
int a, b;
vec[0].num = 0;
int leaves_num = n;
for (int i = 1; i < n; i++) {
vec[i].num = i;
cin >> a >> b;
a--; b--;
vec[a].conns.push_back(&vec[b]);
vec[b].conns.push_back(&vec[a]);
if (vec[a].conns.size() == 2) leaves_num--;
if (vec[b].conns.size() == 2) leaves_num--;
}
int new_num;
for (int i = 0; i < q; i++) {
int new_leaves_num = 0;
cin >> new_num;
vector<int> added;
for (int j = 0; j < new_num; j++) {
cin >> a; a--;
added.push_back(a);
vec[a].conns.push_back(&empty_node);
if (vec[a].conns.size() != 2)
new_leaves_num++;
}
if ((leaves_num + new_leaves_num) % 2 == 1) cout << -1 << endl;
else answer(&vec[0]);
for (int i : added) {
vec[i].conns.erase(vec[i].conns.begin() + vec[i].conns.size() - 1);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
2080 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
1828 KB |
Output is correct |
2 |
Correct |
12 ms |
1976 KB |
Output is correct |
3 |
Correct |
25 ms |
7372 KB |
Output is correct |
4 |
Correct |
35 ms |
6552 KB |
Output is correct |
5 |
Correct |
42 ms |
8784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2512 KB |
Output is correct |
2 |
Correct |
13 ms |
2168 KB |
Output is correct |
3 |
Correct |
37 ms |
13012 KB |
Output is correct |
4 |
Correct |
52 ms |
13768 KB |
Output is correct |
5 |
Correct |
34 ms |
11604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
2844 KB |
Output is correct |
2 |
Correct |
82 ms |
1916 KB |
Output is correct |
3 |
Correct |
67 ms |
1624 KB |
Output is correct |
4 |
Correct |
8 ms |
2136 KB |
Output is correct |
5 |
Correct |
131 ms |
2544 KB |
Output is correct |
6 |
Correct |
182 ms |
2244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1058 ms |
5584 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1061 ms |
7304 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
2080 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |