51nodP1378夹克老爷的愤怒
将每一个点按照深度排序,每次取出深度最大的点,向上跳k个后再扩展标记.
#include<bits/stdc++.h>
#define il inline
#define rg register
#define gi read<int>
using namespace std;
const int O = 1e5 + 10;
struct Edge { int to, nt; } e[O << 1];
template<class TT>
il TT read() {
TT o = 0,fl = 1; char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') fl = -1, ch = getchar();
while (isdigit(ch)) o = o * 10 + ch - '0', ch = getchar();
return fl * o;
}
bool vis[O];
int n, k, cnt, mx, ans, head[O], f[O];
vector<int>G[O];
il void add(int u, int v) {
e[++cnt] = (Edge){v, head[u]};
head[u] = cnt;
}
il void dfs_dep(int u, int fa, int dep) {
f[u] = fa;
++dep;
G[dep].push_back(u);
if (mx < dep) mx = dep;
for (int i = head[u]; ~i; i = e[i].nt) {
int v = e[i].to;
if (v == fa) continue;
dfs_dep(v, u, dep);
}
}
il void dfs_mark(int u, int fa, int K) {
vis[u] = 1;
if (K++ == k) return;
for (int i = head[u]; ~i; i = e[i].nt)
if (e[i].to ^ fa) dfs_mark(e[i].to, u, K);
}
int main() {
memset(head, -1, sizeof head);
n = gi(), k = gi();
for (int i = 1; i < n; ++i) {
int u = gi(), v = gi();
add(u, v); add(v, u);
}
G[0].push_back(0);
dfs_dep(0, -1, 0);
for(int i = mx; i; --i) {
for (unsigned int j = 0; j < G[i].size(); ++j) {
int v = G[i][j];
if (vis[v]) continue;
for (int i = 1; i <= k; ++i)
if (f[v] != -1) v = f[v];
else break;
dfs_mark(v, -1, 0);
ans++;
}
}
printf("%d\n", ans);
return 0;
}