Codeforces E. High Load(构造)

题目描述:

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

Copy

3 2

Output

Copy

21 22 3

Input

Copy

5 3

Output

Copy

31 22 33 43 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.

img

思路:

题目是说给一个总的顶点数和特殊的顶点数,其中特殊的顶点只能连接另一个顶点,而其他顶点必须连至少两个顶点,现在要构造一种连接方式,使得特殊顶点距离的最大值最小。

刚开始看漏要求,以为可以成环,WA了后看清题目要是棵树,瞬间难度不在一个档次(虽然也不是太难),但我胡乱想也不知道要怎么构造。原来题目的思路是要把这棵树的没一个分支高度尽可能相等,也就是节点要尽可能平均,这样可以使距离最大值最小,如下:

然后就可以做了。先算出分摊下来每个分枝上至少有多少个节点,在算出余数就是要往分枝末端加上的节点。注意是当有一个节点多出来以后,两个特殊节点的最长距离加一;多两个节点后,由于这两个节点会加到不同分枝的末端,所以距离加二;而当多出节点数大于2,最长距离也是加二,因为多出的节点只加了不到一层节点,深度只增加一。

代码:

#include <iostream>
using namespace std;
int n,k;
int length;
int stem;
int main()
{
    cin >> n >> k;
    length = (n-1)/k*2;
    stem = (n-1)/k;
    int re = (n-1)%k;
    if(re==0)
    {
        cout << length << endl;
        //cout << "k " << k << endl;
        for(int j = 2; j<n+1; j+=stem)
        {
            cout << 1 << " " << j << endl;
            for(int z = j+1; z<j+stem; z++)
            {
                cout << z-1 << " " << z << endl;
            }
        }
    }
    if(re==1)
    {
        cout << length+1 << endl;
        for(int j = 2; j<n; j+=stem)
        {
            cout << 1 << " " << j << endl;
            for(int z = j+1; z<j+stem; z++)
            {
                cout << z-1 << " " << z << endl;
            }
        }
        cout << n-1 << " " << n;
    }
    if(re==2)
    {
        //cout << "re " << re << " k " << k << endl;
        cout << length+2 << endl;
        for(int j = 2; j<n-1; j+=stem)
        {
            cout << 1 << " " << j << endl;
            for(int z = j+1; z<j+stem; z++)
            {
                cout << z-1 << " " << z << endl;
            }
        }
        cout << n-2 << " " << n-1 << endl;
        cout << n-2-stem << " " << n << endl;
    }
    if(re>2)
    {
        //cout << "length " << length << endl;
        cout << length+2 << endl;
        for(int j = 2; j<n-re+1; j+=stem)
        {
            cout << 1 << " " << j << endl;
            for(int z = j+1; z<j+stem; z++)
            {
                cout << z-1 << " " << z << endl;
            }
        }
        for(int i = 0;i<re;i++)
        {
            cout << n-re-i*stem << " " << n-re+1+i << endl;
        }
    }
    return 0;
}
posted @ 2019-08-15 10:55  小张人  阅读(...)  评论(...编辑  收藏
分享到: