Description
构造一棵点数为n,直径为d,深度为h的树
Input
三个整数n,d,h(2<=n<=1e6,1<=h<=d<=n-1)
Output
如果存在合法方案则输出该树,否则输出-1
Sample Input
5 3 2
Sample Output
1 2
1 3
3 4
3 5
Solution
d > 2h或d < h都无解,d > h的时候以1为根构造一条长度为h一条长度为d-h的链,剩余的点全部连向1,d=h的时候,如果d=1且n > 2则无解,否则连一条长度为d的单链,然后把所有其他点连上该链一个非端点的点上即可
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
int n,d,h;
int main()
{
while(~scanf("%d%d%d",&n,&d,&h))
{
if(d>2*h||d<h)printf("-1\n");
else
{
if(d==h)
{
if(d==1&&n>2)printf("-1\n");
else
{
for(int i=1;i<=d;i++)printf("%d %d\n",i,i+1);
for(int i=d+2;i<=n;i++)printf("2 %d\n",i);
}
}
else
{
for(int i=1;i<=h;i++)printf("%d %d\n",i,i+1);
printf("1 %d\n",h+2);
for(int i=2;i+h<=d;i++)printf("%d %d\n",h+i,h+i+1);
for(int i=d+2;i<=n;i++)printf("1 %d\n",i);
}
}
}
return 0;
}