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);
}
vector<int> v(N), par(N, -1);
auto dfs = [&](auto dfs, int x) -> void {
if (adj[x].size() == 1) {
v[x] = 1;
}
for (int y : adj[x]) {
if (y == par[x]) {
continue;
}
par[y] = x;
dfs(dfs, y);
v[x] ^= v[y];
}
};
dfs(dfs, 0);
int ans = 2 * (N - 1) - count(v.begin(), v.end(), 1);
vector<int> deg(N);
for (int i = 0; i < N; i++) {
deg[i] = adj[i].size();
}
while (Q--) {
int D;
cin >> D;
vector<int> a(D);
for (int &p : a) {
cin >> p; p--;
if (deg[p] != 1) {
ans += v[p] == 0 ? -1 : 1;
v[p] ^= 1;
}
deg[p]++;
int x = par[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) {
deg[p]--;
if (deg[p] != 1) {
ans += v[p] == 0 ? -1 : 1;
v[p] ^= 1;
}
int x = par[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... |