http://codeforces.com/contest/828/problem/Dnode
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.c++
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.ide
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.ui
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.spa
Note that it is always possible to build at least one network with n nodes and k exit-nodes within the given constraints.code
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.ip
If there are multiple answers, print any of them.ci
3 2get
2
1 2
2 3input
你須要構造n個點一棵樹,知足這個樹裏面只有k個點的度數爲1,且樹的直徑最小。
紙上隨便畫一畫就能夠知道,咱們確定是按照那k個平均去畫就行了,也就是首先讓一個節點當中央節點,而後剩下的每個點都分(n-1)/k節點,若是n-k%k!=0的話,咱們讓一個節點後面增長一個就行了。 >1同理
#include<bits/stdc++.h> using namespace std; int n,k; vector<pair<int,int> >ans; int main(){ scanf("%d%d",&n,&k); n=n-1; int Ans=n/k*2+(n%k>0)+(n%k>1); int p=0; for(int i=0;i<n-n%k;i++){ if(i%(n/k)==0) ans.push_back(make_pair(i+1,n+1)); else ans.push_back(make_pair(i,i+1)); if(i%(n/k)==0)p=p+1; } int step =n/k-1; for(int i=n-n%k;i<n;i++){ // cout<<p<<endl; if(p<k){ p=p+1; ans.push_back(make_pair(step,i+1)); }else{ ans.push_back(make_pair(step+1,i+1)); } step+=n/k; } cout<<Ans<<endl; sort(ans.begin(),ans.end()); for(int i=0;i<ans.size();i++){ cout<<ans[i].first<<" "<<ans[i].second<<endl; } }