#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
const int N = 1e5 + 69;
const int K = 17;
int n, q, ch[N], par[N], leaf[N], is[N], dep[N];
vector<int> g[N];
void dfs(int v, int p) {
par[v] = p;
if (g[v].size() == 1)
leaf[v] = ch[v] = is[v] = 1;
for (auto u : g[v]) {
if (u == p)
continue;
dep[u] = dep[v] + 1;
dfs(u, v);
ch[v] += ch[u];
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> q;
for (int i = 1;i <= n - 1;i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs(1, 0);
int uk = ch[1];
int ans = 0;
for (int i = 2;i <= n;i++)
ans += (ch[i] % 2 == 0 ? 2 : 1);
while (q--) {
int t;
cin >> t;
int a[t];
int res = ans + t;
for (int i = 0;i < t;i++) {
cin >> a[i];
int v = a[i];
uk++;
if (leaf[v]) {
leaf[v]--;
uk--;
while (v != 1) {
res -= (ch[v] % 2 == 0 ? 2 : 1);
ch[v]--;
res += (ch[v] % 2 == 0 ? 2 : 1);
v = par[v];
}
}
v = a[i];
while (v != 1) {
res -= (ch[v] % 2 == 0 ? 2 : 1);
ch[v]++;
res += (ch[v] % 2 == 0 ? 2 : 1);
v = par[v];
}
}
if (uk % 2 == 1)
cout << "-1\n";
else
cout << res << '\n';
for (int i = 0;i < t;i++) {
uk--;
int v = a[i];
if (is[a[i]] && !leaf[a[i]]) {
uk++;
leaf[a[i]] = 1;
while (v != 1) {
res -= (ch[v] % 2 == 0 ? 2 : 1);
ch[v]++;
res += (ch[v] % 2 == 0 ? 2 : 1);
v = par[v];
}
}
v = a[i];
while (v != 1) {
res -= (ch[v] % 2 == 0 ? 2 : 1);
ch[v]--;
res += (ch[v] % 2 == 0 ? 2 : 1);
v = par[v];
}
}
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
25 ms |
5980 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
5468 KB |
Output is correct |
2 |
Correct |
6 ms |
5548 KB |
Output is correct |
3 |
Correct |
21 ms |
9000 KB |
Output is correct |
4 |
Correct |
21 ms |
8208 KB |
Output is correct |
5 |
Correct |
24 ms |
9740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
819 ms |
5692 KB |
Output is correct |
2 |
Correct |
841 ms |
5700 KB |
Output is correct |
3 |
Correct |
450 ms |
16356 KB |
Output is correct |
4 |
Execution timed out |
1032 ms |
15188 KB |
Time limit exceeded |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1022 ms |
6332 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
8032 KB |
Output is correct |
2 |
Correct |
50 ms |
8024 KB |
Output is correct |
3 |
Correct |
38 ms |
6712 KB |
Output is correct |
4 |
Correct |
40 ms |
8028 KB |
Output is correct |
5 |
Correct |
51 ms |
8144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
10064 KB |
Output is correct |
2 |
Execution timed out |
1055 ms |
12072 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4700 KB |
Output is correct |
2 |
Correct |
25 ms |
5980 KB |
Output is correct |
3 |
Correct |
8 ms |
5468 KB |
Output is correct |
4 |
Correct |
6 ms |
5548 KB |
Output is correct |
5 |
Correct |
21 ms |
9000 KB |
Output is correct |
6 |
Correct |
21 ms |
8208 KB |
Output is correct |
7 |
Correct |
24 ms |
9740 KB |
Output is correct |
8 |
Correct |
819 ms |
5692 KB |
Output is correct |
9 |
Correct |
841 ms |
5700 KB |
Output is correct |
10 |
Correct |
450 ms |
16356 KB |
Output is correct |
11 |
Execution timed out |
1032 ms |
15188 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |