题目链接:

http://codeforces.com/contest/658/problem/C

题意:

给定结点数,树的直径(两点的最长距离),树的高度(1号结点距离其他结点的最长距离),写出树边的端点,一种情况即可。如不存在,输出-1。规定根为1号结点。

分析:

首先可以明确h<(d+1)/2的时候不能构成树。

当 h!=d 时,先将h−1个点和1连成串,然后剩下的点都和1直接相连。

当 h==d 时,还是先将h−1个点和1连成串,剩下的点不能连在这个h个点构成的串的两头,在串中间随便找一点,然后剩下的点都和这个点连在一起就好了。

但是!前提是有这个随便的一点,也就是说h等于1的时候是没有中间节点的,除非只有两个结点,否则无法构成树。

h不等于1的情况就很简单了,直接在2上不停的加点就好了。。。

代码:

#include <cstdio>
int main (void)
{
int n, d, h;
scanf("%d%d%d", &n, &d, &h);
if(h < (d + 1) / 2 ) return printf("-1\n"), 0;
if(d == 1 && n > 2) return printf("-1\n"), 0;
for(int i = 1 ; i <= h; i++)
printf("%d %d\n", i , i + 1);
if (d != h){
printf("1 %d\n", h + 2);
for(int i = h + 2; i <= d; i++ )
printf("%d %d\n", i , i + 1);
for(int i = d + 2; i <= n ; i++)
printf("1 %d\n", i);
}else{
for(int i = h+ 2; i <= n; i++)
printf("2 %d\n", i);
} return 0;
}

Codeforces 658C Bear and Forgotten Tree 3【构造】的更多相关文章

  1. CodeForces 658C Bear and Forgotten Tree 3 (构造)

    题意:构造出一个 n 个结点,直径为 m,高度为 h 的树. 析:先构造高度,然后再构造直径,都全了,多余的边放到叶子上,注意直径为1的情况. 代码如下: #pragma comment(linker ...

  2. Codeforces 639B——Bear and Forgotten Tree 3——————【构造、树】

    Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  3. VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3 构造

    C. Bear and Forgotten Tree 3 题目连接: http://www.codeforces.com/contest/658/problem/C Description A tre ...

  4. [Codeforces 639B] Bear and Forgotten Tree 3

    [题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...

  5. codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)

    题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...

  6. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E. Bear and Forgotten Tree 2 bfs set 反图的生成树

    E. Bear and Forgotten Tree 2 题目连接: http://www.codeforces.com/contest/653/problem/E Description A tre ...

  7. VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3

    C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表

    E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...

  9. Code Forces Bear and Forgotten Tree 3 639B

    B. Bear and Forgotten Tree 3 time limit per test2 seconds memory limit per test256 megabytes inputst ...

随机推荐

  1. 工作中Docker使用命令笔记

    docker安装与启动 安装docker [root@localhost /]# yum -y install docker-io 更改配置文件 [root@localhost /]# vi /etc ...

  2. Mac OSX简单使用中会用到的

    选择操作系统(例如选择BootCamp分区的Windows):开机按住Option键直到磁盘图标出现后选择. 忘记本地账号密码:按着Command+R开机选择Recovered启动打开终端输入re ...

  3. iview modal 弹框 模板

    iview modal 弹框 模板 <!-- * @description 上传图片 * @fileName camera.vue * @author 彭成刚 * @date // :: * @ ...

  4. app自动化配置信息

    caps={    "platformName":"Android",#平台名称    "platformVersion":"6. ...

  5. HTML页面中解决内容元素随窗口变化布局变乱问题

    1.给body加上一个min-width最小宽度,以px为单位,这样当页面变小时,当达到你所设置的最小宽度,body的宽度不再改变,超出的部分会用横向滚动条显示,其内所有元素的布局也不会受影响. 2. ...

  6. luogu P1618 三连击(升级版)

    题目描述 将1,2,…,9共9个数分成三组,分别组成三个三位数,且使这三个三位数的比例是A:B:C,试求出所有满足条件的三个三位数,若无解,输出“No!!!”. //感谢黄小U饮品完善题意 输入输出格 ...

  7. POJ 1383 Labyrinth (树的直径求两点间最大距离)

    Description The northern part of the Pyramid contains a very large and complicated labyrinth. The la ...

  8. luogu1502 窗口的星星

    扫描线应该打懒标记的-- #include <algorithm> #include <iostream> #include <cstdio> using name ...

  9. js总结(一):javascript的类型:基本类型、对象和数组

    javascript 类型分为2种,一个是原始值,另一个是复杂值(对象). 一.原始值 5个原始值是:数字,字符,布尔,null,undefined. 9个原生的对象构造函数:Number Strin ...

  10. 使用PL/SQL将sql脚本数据导入数据库

    一. PL/SQL登录到数据库,使用tools工具进行导入.使用plsql登录到需要导入数据的数据库.点击工具栏上[tools]--[Import tables] 二.commit;