Codeforces D - High Load

时间:2023-03-09 16:18:30
Codeforces D - High Load

D - High Load

因为要出口节点距离最小,所以除了根节点(根节点连接k个儿子)其他节点的儿子只能有一个,其他情况下的距离都比这个长,因为如果不是这样,那么根节点连接的子树数量就小与k,那么每个子树的长度就更长了(显然,画图可证)。

n-1个节点平均分配到k条子树上,如果画图的话,是这样的

Codeforces D - High Load

只要讨论一下最后一层的的个数((n-1)%k)情况即可,连接方式也如上图。

代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie();
int n,k;
while(cin>>n>>k)
{
int m=(n-)/k;
if((n-)%k==)
{
cout<<m*<<endl;
}
else if((n-)%k==)
{
cout<<m*+<<endl;
}
else cout<<m*+<<endl;
int i=;
for(i=;i<=k+&&i<=n;i++)cout<<i<<' '<<<<endl;
while(i<=n)
{
cout<<i<<' '<<i-k<<endl;
i++;
}
}
return ;
}