#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], zleaf[N], sz[N], top[N], timer, pos[N], lvl[N], a[N];
vector<int> ch[N];
void init(int x) {
sz[x] = 1;
if ((int)ch[x].size() + (x != 1) == 1) zleaf[x] = 1;
for (int& y : ch[x]) {
lvl[y] = lvl[x] + 1;
ch[y].erase(find(ch[y].begin(), ch[y].end(), par[y] = x));
init(y);
sz[x] += sz[y];
zleaf[x] ^= zleaf[y];
if (sz[y] > sz[ch[x][0]]) swap(ch[x][0], y);
}
}
void dfs(int x) {
pos[x] = timer++;
for (int y : ch[x]) {
top[y] = ch[x][0] == y ? top[x] : y;
dfs(y);
}
}
void upd(int l, int r) {
FOR(i, l, r - 1) a[i] ^= 1;
}
void flip(int x) {
for (; x; x = par[top[x]]) upd(pos[top[x]], pos[x] + 1);
}
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);
}
top[1] = 1, init(1), dfs(1);
FOR(x, 1, n) a[pos[x]] = zleaf[x];
while (q--) {
int D;
cin >> D;
int Zleaf = zleaf[1] ^ D;
vector<int> u(D);
for (auto& x : u) {
cin >> x;
if (!seen[x] && ch[x].size() + (x != 1) == 1)
seen[x] = 1, Zleaf ^= 1;
else flip(x);
}
int even = 0;
FOR(i, 0, n - 1) if (a[i] % 2 == 0) even++;
cout << (Zleaf % 2 ? -1 : n - 1 + D + even - 1) << "\n";
for (int x : u) {
if (seen[x] && ch[x].size() + (x != 1) == 1)
seen[x] = 0, Zleaf ^= 1;
else flip(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... |