题目链接:

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. 洛谷 P2788 数学1(math1)- 加减算式

    题目背景 蒟蒻HansBug在数学考场上,挠了无数次的头,可脑子里还是一片空白. 题目描述 好不容易啊,HansBug终于熬到了做到数学最后一题的时刻了,眼前是一堆杂乱的加减算式.显然成功就在眼前了. ...

  2. dnskeygen - 针对DNS安全性所生成的公共,私有和共享的密钥

    SYNOPSIS(总览) dnskeygen [- [DHR ] size ] [-F ] -[zhu ] [-a ] [-c ] [-p num ] [-s num ] -n name DESCRI ...

  3. softmax_regression完成mnist手写体数据集的识别

    ---恢复内容开始--- import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data mnis ...

  4. android 近百个源码项目【转】

    http://www.cnblogs.com/helloandroid/articles/2385358.html Android开发又将带来新一轮热潮,很多开发者都投入到这个浪潮中去了,创造了许许多 ...

  5. mysql的sql语句练习的2个网址

    sql语句练习: https://blog.csdn.net/mrbcy/article/details/68965271 完成. https://blog.csdn.net/flycat296/ar ...

  6. source 1.7 中不支持 lambda 表达式(请使用 -source 8 或更高版本以启用 lambda 表达式)

    from:http://blog.csdn.net/qwdafedv/article/details/54691740 https://www.youtube.com/watch?v=oXxijdf8 ...

  7. 怎么给xshell服务配置密钥远程登陆

    xshell密钥登陆 1.找一台测试服务器xiuxiu-test生成密钥对2. cd ./.ssh/ && 把公钥放在~/.ssh/authorized_keys文件中  cat id ...

  8. 环境变量HISTCONTROL命令及对快捷键Ctrl+o命令的影响

    在linux中环境变量HISTCONTROL可以控制历史的记录方式. HISTCONTROL有以下的选项: ignoredups          默认,忽略重复命令 ignorespace      ...

  9. bash实现自动补全

    yum install -y bash-completion source /usr/share/bash-completion/bash_completion 执行后yum拥有选项自动补全功能 对于 ...

  10. css 实践记录

    子绝父相 https://developer.mozilla.org/zh-CN/docs/Web/CSS/position 利用子绝父相来实现一种比较老的居中方式:1.明确宽度:2.定位左边到容器的 ...