The internet cafes of penguin countries are connected by network cables to form a tree structure. Now that the winter is up, the heating sector lacks fuel, so they decide to dismantle some of the cables to make fuel. But now there are K penguins to surf the internet and others online games, so they need to put this K-Penguin into different room (two penguins in the same room will quarrel), and then remove some network cable, but need to ensure that each penguin at least through the left of the network cable and at least another penguin online game.
So they want to know how many network cables need to be retained at least.
Today the examination room again ZZ, only cut the second question Qwq
The first question hit a very obviously wrong greedy, 35pts QwQ
Later an exchange found: The tree on the two-figure matching AH.
Why do you want to choose as many as possible without the public end of the edge but did not think of a match ah (I will probably retire this year)
Okay, here we go.
It's already been said, but obviously we can't actually go to Hungary.
Set F[i][0] Indicates how many pairs of points are 22 paired in the X's subtree, where x is not selected
F[I][1] = x selected case, obviously f[i][0]=σf[v][1], f[i][1]=max{f[i][0]-f[v][1]+f[v][0]+2} {V∈son[i]}
Let us make Ans=max (f[1][0],f[1][1])
If 2ans>=k then the answer is (k+1)/2
Otherwise it is ans+ (K-2ans) (Obviously there is no 22 pairing point, you can add a point by adding one edge (one change))
#include <vector>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int n,k,f[100010][2],ans=0;
Vector<int> g[100010];
void dp (int x,int p) {
f[x][0]=f[x][1]=0;
for (int v,i=0,z=g[x].size (); i<z;++i)
if ((V=g[x][i])!=p) {
dp (V,X);
F[X][0]+=F[V][1];
}
for (int v,i=0,z=g[x].size (); i<z;++i)
if ((V=g[x][i])!=p) F[x][1]=max (f[x][1],f[x][0]-f[v][1]+f[v][0]+1);
}
int vvv () {
g[1].clear ();
scanf ("%d%d", &n,&k);
for (int x,i=2;i<=n;++i) {
g[i].clear ();
scanf ("%d", &x);
G[x].push_back (i);
}
DP (1,0); Ans=max (f[1][0],f[1][1]);
if (ans*2>=k) printf ("%d\n", k+1>>1);
else printf ("%d\n", ans+ (k-ans*2));
}
int main () {
freopen ("tree.in", "R", stdin);
Freopen ("Tree.out", "w", stdout);
int T; For (scanf ("%d", &t); T--;VVV ());
}