#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int N, Q;
cin >> N >> Q;
vector<vector<int>> adj(N);
for (int i = 0; i < N - 1; i++) {
int x, y;
cin >> x >> y;
x--, y--;
adj[x].push_back(y);
adj[y].push_back(x);
}
vector<int> v(N), par(N, -1);
auto dfs = [&](auto dfs, int x) -> void {
if (adj[x].size() == 1) {
v[x] = 1;
}
for (int y : adj[x]) {
if (y == par[x]) {
continue;
}
par[y] = x;
dfs(dfs, y);
v[x] ^= v[y];
}
};
dfs(dfs, 0);
int ans = 2 * (N - 1) - count(v.begin(), v.end(), 1);
vector<int> deg(N);
for (int i = 0; i < N; i++) {
deg[i] = adj[i].size();
}
while (Q--) {
int D;
cin >> D;
vector<int> a(D);
for (int &p : a) {
cin >> p;
p--;
if (deg[p]++ != 1) {
int x = p;
while (x != -1) {
ans += v[x] == 0 ? -1 : 1;
v[x] ^= 1;
x = par[x];
}
}
}
if (v[0] == 1) {
cout << -1 << "\n";
} else {
cout << ans + D << "\n";
}
for (int p : a) {
if (--deg[p] != 1) {
int x = p;
while (x != -1) {
ans += v[x] == 0 ? -1 : 1;
v[x] ^= 1;
x = par[x];
}
}
}
}
return 6/22;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
18 ms |
1896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
1100 KB |
Output is correct |
2 |
Correct |
7 ms |
1232 KB |
Output is correct |
3 |
Correct |
18 ms |
8140 KB |
Output is correct |
4 |
Correct |
20 ms |
6604 KB |
Output is correct |
5 |
Correct |
25 ms |
8972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
749 ms |
1356 KB |
Output is correct |
2 |
Correct |
747 ms |
1376 KB |
Output is correct |
3 |
Execution timed out |
1068 ms |
11928 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1074 ms |
2308 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
5360 KB |
Output is correct |
2 |
Correct |
38 ms |
5984 KB |
Output is correct |
3 |
Correct |
22 ms |
3424 KB |
Output is correct |
4 |
Correct |
39 ms |
6060 KB |
Output is correct |
5 |
Correct |
36 ms |
5980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
7804 KB |
Output is correct |
2 |
Execution timed out |
1083 ms |
10216 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
18 ms |
1896 KB |
Output is correct |
3 |
Correct |
7 ms |
1100 KB |
Output is correct |
4 |
Correct |
7 ms |
1232 KB |
Output is correct |
5 |
Correct |
18 ms |
8140 KB |
Output is correct |
6 |
Correct |
20 ms |
6604 KB |
Output is correct |
7 |
Correct |
25 ms |
8972 KB |
Output is correct |
8 |
Correct |
749 ms |
1356 KB |
Output is correct |
9 |
Correct |
747 ms |
1376 KB |
Output is correct |
10 |
Execution timed out |
1068 ms |
11928 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |