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 i64 = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q;
cin >> n >> q;
vector<vector<int>> e(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
e[u].push_back(v);
e[v].push_back(u);
}
while (q--) {
int k;
cin >> k;
vector<int> d(k);
for (int i = 0; i < k; i++) {
cin >> d[i];
d[i]--;
}
auto adj = e;
adj.resize(n + k);
for (int i = 0; i < k; i++) {
adj[d[i]].push_back(i + n);
}
int ans = 0;
function<int(int, int)> dfs = [&](int u, int p) {
int c = adj[u].size() <= 1;
for (int v : adj[u]) {
if (v != p) {
int s = dfs(v, u);
if (s % 2 == 0) {
ans += 2;
} else {
ans += 1;
}
c += s;
}
}
return c;
};
int c = dfs(0, -1);
if (c % 2 == 1) {
cout << "-1\n";
} else {
cout << ans << "\n";
}
}
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... |