CodeForces 639 B.Bear and Forgotten Tree 3(构造)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值