#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5;
int leafs[MAX_N + 1], parent[MAX_N + 1], children[MAX_N + 1], a[MAX_N + 1];
vector<int> adj[MAX_N + 1];
int ans;
void dfs( int u, int p ) {
parent[u] = p;
for ( int v: adj[u] ) {
if ( v == p )
continue;
children[u]++;
dfs( v, u );
leafs[u] += leafs[v];
}
leafs[u] += (children[u] == 0);
ans += 2 - leafs[u] % 2;
}
int main() {
int n, q;
cin >> n >> q;
for ( int i = 0; i < n - 1; i++ ) {
int u, v;
cin >> u >> v;
adj[u].push_back( v );
adj[v].push_back( u );
}
int r = 1;
while ( adj[r].size() == 1 )
r++;
ans = 0;
dfs( r, 0 );
for ( int i = 0; i < q; i++ ) {
int d;
int change = 0;
cin >> d;
for ( int i = 0; i < d; i++ ) {
cin >> a[i];
int v = a[i];
change++;
if ( children[v] == 0 ) {
children[v]++;
continue;
}
while ( v != 0 ) {
change -= 2 - leafs[v] % 2;
leafs[v]++;
children[v]++;
change += 2 - leafs[v] % 2;
v = parent[v];
}
}
change -= 2 - leafs[r] % 2;
cout << (leafs[r] % 2 == 0 ? ans + change : -1) << "\n";
for ( int i = 0; i < d; i++ ) {
int v = a[i];
if ( children[v] == 1 ) {
children[v]--;
continue;
}
while ( v != 0 ) {
leafs[v]--;
children[v]--;
v = parent[v];
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
45 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
3160 KB |
Output is correct |
2 |
Correct |
16 ms |
3424 KB |
Output is correct |
3 |
Correct |
42 ms |
8136 KB |
Output is correct |
4 |
Correct |
60 ms |
7368 KB |
Output is correct |
5 |
Correct |
60 ms |
8648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
915 ms |
3916 KB |
Output is correct |
2 |
Correct |
916 ms |
3664 KB |
Output is correct |
3 |
Correct |
504 ms |
14704 KB |
Output is correct |
4 |
Execution timed out |
1079 ms |
14096 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1036 ms |
4288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
5872 KB |
Output is correct |
2 |
Correct |
70 ms |
5972 KB |
Output is correct |
3 |
Correct |
52 ms |
4948 KB |
Output is correct |
4 |
Correct |
79 ms |
6740 KB |
Output is correct |
5 |
Correct |
72 ms |
6972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
226 ms |
8016 KB |
Output is correct |
2 |
Execution timed out |
1042 ms |
9928 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2648 KB |
Output is correct |
2 |
Correct |
45 ms |
3676 KB |
Output is correct |
3 |
Correct |
16 ms |
3160 KB |
Output is correct |
4 |
Correct |
16 ms |
3424 KB |
Output is correct |
5 |
Correct |
42 ms |
8136 KB |
Output is correct |
6 |
Correct |
60 ms |
7368 KB |
Output is correct |
7 |
Correct |
60 ms |
8648 KB |
Output is correct |
8 |
Correct |
915 ms |
3916 KB |
Output is correct |
9 |
Correct |
916 ms |
3664 KB |
Output is correct |
10 |
Correct |
504 ms |
14704 KB |
Output is correct |
11 |
Execution timed out |
1079 ms |
14096 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |