Description
Solution
不断从深度最大的节点的级父节点进行扩展
Attention
注意 扩展 时使用 , 因为 保存上一个经过的节点较麻烦
代码中有 函数, 仅供参考, 由于回环往复地入队, 不可避免,
Code
#include<bits/stdc++.h>
#define reg register
int read(){
int s = 0, flag = 1;
char c;
while((c=getchar()) && !isdigit(c))
if(c == '-'){ c = getchar(), flag = -1; break ; }
while(isdigit(c)) s = s*10 + c-'0', c = getchar();
return s * flag;
}
const int maxn = 100005;
int N;
int K;
int num0;
int Ans;
int head[maxn];
int Fa[maxn];
int f[maxn], Used[maxn];
std::queue <int> Q;
struct Edge{ int nxt, to; } edge[maxn << 1];
void Add(int from, int to){ edge[++ num0] = (Edge){ head[from], to }; head[from] = num0; }
void BFS(int k){
while(!Q.empty()) Q.pop();
Q.push(k); f[k] = K, Used[k] = 1;
while(!Q.empty()){
int ft = Q.front(); Q.pop();
for(reg int i = head[ft]; i; i = edge[i].nxt){
int to = edge[i].to;
if(Used[to]) continue ;
Used[to] = 1;
if(f[ft]-1) f[to] = f[ft] - 1, Q.push(to);
}
}
}
struct Node{
int dep, id;
} T[maxn];
void DFS(int k, int fa){
T[k].id = k;
Fa[k] = fa;
T[k].dep = T[fa].dep + 1;
for(reg int i = head[k]; i; i = edge[i].nxt){
int to = edge[i].to;
if(to == fa) continue ;
DFS(to, k);
}
}
void DFS_2(int k, int left, int last){
Used[k] = 1;
if(!left) return ;
for(reg int i = head[k]; i; i = edge[i].nxt){
int to = edge[i].to;
if(to == last) continue ;
DFS_2(to, left-1, k);
}
}
bool cmp(Node a, Node b){ return a.dep < b.dep; }
int main(){
N = read(), K = read();
int ____ = read();
if(!K){ printf("%d
", N); return 0; }
for(reg int i = 1; i < N; i ++){
int u = read(), v = read();
Add(u, v), Add(v, u);
}
DFS(1, 0);
std::sort(T+1, T+N+1, cmp);
for(reg int i = N; i >= 1; i --){
int t = T[i].id;
if(!Used[t]){
for(reg int j = 1; j <= K; j ++)
if(Fa[t]) t = Fa[t];
else break ;
DFS_2(t, K, 0), Ans ++;
}
}
printf("%d
", Ans);
return 0;
}