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<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
while (Q--) {
int D;
cin >> D;
for (int i = 0; i < D; i++) {
int p;
cin >> p; p--;
adj.push_back({p});
adj[p].push_back(N + i);
}
int leaves = 0;
for (int i = 0; i < N + D; i++) {
leaves += adj[i].size() == 1;
}
if (leaves % 2 == 1) {
cout << -1 << "\n";
} else {
int ans = 0;
auto dfs = [&](auto dfs, int x, int p) -> int {
int s = adj[x].size() == 1;
for (int y : adj[x]) {
if (y == p) {
continue;
}
int t = dfs(dfs, y, x);
ans += 2 - t % 2;
s += t;
}
return s;
};
dfs(dfs, 0, -1);
cout << ans << "\n";
}
adj.resize(N);
for (int i = 0; i < N; i++) {
if (adj[i].back() >= N) {
adj[i].pop_back();
}
}
}
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... |