#include <bits/stdc++.h>
#define FOR(i, a, b) for (auto i = (a); i <= (b); i++)
#define ROF(i, a, b) for (auto i = (a); i >= (b); i--)
using namespace std;
using ll = long long;
const int N = 1e5 + 5;
bool seen[N];
int par[N], sz[N], even = 0;
vector<int> ch[N];
void dfs(int x) {
if ((int)ch[x].size() + (x != 1) == 1) sz[x] = 1;
for (int y : ch[x]) {
ch[y].erase(find(ch[y].begin(), ch[y].end(), par[y] = x));
dfs(y);
sz[x] += sz[y];
}
if (sz[x] % 2 == 0) even++;
}
void add(int x, int z) {
for (int y = x; y; y = par[y]) {
even += sz[y] % 2 == 0 ? -1 : 1;
sz[y] += z;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
FOR(i, 1, n - 1) {
int a, b;
cin >> a >> b;
ch[a].push_back(b);
ch[b].push_back(a);
}
dfs(1);
while (q--) {
int D;
cin >> D;
vector<int> a(D);
for (auto& x : a) {
cin >> x;
add(x, 1);
if (!seen[x] && ch[x].size() + (x != 1) == 1)
add(x, -1), seen[x] = 1;
}
cout << (sz[1] % 2 ? -1 : n - 1 + D + even - 1) << "\n";
for (int x : a) {
add(x, -1);
if (seen[x] && ch[x].size() + (x != 1) == 1)
add(x, 1), seen[x] = 0;
}
}
return 6/22;
}
# | 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... |