#include <bits/stdc++.h>
#define ll long long
#define task "crt"
using namespace std;
const int N = 2e5 + 16;
int n, q, u, v, a[N];
vector <int> adj[N];
ll result;
/*
7 3
1 2
2 4
4 5
5 6
5 7
3 4
1 4
2 2 4
1 1
*/
int leaves[N], leafCnt, b[N];
void preDfs(int k, int p) {
if (adj[k].size() == 1) {
leaves[k] = 1;
}
else {
leaves[k] = 0;
}
for (auto v : adj[k]) {
if (v == p) {
continue;
}
preDfs(v, k);
leaves[k] += leaves[v];
}
}
int h[N], par[N], sz[N];
int chainHead[N], chainId[N], chainArr[N], pos[N], curPos = 1, curChain = 1, endPos[N];
void dfs(int k, int p) {
sz[k] = 1;
for (auto v : adj[k]) {
if (v == p) {
continue;
}
h[v] = h[k] + 1;
par[v] = k;
dfs(v, k);
sz[k] += sz[v];
}
}
void hld(int k, int p) {
if (!chainHead[curChain]) {
chainHead[curChain] = k;
}
chainId[k] = curChain;
pos[k] = curPos;
chainArr[curPos] = k;
curPos++;
int heaviest = 0;
for (auto v : adj[k]) {
if (v == p) {
continue;
}
if (heaviest == 0 || sz[v] > sz[heaviest]) {
heaviest = v;
}
}
if (heaviest) {
hld(heaviest, k);
}
for (auto v : adj[k]) {
if (v == p || v == heaviest) {
continue;
}
curChain++;
hld(v, k);
}
endPos[k] = curPos - 1;
}
int lca(int u, int v) {
while (chainId[u] != chainId[v]) {
if (chainId[u] > chainId[v]) {
u = par[chainHead[chainId[u]]];
}
else {
v = par[chainHead[chainId[v]]];
}
}
if (h[u] < h[v]) {
return u;
}
return v;
}
struct node {
int val = 0, lazy = 0;
node operator + (const node &other) const {
node ans;
ans.val = val + other.val;
return ans;
}
};
node segmentTree[N << 2];
void build(int k, int l, int r) {
if (l == r) {
segmentTree[k].val = b[chainArr[l]];
return;
}
int mid = l + r >> 1;
build(k << 1, l, mid);
build(k << 1 | 1, mid + 1, r);
segmentTree[k] = segmentTree[k << 1] + segmentTree[k << 1 | 1];
}
void down(int k, int l, int r) {
if (segmentTree[k].lazy) {
int mid = l + r >> 1;
segmentTree[k << 1].val = (mid - l + 1) - segmentTree[k << 1].val;
segmentTree[k << 1].lazy ^= 1;
segmentTree[k << 1 | 1].val = (r - mid) - segmentTree[k << 1 | 1].val;
segmentTree[k << 1 | 1].lazy ^= 1;
segmentTree[k].lazy ^= 1;
}
}
void update(int k, int l, int r, int x, int y) {
if (l > y || x > r) {
return;
}
if (l >= x && r <= y) {
segmentTree[k].val = (r - l + 1) - segmentTree[k].val;
segmentTree[k].lazy ^= 1;
return;
}
int mid = l + r >> 1;
down(k, l, r);
update(k << 1, l, mid, x, y);
update(k << 1 | 1, mid + 1, r, x, y);
segmentTree[k] = segmentTree[k << 1] + segmentTree[k << 1 | 1];
}
node get(int k, int l, int r, int x, int y) {
if (l > y || x > r) {
node ans;
return ans;
}
if (l >= x && r <= y) {
return segmentTree[k];
}
int mid = l + r >> 1;
down(k, l, r);
return get(k << 1, l, mid, x, y) + get(k << 1 | 1, mid + 1, r, x, y);
}
void updateTree(int u, int v) {
int LCA = lca(u, v);
while (chainId[u] != chainId[LCA]) {
update(1, 1, curPos, pos[chainHead[chainId[u]]], pos[u]);
u = par[chainHead[chainId[u]]];
}
while (chainId[v] != chainId[LCA]) {
update(1, 1, curPos, pos[chainHead[chainId[v]]], pos[v]);
v = par[chainHead[chainId[v]]];
}
if (h[u] < h[v]) {
update(1, 1, curPos, pos[u], pos[v]);
}
else {
update(1, 1, curPos, pos[v], pos[u]);
}
}
node getTree(int u, int v) {
node ans;
int LCA = lca(u, v);
while (chainId[u] != chainId[LCA]) {
ans = get(1, 1, curPos, pos[chainHead[chainId[u]]], pos[u]) + ans;
u = par[chainHead[chainId[u]]];
}
while (chainId[v] != chainId[LCA]) {
ans = get(1, 1, curPos, pos[chainHead[chainId[v]]], pos[v]) + ans;
v = par[chainHead[chainId[v]]];
}
if (h[u] < h[v]) {
ans = get(1, 1, curPos, pos[u], pos[v]) + ans;
}
else {
ans = get(1, 1, curPos, pos[v], pos[u]) + ans;
}
return ans;
}
node getSubtree(int k) {
return get(1, 1, curPos, pos[k], endPos[k]);
}
int main() {
ios_base :: sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin >> n >> q;
for (int i = 1; i < n; i++) {
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
for (int i = 1; i <= n; i++) {
leafCnt += (adj[i].size() == 1);
}
preDfs(1, 1);
for (int i = 1; i <= n; i++) {
if (leaves[i] & 1) {
b[i] = 1;
}
}
dfs(1, 1);
hld(1, 1);
build(1, 1, curPos);
while (q--) {
int d;
cin >> d;
set <int> distinct;
for (int i = 1; i <= d; i++) {
cin >> a[i];
if (adj[a[i]].size() == 1) {
distinct.insert(a[i]);
}
}
if ((leafCnt - distinct.size() + d) & 1) {
cout << -1 << "\n";
continue;
}
distinct.clear();
vector <int> lst;
for (int i = 1; i <= d; i++) {
if (adj[a[i]].size() == 1) {
auto it = distinct.lower_bound(a[i]);
if (it != distinct.end()) {
lst.push_back(a[i]);
}
distinct.insert(a[i]);
}
else {
lst.push_back(a[i]);
}
}
for (auto x : lst) {
updateTree(1, x);
}
result = 0;
ll odd = getSubtree(1).val + d;
ll even = n + d - odd;
result += odd + 2 * even;
if (getTree(1, 1).val == 1) {
result--;
}
else {
result -= 2;
}
cout << result << "\n";
for (auto x : lst) {
updateTree(1, x);
}
}
return 0;
}
Compilation message (stderr)
cleaning.cpp: In function 'int main()':
cleaning.cpp:217:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
217 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cleaning.cpp:218:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
218 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |