#include <bits/stdc++.h>
typedef long long ll;
#define FOR(i,x,y) for(ll i=x; i<y; i++)
#define FORNEG(i,x,y) for(ll i=x; i>y; i--)
using namespace std;
vector<ll> edges[200005];
vector<ll> vtree[200005];
ll realans = 0;
ll vals[200005];
ll contrib[200005];
ll depth[200005];
ll par[200005];
ll tin[200005];
ll timer = 0;
map<ll, ll> lmao;
ll lmfao = 0;
ll dp(ll a, ll p){
tin[a] = timer++;
par[a] = p;
ll ans = 0;
if (edges[a].size() <= 1) ans = 1;
for (auto&i : edges[a]){
if (i != p) ans += dp(i, a);
}
if (p!=-1) realans += 1 + (ans+1)%2;
if (p != -1) vals[a] = 1 + (ans+1)%2;
return 1 + (ans+1)%2;
}
void dp2(ll a, ll p, ll d){
depth[a] = d;
if (p==-1) contrib[a] = 0;
if (p != -1){
contrib[a] = contrib[par[a]] + vals[a];
}
for (auto&i : edges[a]){
if (i != p) dp2(i, a, d+1);
}
}
ll dp3(ll a, ll p){
ll tot = lmao[a];
for (auto&i : vtree[a]){
tot += dp3(i,a);
}
if (depth[a]==0) return 0;
lmfao += (tot/2)*2 * -(depth[a]*3 - contrib[a]*2);
return (tot)%2;
}
int anc[200005][30];
int ancc(ll a, ll x){
if (par[a] == -1) return a;
if (x==0) return par[a];
if (anc[a][x] != -1) return anc[a][x];
else{
return anc[a][x] = ancc(ancc(a,x-1),x-1);
}
}
ll lca(ll a, ll b){
if (depth[a] < depth[b]) swap(a,b);
FORNEG(i,29,-1) if (depth[ancc(a, i)] >= depth[b]) a = ancc(a,i);
if (a==b) return a;
FORNEG(i,29,-1) if (ancc(a,i) != ancc(b,i)) a = ancc(a,i), b = ancc(b,i);
return par[a];
}
int main(){
FOR(i,0,200005) FOR(j,0,30) anc[i][j] = -1;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ll n,q;
cin >> n >> q;
FOR(i,0,n-1){
ll a,b;
cin >> a >> b;
edges[a].push_back(b);
edges[b].push_back(a);
}
ll root = -1;
ll cnt = 0;
FOR(j,1,n+1){
if (edges[j].size() != 1){
root = j;
}else{
cnt++;
}
}
dp(root, -1);
dp2(root, -1, 0);
ll ans = 0;
FOR(i,1,n+1) ans += vals[i];
FOR(i,0,q){
ll d; cin >> d;
ll cur = n+1;
ll curans = 0;
set<ll> used;
ll newleaves=0;
lmao.clear();
vector<vector<ll>> stuff;
lmfao = 0;
FOR(k,0,d){
ll temp; cin >> temp;
used.insert(temp);
curans++;
edges[temp].push_back(cur++);
if (edges[temp].size() == 2)continue;
newleaves++;
stuff.push_back({tin[temp], temp});
curans += depth[temp]*3 - contrib[temp]*2;
lmao[temp] += 1;
}
sort(stuff.begin(), stuff.end());
set<vector<ll>> stuff2;
FOR(i,1,stuff.size()){
ll lcatemp = lca(stuff[i][1], stuff[i-1][1]);
stuff2.insert({tin[lcatemp], lcatemp});
}
if (stuff.size()){
ll lcatemp = lca(stuff[0][1], stuff[stuff.size()-1][1]);
stuff2.insert({tin[lcatemp], lcatemp});
for (auto&i : stuff)stuff2.insert(i);
ll virtualroot = (*(stuff2.begin()))[1];
vector<ll> stack;
for (auto&k : stuff2){
while (stack.size() && lca(stack[stack.size()-1], k[1]) != stack[stack.size()-1]) stack.pop_back();
if (stack.size()) vtree[stack[stack.size()-1]].push_back(k[1]);
stack.push_back(k[1]);
}
dp3(virtualroot, -1);
}
if ((cnt+newleaves)%2==1) cout << -1 << "\n";
else{
cout << ans+curans+lmfao << "\n";
}
for (auto&i : stuff2) vtree[i[1]].clear();
for (auto&i : used){
while (edges[i].size() && edges[i][edges[i].size()-1]>n){
edges[i].pop_back();
}
}
}
}
Compilation message
cleaning.cpp: In function 'int main()':
cleaning.cpp:4:33: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define FOR(i,x,y) for(ll i=x; i<y; i++)
......
149 | FOR(i,1,stuff.size()){
| ~~~~~~~~~~~~~~~~
cleaning.cpp:149:9: note: in expansion of macro 'FOR'
149 | FOR(i,1,stuff.size()){
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
41052 KB |
Output is correct |
2 |
Correct |
77 ms |
43780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
47888 KB |
Output is correct |
2 |
Correct |
47 ms |
47888 KB |
Output is correct |
3 |
Correct |
20 ms |
46032 KB |
Output is correct |
4 |
Correct |
87 ms |
54368 KB |
Output is correct |
5 |
Correct |
86 ms |
55240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
49160 KB |
Output is correct |
2 |
Correct |
51 ms |
48640 KB |
Output is correct |
3 |
Correct |
54 ms |
50660 KB |
Output is correct |
4 |
Correct |
167 ms |
69884 KB |
Output is correct |
5 |
Correct |
32 ms |
49236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
43344 KB |
Output is correct |
2 |
Correct |
34 ms |
42840 KB |
Output is correct |
3 |
Correct |
12 ms |
42088 KB |
Output is correct |
4 |
Correct |
11 ms |
42332 KB |
Output is correct |
5 |
Correct |
13 ms |
42584 KB |
Output is correct |
6 |
Correct |
68 ms |
43944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
45372 KB |
Output is correct |
2 |
Correct |
109 ms |
45908 KB |
Output is correct |
3 |
Correct |
93 ms |
44116 KB |
Output is correct |
4 |
Correct |
105 ms |
45904 KB |
Output is correct |
5 |
Correct |
107 ms |
45904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
89 ms |
48464 KB |
Output is correct |
2 |
Correct |
70 ms |
49476 KB |
Output is correct |
3 |
Correct |
101 ms |
50256 KB |
Output is correct |
4 |
Correct |
105 ms |
50252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
41052 KB |
Output is correct |
2 |
Correct |
77 ms |
43780 KB |
Output is correct |
3 |
Correct |
49 ms |
47888 KB |
Output is correct |
4 |
Correct |
47 ms |
47888 KB |
Output is correct |
5 |
Correct |
20 ms |
46032 KB |
Output is correct |
6 |
Correct |
87 ms |
54368 KB |
Output is correct |
7 |
Correct |
86 ms |
55240 KB |
Output is correct |
8 |
Correct |
48 ms |
49160 KB |
Output is correct |
9 |
Correct |
51 ms |
48640 KB |
Output is correct |
10 |
Correct |
54 ms |
50660 KB |
Output is correct |
11 |
Correct |
167 ms |
69884 KB |
Output is correct |
12 |
Correct |
32 ms |
49236 KB |
Output is correct |
13 |
Correct |
80 ms |
43344 KB |
Output is correct |
14 |
Correct |
34 ms |
42840 KB |
Output is correct |
15 |
Correct |
12 ms |
42088 KB |
Output is correct |
16 |
Correct |
11 ms |
42332 KB |
Output is correct |
17 |
Correct |
13 ms |
42584 KB |
Output is correct |
18 |
Correct |
68 ms |
43944 KB |
Output is correct |
19 |
Correct |
43 ms |
45372 KB |
Output is correct |
20 |
Correct |
109 ms |
45908 KB |
Output is correct |
21 |
Correct |
93 ms |
44116 KB |
Output is correct |
22 |
Correct |
105 ms |
45904 KB |
Output is correct |
23 |
Correct |
107 ms |
45904 KB |
Output is correct |
24 |
Correct |
89 ms |
48464 KB |
Output is correct |
25 |
Correct |
70 ms |
49476 KB |
Output is correct |
26 |
Correct |
101 ms |
50256 KB |
Output is correct |
27 |
Correct |
105 ms |
50252 KB |
Output is correct |
28 |
Correct |
103 ms |
45908 KB |
Output is correct |
29 |
Correct |
129 ms |
49492 KB |
Output is correct |
30 |
Correct |
84 ms |
55052 KB |
Output is correct |
31 |
Correct |
157 ms |
69368 KB |
Output is correct |
32 |
Correct |
105 ms |
45900 KB |
Output is correct |
33 |
Correct |
155 ms |
49888 KB |
Output is correct |
34 |
Correct |
174 ms |
50100 KB |
Output is correct |
35 |
Correct |
176 ms |
51028 KB |
Output is correct |