Codeforces Round 936 (Div. 2) C. Tree Cutting

Tree Cutting

题目描述

You are given a tree with n n n vertices.

Your task is to find the maximum number x x x such that it is possible to remove exactly k k k edges from this tree in such a way that the size of each remaining connected component † ^{\dagger} is at least x x x.

† ^{\dagger} Two vertices v v v and u u u are in the same connected component if there exists a sequence of numbers t 1 , t 2 , … , t k t_1, t_2, \ldots, t_k t1,t2,,tk of arbitrary length k k k, such that t 1 = v t_1 = v t1=v, t k = u t_k = u tk=u, and for each i i i from 1 1 1 to k − 1 k - 1 k1, vertices t i t_i ti and t i + 1 t_{i+1} ti+1 are connected by an edge.

输入描述

Each test consists of several sets of input data. The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104) — the number of sets of input data. This is followed by a description of the sets of input data.

The first line of each set of input data contains two integers n n n and k k k ( 1 ≤ k < n ≤ 1 0 5 1 \le k < n \le 10^5 1k<n105) — the number of vertices in the tree and the number of edges to be removed.

Each of the next n − 1 n - 1 n1 lines of each set of input data contains two integers v v v and u u u ( 1 ≤ v , u ≤ n 1 \le v, u \le n 1v,un) — the next edge of the tree.

It is guaranteed that the sum of the values of n n n for all sets of input data does not exceed 1 0 5 10^5 105.

输出描述

For each set of input data, output a single line containing the maximum number x x x such that it is possible to remove exactly k k k edges from the tree in such a way that the size of each remaining connected component is at least x x x.

样例输入

6
5 1
1 2
1 3
3 4
3 5
2 1
1 2
6 1
1 2
2 3
3 4
4 5
5 6
3 1
1 2
1 3
8 2
1 2
1 3
2 4
2 5
3 6
3 7
3 8
6 2
1 2
2 3
1 4
4 5
5 6

样例输出

2
1
3
1
1
2

原题

CF——传送门

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxn = 1e5 + 6;
vector<int> e[maxn]; // 存边

int n, k;

void add(int a, int b)
{
    e[a].push_back(b);
}

int cnt; // 剪边后每个剩余的连接部分的大小为至少为x时的连通块的最大数量
int dfs(int u, int fath, int target)
{
    int cur_num = 1; // 当前子树大小
    for (int i = 0; i < e[u].size(); i++)
    {
        int v = e[u][i];
        if (v != fath)
        {
            cur_num += dfs(v, u, target); // 递归,并记录当前子树剩余大小(部分块会在递归后被剪掉)
        }
    }
    if (cur_num >= target) // 贪心,一旦大小大于等于x即目标大小,则将其剪掉
    {
        cnt++;
        return 0; // 已经剪掉该子树,返回大小0
    }
    return cur_num;
}

bool check(int x)
{
    cnt = 0;
    dfs(1, 0, x);
    if (cnt >= k + 1) // 连通块的最大数量大于等于k+1时,才能满足剪k条边时每个剩余的连接部分的大小为至少为x
        return true;
    else
        return false;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    while (t--)
    {
        cin >> n >> k;
        for (int i = 1; i <= n; i++) // 清空上一次测试中的边
        {
            e[i].clear();
        }
        int v, u;
        for (int i = 1; i <= n - 1; i++) // 邻接表加边
        {
            cin >> v >> u;
            add(v, u);
            add(u, v);
        }
        // 二分查找满足条件的最大x
        int l = 0, r = n + 6;
        while (l < r)
        {
            int mid = (l + r + 1) / 2; // 这里要 l + r +1 要不然会死循环
            if (check(mid))            // check函数自己写
            {
                l = mid;
            }
            else
            {
                r = mid - 1; // [mid,r] 不满足条件, 所以要移到满足条件的一方, r = mid - 1
            }
        }
        // 最后的l,r是答案 因为 l == r
        cout << l << '\n';
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值