题目大意
给出一棵树,现在在一些节点上安插健康监测站,使得每条简单路径上的检测站的数目不超过 k k k个,给定 k k k求出最多能安排多少监测站
解题思路
这题比赛时队友看成在边上建立监测站了,我也没有再次读题,好难受。实际上观察如下的图:
当
k
=
4
k=4
k=4时,我们发现如果安插到
1
,
2
,
5
,
6
,
7
,
8
1,2,5,6,7,8
1,2,5,6,7,8,这样得到的才是最多的,实际上我们每次安插监测站时尽量安排到叶子节点,而不是安排到中间,因为中间的节点是很多交叉节点的
L
C
A
LCA
LCA,那么安排到叶子节点后对其他所有简单路径的影响是最小的
那么具体的思路就是,每次将树上的所有度为 1 1 1的节点设置为监测站,然后从树上删除。这样每次增加的贡献为 2 2 2,我们最多安排的轮次为 ⌊ k 2 ⌋ \lfloor \frac{k}{2} \rfloor ⌊2k⌋,那么就设置一个标记记录每个节点是第几次入队的,如果进行到 ⌊ k 2 ⌋ + 1 \lfloor \frac{k}{2} \rfloor+1 ⌊2k⌋+1次就结束。最后判断一下 k k k为奇数,然后队列中仍存在节点时还能贡献一次
//
// Created by Happig on 2020/9/29
//
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define Vector Point
#define ENDL "\n"
#define lowbit(x) (x&(-x))
#define mkp(x, y) make_pair(x,y)
#define mem(a, x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double dinf = 1e300;
const ll INF = 1e18;
const int Mod = 1e9 + 7;
const int maxn = 1e6 + 10;
int d[maxn];
bitset<maxn> vis;
vector<int> G[maxn];
queue<pii> q;
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int n, k;
cin >> n >> k;
for (int i = 1, u, v; i < n; i++) {
cin >> u >> v;
d[u]++, d[v]++;
G[u].push_back(v), G[v].push_back(u);
}
if (k == 0 || k == 1) {
cout << k << ENDL;
return 0;
}
vis.set();
for (int i = 1; i <= n; i++)
if (d[i] == 1) q.push({i, 1});
int ans = 0, tot = k / 2;
while (!q.empty()) {
pii cur = q.front();
q.pop();
int u = cur.first, num = cur.second;
vis[u] = 0;
if (num == tot + 1) break;
ans++;
for (auto v: G[u]) {
if (vis[v]) {
d[v]--;
if (d[v] == 1) q.push({v, num + 1});
}
}
}
if ((k & 1) && !q.empty() && q.front().second == tot + 1) ans++;
cout << ans << ENDL;
return 0;
}