Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) D. High Load
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
#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; }