题目地址http://codeforces.com/problemset/problem/828/D
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.
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.
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.
3 2
2 1 2 2 3
5 3
3 1 2 2 3 3 4 3 5
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个点,其中k个点只能有一条边,其他点的边不定,要使距离最远的两个点的距离尽量靠近,最大值最小化,那k个只有一条边的就是边缘,将构成的图看做一棵向四周发散的树,有k个枝条,这样也就得到了最短距离。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int n,k;
scanf("%d%d",&n,&k);
int a;
if((n-1)%k==0)//这时每个枝条都是一样长的,因此距离就是层数*2;
a=(n-1)/k*2;
else if((n-1)%k==1)//余数为1,说明有一个枝条会比其他的长1,因此最大距离加一;
a=(n-1)/k*2+1;
else//余数大于1,既至少有两个枝条的长度比原来的长,因此距离加2;
a=(n-1)/k*2+2;
printf("%d\n",a);
for(int i=2;i<=k;i++)
printf("%d %d\n",1,i);//1为根,最中间的点;
for(int i=k+1;i<=n;i++)
printf("%d %d\n",i,i-k);//一圈一圈的往外扩散;
return 0;
}