/**
* author: wxhtzdy
* created: 01.07.2022 19:07:12
**/
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v;
--u; --v;
g[u].push_back(v);
g[v].push_back(u);
}
while (q--) {
int d;
cin >> d;
vector<int> a(d);
g.resize(n + d);
for (int i = 0; i < d; i++) {
cin >> a[i];
--a[i];
g[a[i]].push_back(n + i);
}
vector<int> sz(n + d);
function<void(int, int)> Dfs = [&](int v, int pr) {
sz[v] = 1;
for (int u : g[v]) {
if (u != pr) {
Dfs(u, v);
sz[v] += sz[u];
}
}
};
Dfs(0, 0);
int root = -1;
Dfs = [&](int v, int pr) {
bool ok = true;
for (int u : g[v]) {
if (sz[u] * 2 > n + d) {
ok = false;
}
}
if (v < n && ok) {
root = v;
}
for (int u : g[v]) {
if (u != pr) {
sz[v] -= sz[u];
sz[u] += sz[v];
Dfs(u, v);
sz[u] -= sz[v];
sz[v] += sz[u];
}
}
};
Dfs(0, 0);
assert(root != -1);
const int L = 20;
vector<vector<int>> jump(n + d, vector<int>(L));
vector<int> dep(n + d);
vector<int> ver;
Dfs = [&](int v, int pr) {
if ((int) g[v].size() <= 1) {
ver.push_back(v);
}
jump[v][0] = pr;
dep[v] = dep[pr] + 1;
for (int u : g[v]) {
if (u != pr) {
Dfs(u, v);
}
}
};
Dfs(root, root);
for (int j = 1; j < L; j++) {
for (int i = 0; i < n + d; i++) {
jump[i][j] = jump[jump[i][j - 1]][j - 1];
}
}
auto LCA = [&](int u, int v) {
if (dep[u] > dep[v]) {
swap(u, v);
}
for (int i = L - 1; i >= 0; i--) {
if (dep[jump[v][i]] >= dep[u]) {
v = jump[v][i];
}
}
for (int i = L - 1; i >= 0; i--) {
if (jump[u][i] != jump[v][i]) {
u = jump[u][i];
v = jump[v][i];
}
}
return u == v ? v : jump[v][0];
};
auto Dist = [&](int u, int v) {
return dep[u] + dep[v] - 2 * dep[LCA(u, v)];
};
if ((int) ver.size() % 2 == 1) {
cout << -1 << '\n';
} else {
int ans = 0;
int sz = (int) ver.size();
for (int i = 0; i < sz / 2; i++) {
ans += Dist(ver[i], ver[sz / 2 + i]);
}
cout << ans << '\n';
}
for (int i = 0; i < d; i++) {
g[a[i]].pop_back();
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1078 ms |
4700 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
53 ms |
14544 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
15444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
5480 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
12832 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1091 ms |
19108 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Execution timed out |
1078 ms |
4700 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |