题目链接:

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. jQuery ajax参数后台获取不到的问题

    <script type="text/javascript"> init(); var alldate = {a : "0",b:"1&q ...

  2. 最新精品 强势来袭 XP,32/64位Win7,32/64位Win8,32/64位Win10系统【国庆版版】

    本系统是10月最新完整版本的Windows10 安装版镜像,Win10正式版,更新了重要补丁,提升应用加载速度,微软和百度今天宣布达成合作,百度成为Win10 Edge浏览器中国默认主页和搜索引擎,系 ...

  3. centos6上安装mysql8.0版本

    本博客是采用yum源的方式安装,非常的方便和快捷.(redhat 与centos7 等操作系统都可以采用此方法,步骤大体一致) mysql官网地址:   https://dev.mysql.com 开 ...

  4. Vue构建命令

    node -v npm -v vue -V npm install vue (这个命令不行) 提示信息:+ vue@2.6.10 updated 1 package and audited 1 pac ...

  5. 制作JPEGImages出现的bug

    我用的是下面这个脚本进行改名字: import os import sys path = "/home/bnrc/py-faster-rcnn/data/VOCdevkit2007/VOC2 ...

  6. C ++ _基础之共用体

    由以下代码来进一步学习共用体 #include <stdio.h> #include<iostream> void main() { union un { int a; cha ...

  7. Dubbo框架的说明

    说实话,自己现在做的项目中有用到dubbo,但是我所负责的那一个模块,并没有涉及到dubbo,想学习一下dubbo,之前是没有学习完,这次继续... 一.背景知识总结 二.服务治理 三.Dubbo架构 ...

  8. adb 调试真机 wait for device 错误解决办法

    起因 真机测试,使用adb安装和卸载应用时,出现wait for device. 解决办法 拔掉USB重新插入即可.

  9. My Friends

    HMQ's blog RMY's blog Shq's blog wjyyy‘s blog

  10. Hibernate-03

    目的:表操作(表维护) 一.一对一(略过) 二.一对 1.建表原则:在多的一方创建外键指向一的一方的外键 2.建表:实体中添加 商品实体表: private Set<User> user ...