#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct SegmentTree {
int n;
vector<int> t;
SegmentTree(int n) : n(n), t(2 * n) {}
int get(int l, int r) {
int res = 0;
for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
if (l & 1)
res ^= t[l++];
if (r & 1)
res ^= t[--r];
}
return res;
}
void update(int x) {
for (x += n; x > 0; x >>= 1) {
t[x] ^= 1;
}
}
};
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);
}
vector<int> c(n), in(n), out(n);
vector s(2, vector<int>(n));
int timeStamp = 0;
int cnt = 0;
function<void(int, int)> dfs = [&](int u, int p) {
in[u] = timeStamp++;
c[u] = e[u].size() <= 1;
cnt += c[u];
for (int v : e[u]) {
if (v != p) {
dfs(v, u);
c[u] ^= c[v];
}
}
out[u] = timeStamp;
};
dfs(0, -1);
int f[2] {};
dfs = [&](int u, int p) {
if (p != -1) {
s[1][u] += c[u];
s[0][u] += !c[u];
f[c[u]]++;
}
for (int v : e[u]) {
if (v != p) {
s[1][v] = s[1][u];
s[0][v] = s[0][u];
dfs(v, u);
}
}
};
dfs(0, -1);
SegmentTree t(n);
while (q--) {
int k;
cin >> k;
vector<int> d(k);
for (int i = 0; i < k; i++) {
cin >> d[i];
d[i]--;
}
set<int> v(d.begin(), d.end());
int leaves = cnt + k;
for (int x : v) {
if (e[x].size() <= 1) {
leaves--;
}
}
if (leaves % 2 != 0) {
cout << "-1\n";
continue;
}
int a[2] {f[0], f[1]};
set<int> done;
sort(d.begin(), d.end(), [&](int i, int j) {
return in[i] > in[j];
});
for (int u : d) {
if (e[u].size() <= 1 && !done.count(u)) {
// nothing?
} else {
int x = t.get(in[u], out[u]);
a[1] -= s[1 ^ x][u];
a[1] += s[0 ^ x][u];
a[0] -= s[0 ^ x][u];
a[0] += s[1 ^ x][u];
t.update(in[u]);
}
done.insert(u);
}
int ans = 2 * a[0] + a[1] + k;
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
46 ms |
2364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
1108 KB |
Output is correct |
2 |
Correct |
12 ms |
932 KB |
Output is correct |
3 |
Correct |
35 ms |
9320 KB |
Output is correct |
4 |
Correct |
107 ms |
11460 KB |
Output is correct |
5 |
Correct |
114 ms |
14768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
2008 KB |
Output is correct |
2 |
Correct |
13 ms |
1744 KB |
Output is correct |
3 |
Correct |
62 ms |
19384 KB |
Output is correct |
4 |
Correct |
104 ms |
23244 KB |
Output is correct |
5 |
Correct |
48 ms |
17520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
35 ms |
3028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
5988 KB |
Output is correct |
2 |
Incorrect |
108 ms |
5976 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
106 ms |
9148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
46 ms |
2364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |