Codeforces 658C Bear and Forgotten Tree 3【构造】
题目链接:
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【构造】的更多相关文章
- CodeForces 658C Bear and Forgotten Tree 3 (构造)
题意:构造出一个 n 个结点,直径为 m,高度为 h 的树. 析:先构造高度,然后再构造直径,都全了,多余的边放到叶子上,注意直径为1的情况. 代码如下: #pragma comment(linker ...
- 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 ...
- 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 ...
- [Codeforces 639B] Bear and Forgotten Tree 3
[题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...
- 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 ...
- 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 ...
- 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 ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- 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 ...
随机推荐
- (转)新手学习System Verilog & UVM指南
从刚接触System Verilog以及后来的VMM,OVM,UVM已经有很多年了,随着电子工业的逐步发展,国内对验证人才的需求也会急剧增加,这从各大招聘网站贴出的职位上也可以看出来,不少朋友可能想尽 ...
- Redis杂谈
这是2015年初应邀在南华智闻作技术交流时所作的Redis方面的一个presentation. 因为原件是Keynote格式,已经转成PDF,点击下面链接打开或者下载PDF: Redis 杂谈
- 【转载】SQL Server 2012 日志传送
SQL Server 2012 日志传送 一.准备: 数据库为完全恢复模式,并事先做一次完全备份. 共享一个文件夹,主机备份放在这个文件夹,而且客户机有权访问这个共享文件夹. 二.基本配置 1.启动配 ...
- 联玛客(T 面试)
我看你写的项目都是SSM架构,那我们就来聊下Spring 1.Spring的生命周期,与生命周期相关的事件? 2.阿里巴巴开发手册中的规范有哪些? 切到了异常捕捉话题 3.线程你有了解吗? 创建线程的 ...
- Vue 在beaforeCreate时获取data中的数据
众所周知,vue在beforecreate时期是获取不到data中的 数据的 但是通过一些方法可以实现在beforecreate时获取到data中的数据 暂时想到两种放发可以实现,vue在before ...
- hard fault 学习记录
使用 segger 的 hard fault 的源文件后,当调试时,发生硬件错误的时候,可以查看 HardFaultRegs 中的内容,并对比 segger_HardFaultHandler.c 中的 ...
- 解决margin塌陷问题
父元素添加: border: 1px solid transparent; 或者 over-flow:hidden;
- source 1.7 中不支持 lambda 表达式(请使用 -source 8 或更高版本以启用 lambda 表达式)
from:http://blog.csdn.net/qwdafedv/article/details/54691740 https://www.youtube.com/watch?v=oXxijdf8 ...
- luogu P2734 游戏 A Game
https://www.luogu.org/problemnew/show/P2734 数据范围比较小,二位DP可做,而luogu 3004,虽然几乎一模一样(只是数据范围大点),则需要压维. 定义f ...
- 微信小程序wx.request请求服务器json数据并渲染到页面
[原文出自]: https://blog.csdn.net/weixin_39927850/article/details/79766259 微信小程序的数据总不能写死吧,肯定是要结合数据库来做数据更 ...