This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1e5;
int leafs[MAX_N + 1], parent[MAX_N + 1], a[MAX_N + 1];
vector<int> adj[MAX_N + 1];
int ans;
void dfs( int u, int p ) {
parent[u] = 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 );
}
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 ( adj[v].size() == 1 )
continue;
while ( v != 0 ) {
change -= 2 - leafs[v] % 2;
leafs[v]++;
change += 2 - leafs[v] % 2;
v = parent[v];
}
}
change -= 2 - leafs[r] % 2;
cout << ((n - 1 + d) % 2 == 0 ? n - 1 + d : -1) << "\n";
for ( int i = 0; i < d; i++ ) {
int v = a[i];
if ( adj[v].size() == 1 )
continue;
while ( v != 0 ) {
leafs[v]--;
v = parent[v];
}
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |