#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
vector<int> t[maxn];
int n, sz[maxn], Time = 0, st[maxn], par[maxn], up[maxn];
int seg[4*maxn], lazy[4*maxn];
void shift(int,int,int);
void change(int id, int L, int R, int l, int r){
if (r <= L or R <= l)
return;
if (l <= L and R <= r){
seg[id] = (R-L)-seg[id];
lazy[id] ^= 1;
return;
}
shift(id,L,R);
int mid = (L + R) >> 1;
change(2*id+0, L, mid, l, r);
change(2*id+1, mid, R, l, r);
seg[id] = seg[2*id+0] + seg[2*id+1];
}
void shift(int id, int L, int R){
if (lazy[id] == 0)
return;
int mid = (L + R) >> 1;
change(2*id+0,L,mid,L,mid);
change(2*id+1,mid,R,mid,R);
lazy[id] = 0;
}
void change(int v){
while (up[v] != 1){
change(1, 1, n, st[up[v]], st[v]+1);
v = par[up[v]];
}
if (v != 1)
change(1, 1, n, st[up[v]]+1, st[v]+1);
}
void dfs(int v){
st[v] = Time++;
bool bigChild = true;
for (auto u : t[v]){
if (bigChild == true){
up[u] = up[v], par[u] = v;
dfs(u);
bigChild = false;
continue;
}
up[u] = u, par[u] = v;
dfs(u);
}
}
int dfsSize(int v, int par = -1){
sz[v] = 1;
for (auto u : t[v])
if (u != par)
sz[v] += dfsSize(u,v);
for (int i = t[v].size()-1; i >= 0; i--)
if (t[v][i] == par)
t[v].erase(t[v].begin()+i);
sort(t[v].begin(),t[v].end(),[](int x, int y){
return sz[x] > sz[y];
});
return sz[v];
}
bool isleaf[maxn];
int mnH, mxH;
void checkPerfect(int v, int par = -1, int h = 0) {
if ((int)t[v].size() == 1) {
mnH = min(mnH, h);
mxH = max(mxH, h);
}
for (auto u : t[v])
if (u != par)
checkPerfect(u, v, h + 1);
}
int main(){
ios_base::sync_with_stdio(false);
int q;
cin >> n >> q;
for (int i = 0; i < n-1; i++){
int v, u;
cin >> v >> u;
t[v].push_back(u);
t[u].push_back(v);
}
assert((int)t[1].size() == 2);
for (int i = 2; i <= n; i++)
assert((int)t[i].size() == 1 || (int)t[i].size() == 3);
mnH = n;
checkPerfect(1);
assert(mnH == mxH);
int cntLeaf = 0;
for (int i = 1; i <= n; i++){
isleaf[i] = (t[i].size() == 1);
cntLeaf += isleaf[i];
}
dfsSize(1);
up[1] = 1;
dfs(1);
for (int i = 1; i <= n; i++)
if (isleaf[i])
change(i);
while (q --){
int d;
cin >> d;
vector<int> a(d);
for (int i = 0; i < d; i++)
cin >> a[i];
sort(a.begin(), a.end());
int cnt = cntLeaf;
for (int i = 0; i < d; i++){
cnt ++;
if ((i == 0 or a[i] != a[i-1]) and isleaf[a[i]])
cnt --;
}
if (cnt % 2 == 1){
cout << -1 << '\n';
continue;
}
for (int i = 0; i < d; i++){
change(a[i]);
if ((i == 0 or a[i] != a[i-1]) and isleaf[a[i]])
change(a[i]);
}
cout << n+d-1 + (n-1-seg[1]) << '\n';
for (int i = 0; i < d; i++){
change(a[i]);
if ((i == 0 or a[i] != a[i-1]) and isleaf[a[i]])
change(a[i]);
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
5196 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
5 ms |
5452 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
6 ms |
5512 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
11 ms |
6536 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
429 ms |
7316 KB |
Output is correct |
2 |
Correct |
657 ms |
8296 KB |
Output is correct |
3 |
Correct |
482 ms |
5764 KB |
Output is correct |
4 |
Correct |
622 ms |
8116 KB |
Output is correct |
5 |
Correct |
557 ms |
8120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
34 ms |
11812 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
4 ms |
5196 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |