不用建树
#include <iostream>
#include <algorithm>
using namespace std;
int tree[100002] = {0};
typedef struct Pair
{
int u,v;
}Pair;
bool cmp(Pair p1,Pair p2)
{
return p1.u < p2.u;
}
int main(void)
{
int t,u,v,n,m,c = 0;
cin >> n >> m;
Pair* p = new Pair[n];
for(int i = 1;i < n;++i)
{
cin >> u >> v;
if(u > v)
{
p[i].u = v;
p[i].v = u;
}
else
{
p[i].u = u;
p[i].v = v;
}
}
sort(p+1,p+n,cmp);
for(int i = 1;i < n;++i)
tree[p[i].v] = tree[p[i].u]+1;
for(int i = 2;i <= n;++i)
{
if(tree[i] <= m)
++c;
}
cout << c << endl;
delete []p;
return 0;
}