Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load

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
题解中说到了一种解决方法,星型树。
我大致猜测了一下什么情况,以某个点为中心,某个叶子节点要访问其他某个叶子节点必须经过这个中心。
那么也就是说其他点成一条链状连接着这个中心,每条链的末端便是叶子节点。那么就很好理解了,k条链连接着中心,n-1个点。
叶子节点到中心平均大约(n-1)/k的距离,这样可以保证最远点对的距离最小。
 
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<cmath>
#include<set>
#include<stack>
#define ll long long
#define pb push_back
#define max(x,y) ((x)>(y)?(x):(y))
#define min(x,y) ((x)>(y)?(y):(x))
#define cls(name,x) memset(name,x,sizeof(name))
#define pos first
#define index second
#define mp make_pair
using namespace std;
const int inf=1e9+10;
const ll llinf=1e16+10;
const int maxn=2e5+10;
const int maxm=1e2+10;
const int mod=1e9+7;
int n,k;
int pre[maxn];
int main()
{
    //freopen("in.txt","r",stdin);
    while(~scanf("%d %d",&n,&k))
    {
        int res=n-1,p=k;
        int key=2;
        for(int i=1;i<=k;i++)
        {
            pre[key]=1;
            for(int i=1;i<=res/p;i++)
                pre[key+1]=key++;
            res-=res/p;
            p--;
        }
        if((n-1)%k==0)
            printf("%d\n",(n-1)/k*2);
        else if((n-1)%k==1)
            printf("%d\n",(n-1)/k*2+1);
        else if((n-1)%k>=2)
            printf("%d\n",(n-1)/k*2+2);
        for(int i=2;i<=n;i++)
            printf("%d %d\n",i,pre[i]);
    }
    return 0;
}

 

posted @ 2017-07-13 15:26  爱种树的码农  阅读(118)  评论(0编辑  收藏  举报