#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) {
ans += v[p] == 0 ? -1 : 1;
v[p] ^= 1;
}
deg[p]++;
int x = par[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) {
deg[p]--;
if (deg[p] != 1) {
ans += v[p] == 0 ? -1 : 1;
v[p] ^= 1;
}
int x = par[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 |
Incorrect |
23 ms |
2388 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
1236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
758 ms |
1568 KB |
Output is correct |
2 |
Correct |
753 ms |
1580 KB |
Output is correct |
3 |
Execution timed out |
1059 ms |
12884 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1064 ms |
2604 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
6084 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
78 ms |
9640 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
23 ms |
2388 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |