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;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<int> deg(N);
vector<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
deg[--x]++;
deg[--y]++;
adj[x].push_back(y);
adj[y].push_back(x);
}
int ans = 2 * (N - 1);
vector<int> v(N), par(N, -1);
auto dfs = [&](auto dfs, int x) -> void {
if (deg[x] == 1) {
v[x] = 1;
}
for (int y : adj[x]) {
if (y == par[x]) {
continue;
}
par[y] = x;
dfs(dfs, y);
v[x] ^= v[y];
}
ans -= v[x];
};
dfs(dfs, 0);
while (Q--) {
int D;
cin >> D;
vector<int> a(D);
for (int &p : a) {
cin >> p;
p--;
if (deg[p]++ != 1) {
int x = p;
while (x != -1) {
ans += v[x] == 0 ? -1 : 1;
v[x] ^= 1;
x = par[x];
}
}
}
if (v[0] == 1) {
cout << -1 << "\n";
} else {
cout << ans + D << "\n";
}
for (int p : a) {
if (--deg[p] != 1) {
int x = p;
while (x != -1) {
ans += v[x] == 0 ? -1 : 1;
v[x] ^= 1;
x = par[x];
}
}
}
}
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... |