#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5;
int leafs[MAX_N + 1], a[MAX_N];
vector<int> adj[MAX_N + 1];
int ans;
void dfs( int u, int p ) {
leafs[u] = (adj[u].size() == 1);
for ( int v: adj[u] ) {
if ( v == p )
continue;
dfs( v, u );
leafs[u] += leafs[v];
}
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 );
}
for ( int i = 0; i < q; i++ ) {
int d;
cin >> d;
for ( int i = 0; i < d; i++ ) {
cin >> a[i];
int p = a[i], v = n + 1 + i;
adj[p].push_back( v );
adj[v].push_back( p );
}
int r = 1;
while ( adj[r].size() == 1 )
r++;
ans = 0;
dfs( r, 0 );
ans -= 2 - leafs[r] % 2;
cout << (leafs[r] % 2 == 0 ? ans : -1) << "\n";
for ( int i = d - 1; i >= 0; i-- ) {
int p = a[i], v = n + 1 + i;
adj[p].pop_back();
adj[v].pop_back();
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
6084 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
8592 KB |
Output is correct |
2 |
Correct |
23 ms |
8796 KB |
Output is correct |
3 |
Correct |
42 ms |
9216 KB |
Output is correct |
4 |
Correct |
66 ms |
12492 KB |
Output is correct |
5 |
Correct |
75 ms |
13876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
9296 KB |
Output is correct |
2 |
Correct |
31 ms |
9324 KB |
Output is correct |
3 |
Correct |
56 ms |
16212 KB |
Output is correct |
4 |
Correct |
104 ms |
20532 KB |
Output is correct |
5 |
Correct |
56 ms |
16220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
183 ms |
6492 KB |
Output is correct |
2 |
Correct |
114 ms |
5852 KB |
Output is correct |
3 |
Correct |
154 ms |
5720 KB |
Output is correct |
4 |
Correct |
203 ms |
6236 KB |
Output is correct |
5 |
Correct |
162 ms |
6488 KB |
Output is correct |
6 |
Correct |
219 ms |
6620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1050 ms |
7252 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1018 ms |
8528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4952 KB |
Output is correct |
2 |
Execution timed out |
1090 ms |
6084 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |