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 ...
随机推荐
- Hibernate中的inverse和cascade属性
Hibernate中的inverse和cascade属性 inverse的值有两种,"true"和"false".inverse="false&quo ...
- codevs 5438 zbd之难题(水题日常)
时间限制: 1 s 空间限制: 1000 KB 题目等级 : 白银 Silver 题目描述 Description zbd想要一个计算器,请你编一个计算器. 输入描述 Input Descrip ...
- 计数器:counter
组成:2属性,1方法 属性1: counter-reset 命名 属性2: counter-increment 启动/自增 方法 : counter()/counters() 调用方法 1.计数器 命 ...
- 在Eclipse中设置自动补全
在Eclipse中菜单中,Window->Preferences->Java->Editor->Content Assist中的Auto activation triggers ...
- CAD交互绘制带颜色宽度的直线(网页版)
用户可以在CAD控件视区任意位置绘制直线. 主要用到函数说明: _DMxDrawX::DrawLine 绘制一个直线.详细说明如下: 参数 说明 DOUBLE dX1 直线的开始点x坐标 DOUBLE ...
- sklearn之SVC
sklearn.svm.SVC(C=1.0, kernel='rbf', degree=3, gamma='auto', coef0=0.0, shrinking=True, probability= ...
- 利用CWinThread实现跨线程父子MFC窗口
利用CWinThread实现跨线程父子MFC窗口 MFC对象只能由创建该对象的线程访问,而不能由其他线程访问. 不遵守该准则将导致断言(assertion)或者无法预知的程序行为等运行期错误. 在多线 ...
- webpack、node、npm之间的关系
webpack能够把.vue后缀名的文件打包成浏览器能够识别的js 而这个.vue文件装换需要打包器vue-loader 这个vue-loader打包器是可以从npm上面下载(npm上面有很多资源包) ...
- 第4节 hive调优:2、数据倾斜
数据的倾斜: 主要就是合理的控制我们的map个数以及reduce个数 第一个问题:maptask的个数怎么定的???与我们文件的block块相关,默认一个block块就是对应一个maptask 第二个 ...
- android滚动图片
关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...