#include <bits/stdc++.h>
using namespace std;
const int Z = 1e5, B = 17;
int N, Q;
vector<int> g[Z], h[Z];
int p[Z][B], L[Z], R[Z], s[Z], d[Z], dfsTimer, rt, leafCnt, ext;
void init(int u) {
L[u] = dfsTimer++;
for(int i = 0; i + 1 < B; ++i)
p[u][i+1] = p[p[u][i]][i];
s[u] = size(g[u]) > 1;
leafCnt += !s[u];
for(const int &v : g[u]) if(v != p[u][0]) {
p[v][0] = u;
d[v] = d[u] + 1;
init(v);
s[u] ^= s[v] ^ 1;
}
if(u != rt) ext += s[u];
R[u] = dfsTimer++;
}
void dfs(int u) {
for(const int &v : g[u]) if(v != p[u][0])
s[v] += s[u], dfs(v);
}
#define isAncestor(U, V) (L[U] < L[V] && R[V] < R[U])
int lca(int u, int v) {
if(isAncestor(u, v)) return u;
for(int i = B; i--; ) if(!isAncestor(p[u][i], v)) u = p[u][i];
return p[u][0];
}
int diff, c[Z];
void calc(int u, int par) {
for(const int &v : h[u]) if(v != par) {
calc(v, u);
c[u] ^= c[v];
}
if(c[u]) {
diff += (d[u] - d[par]) - 2 * (s[u] - s[par]);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> Q;
for(int i = 1; i < N; ++i) {
int u, v; cin >> u >> v;
--u, --v;
g[u].push_back(v);
g[v].push_back(u);
}
for(; size(g[rt]) < 2; ++rt);
p[rt][0] = rt;
init(rt);
dfs(rt);
while(Q--) {
int D; cin >> D;
int leafDiff = diff = 0;
vector<int> a(D);
for(int &u : a) {
cin >> u;
if(size(g[--u]) < 2) leafDiff += !c[u]++;
}
for(int &u : a) c[u] = !!c[u] ^ 1;
for(int t : {1, 0}) {
sort(begin(a), end(a), [&](const int &i, const int &j) {
return L[i] < L[j];
});
if(t) for(int i = D; --i; )
a.push_back(lca(a[i], a[i-1]));
}
a.erase(unique(begin(a), end(a)), end(a));
vector<int> st;
for(const int &u : a) {
if(!empty(st)) {
for(; !isAncestor(st.back(), u); st.pop_back());
h[st.back()].push_back(u);
}
st.push_back(u);
}
calc(a[0], rt);
for(const int &u : a) {
h[u].clear();
c[u] = 0;
}
cout << ((leafCnt + D - leafDiff) & 1 ? -1 : N - 1 + D + ext + diff) << '\n';
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
50 ms |
7840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
6176 KB |
Output is correct |
2 |
Correct |
26 ms |
6188 KB |
Output is correct |
3 |
Correct |
43 ms |
17372 KB |
Output is correct |
4 |
Correct |
60 ms |
14756 KB |
Output is correct |
5 |
Correct |
78 ms |
18456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
6876 KB |
Output is correct |
2 |
Correct |
34 ms |
6880 KB |
Output is correct |
3 |
Correct |
59 ms |
24644 KB |
Output is correct |
4 |
Correct |
96 ms |
26272 KB |
Output is correct |
5 |
Correct |
47 ms |
22600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
8652 KB |
Output is correct |
2 |
Correct |
30 ms |
7720 KB |
Output is correct |
3 |
Correct |
11 ms |
7508 KB |
Output is correct |
4 |
Correct |
13 ms |
8120 KB |
Output is correct |
5 |
Correct |
14 ms |
8276 KB |
Output is correct |
6 |
Correct |
42 ms |
8460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
13060 KB |
Output is correct |
2 |
Correct |
94 ms |
13232 KB |
Output is correct |
3 |
Correct |
62 ms |
9296 KB |
Output is correct |
4 |
Correct |
97 ms |
13248 KB |
Output is correct |
5 |
Correct |
97 ms |
13236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
17492 KB |
Output is correct |
2 |
Correct |
105 ms |
20108 KB |
Output is correct |
3 |
Correct |
117 ms |
20232 KB |
Output is correct |
4 |
Correct |
107 ms |
19644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
50 ms |
7840 KB |
Output is correct |
3 |
Correct |
25 ms |
6176 KB |
Output is correct |
4 |
Correct |
26 ms |
6188 KB |
Output is correct |
5 |
Correct |
43 ms |
17372 KB |
Output is correct |
6 |
Correct |
60 ms |
14756 KB |
Output is correct |
7 |
Correct |
78 ms |
18456 KB |
Output is correct |
8 |
Correct |
34 ms |
6876 KB |
Output is correct |
9 |
Correct |
34 ms |
6880 KB |
Output is correct |
10 |
Correct |
59 ms |
24644 KB |
Output is correct |
11 |
Correct |
96 ms |
26272 KB |
Output is correct |
12 |
Correct |
47 ms |
22600 KB |
Output is correct |
13 |
Correct |
45 ms |
8652 KB |
Output is correct |
14 |
Correct |
30 ms |
7720 KB |
Output is correct |
15 |
Correct |
11 ms |
7508 KB |
Output is correct |
16 |
Correct |
13 ms |
8120 KB |
Output is correct |
17 |
Correct |
14 ms |
8276 KB |
Output is correct |
18 |
Correct |
42 ms |
8460 KB |
Output is correct |
19 |
Correct |
57 ms |
13060 KB |
Output is correct |
20 |
Correct |
94 ms |
13232 KB |
Output is correct |
21 |
Correct |
62 ms |
9296 KB |
Output is correct |
22 |
Correct |
97 ms |
13248 KB |
Output is correct |
23 |
Correct |
97 ms |
13236 KB |
Output is correct |
24 |
Correct |
102 ms |
17492 KB |
Output is correct |
25 |
Correct |
105 ms |
20108 KB |
Output is correct |
26 |
Correct |
117 ms |
20232 KB |
Output is correct |
27 |
Correct |
107 ms |
19644 KB |
Output is correct |
28 |
Correct |
89 ms |
12648 KB |
Output is correct |
29 |
Correct |
129 ms |
20712 KB |
Output is correct |
30 |
Correct |
74 ms |
18440 KB |
Output is correct |
31 |
Correct |
101 ms |
26252 KB |
Output is correct |
32 |
Correct |
94 ms |
13320 KB |
Output is correct |
33 |
Correct |
139 ms |
18412 KB |
Output is correct |
34 |
Correct |
171 ms |
21580 KB |
Output is correct |
35 |
Correct |
184 ms |
21508 KB |
Output is correct |