Codeforces 828D High Load【贪心+求树的最长链】

224 篇文章 2 订阅
186 篇文章 0 订阅

D. High Load
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Arkady needs your help again! This time he decided to build his own high-speed Internet exchange point. It should consist of n nodes connected with minimum possible number of wires into one network (a wire directly connects two nodes). Exactly k of the nodes should be exit-nodes, that means that each of them should be connected to exactly one other node of the network, while all other nodes should be connected to at least two nodes in order to increase the system stability.

Arkady wants to make the system as fast as possible, so he wants to minimize the maximum distance between two exit-nodes. The distance between two nodes is the number of wires a package needs to go through between those two nodes.

Help Arkady to find such a way to build the network that the distance between the two most distant exit-nodes is as small as possible.

Input

The first line contains two integers n and k (3 ≤ n ≤ 2·105, 2 ≤ k ≤ n - 1) — the total number of nodes and the number of exit-nodes.

Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.

Output

In the first line print the minimum possible distance between the two most distant exit-nodes. In each of the next n - 1 lines print two integers: the ids of the nodes connected by a wire. The description of each wire should be printed exactly once. You can print wires and wires' ends in arbitrary order. The nodes should be numbered from 1 to n. Exit-nodes can have any ids.

If there are multiple answers, print any of them.

Examples
Input
3 2
Output
2
1 2
2 3
Input
5 3
Output
3
1 2
2 3
3 4
3 5
Note

In the first example the only network is shown on the left picture.

In the second example one of optimal networks is shown on the right picture.

Exit-nodes are highlighted.

题目大意:


让你构造出来一个具有N个点的一棵树,使得其中度为1的点数为k个。

使得这些度为1的点两两之间的距离最大值最小。

问这个最小值和树的形状,多解输出任一即可.


思路:

贪心思维,假设现在k==3.n=4.显然这样排列答案为2是最优:



我们不断的向上添加点。

n=5时:


n=6时:


我们依次类推,不断在度为1的点之后添加点,显然能够达到最优。


那么我们暴力建边即可,建好图之后,我们以任意节点作为根跑一下Dfs,找到距离他最远的那个点,然后再以那个点作为根,跑一下Dfs,找距离他最远的点的长度,即为树的最长链长度,也就是答案。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
struct node
{
    int x,y;
}ans[250000];
int root;
int maxn;
vector<int>mp[250000];
void Dfs(int u,int from,int depth)
{
    if(depth>maxn)
    {
        maxn=depth;
        root=u;
    }
    for(int i=0;i<mp[u].size();i++)
    {
        int v=mp[u][i];
        if(v==from)continue;
        Dfs(v,u,depth+1);
    }
}
int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        int cnt=0;
        queue<int >s;
        for(int i=1;i<=n;i++)mp[i].clear();
        for(int i=2;i<=k+1;i++)
        {
            mp[1].push_back(i);
            mp[i].push_back(1);
            ans[cnt].x=1;
            ans[cnt++].y=i;
            s.push(i);
        }
        for(int i=k+2;i<=n;i++)
        {
            int u=s.front();
            s.pop();
            ans[cnt].x=i;
            ans[cnt++].y=u;
            mp[i].push_back(u);
            mp[u].push_back(i);
            s.push(i);
        }
        maxn=0;
        root=1;
        Dfs(root,-1,0);
        maxn=0;
        Dfs(root,-1,0);
        printf("%d\n",maxn);
        for(int i=0;i<n-1;i++)
        {
            printf("%d %d\n",ans[i].x,ans[i].y);
        }
    }
}















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值