Codeforces 639B——Bear and Forgotten Tree 3——————【构造、树】
2 seconds
256 megabytes
standard input
standard output
A tree is a connected undirected graph consisting of n vertices and n - 1 edges. Vertices are numbered 1 through n.
Limak is a little polar bear and Radewoosh is his evil enemy. Limak once had a tree but Radewoosh stolen it. Bear is very sad now because he doesn't remember much about the tree — he can tell you only three values n, d and h:
- The tree had exactly n vertices.
- The tree had diameter d. In other words, d was the biggest distance between two vertices.
- Limak also remembers that he once rooted the tree in vertex 1 and after that its height was h. In other words, h was the biggest distance between vertex 1 and some other vertex.
The distance between two vertices of the tree is the number of edges on the simple path between them.
Help Limak to restore his tree. Check whether there exists a tree satisfying the given conditions. Find any such tree and print its edges in any order. It's also possible that Limak made a mistake and there is no suitable tree – in this case print "-1".
The first line contains three integers n, d and h (2 ≤ n ≤ 100 000, 1 ≤ h ≤ d ≤ n - 1) — the number of vertices, diameter, and height after rooting in vertex 1, respectively.
If there is no tree matching what Limak remembers, print the only line with "-1" (without the quotes).
Otherwise, describe any tree matching Limak's description. Print n - 1 lines, each with two space-separated integers – indices of vertices connected by an edge. If there are many valid trees, print any of them. You can print edges in any order.
5 3 2
1 2
1 3
3 4
3 5
8 5 2
-1
8 4 2
4 8
5 7
2 3
8 1
2 1
5 6
1 5
Below you can see trees printed to the output in the first sample and the third sample.
题目大意:给你n、d、h。表示以1结点为根的树,树直径为d,h为树深。问你是否存在这样的树,如果存在输出树的形态,否则输出-1.
解题思路:构造,都是细节。。。
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<string>
#include<iostream>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
using namespace std;
typedef long long LL;
#define mid (L+R)/2
#define lson rt*2,L,mid
#define rson rt*2+1,mid+1,R
#pragma comment(linker, "/STACK:102400000,102400000")
const int maxn = 1e4 + 300;
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
int main(){
int n, d, h;
while(scanf("%d%d%d",&n,&d,&h)!=EOF){
if(n == 2){
puts("1 2"); continue;
}
if(d > 2*h){
puts("-1"); continue;
}
if(d == 1 && n > 2){ // 3 1 1 4 1 1
puts("-1"); continue;
}
int st = 2;
if(d == h){
for(int i = 1; i <= d; i++){
printf("%d %d\n",st-1,st); st++;
}
int tst = st;
for(int i = 1; i <= n-tst+1; i++){
printf("%d %d\n",2, st); st++;
}
}else{
for(int i = 1; i <= h; i++){
printf("%d %d\n",st-1,st); st++;
}
for(int i = 1; i <= d-h; i++){
if(i == 1){
printf("%d %d\n",1,st++);
}else{
printf("%d %d\n",st-1,st); st++;
}
}
for(int i = 1; i <= n - d - 1; i++){
printf("%d %d\n",1, st); st++;
}
}
}
return 0;
}
Codeforces 639B——Bear and Forgotten Tree 3——————【构造、树】的更多相关文章
- [Codeforces 639B] Bear and Forgotten Tree 3
[题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...
- CodeForces 658C Bear and Forgotten Tree 3 (构造)
题意:构造出一个 n 个结点,直径为 m,高度为 h 的树. 析:先构造高度,然后再构造直径,都全了,多余的边放到叶子上,注意直径为1的情况. 代码如下: #pragma comment(linker ...
- 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 658C Bear and Forgotten Tree 3【构造】
题目链接: http://codeforces.com/contest/658/problem/C 题意: 给定结点数,树的直径(两点的最长距离),树的高度(1号结点距离其他结点的最长距离),写出树边 ...
- 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 ...
- 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 ...
随机推荐
- oracle 触发器序列号自增
步骤:1.创建表 table 2.创建序列 SEQUENCE 3.创建 触发器 截图实例:
- solr7.3集群搭建
solr集群搭建 原博客:https://blog.csdn.net/gdsgdh308227363/article/details/81004706 注意,在搭建solr集群前,建议最好有一个sol ...
- Android学习之 adb被占用解决办法
1.adb被占用解决办法 方法一:(1)查看5037端口哪个进程在用 netstat -a -o 5037 (2)查看上面进程是哪个执行文件在占用 tasklist ...
- “全栈2019”Java第四十七章:继承与方法
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- “全栈2019”Java第十三章:基本数据类型
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- “全栈2019”Java第四十四章:继承
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- webpack2的一些使用入门
首先创建一个webpack文件夹我取名叫webpackVue(为了后续把vue集成进来) 1.首先用npm初始化一下,在这个目录下,执行npm init 2.npm install webpack - ...
- 数学 CF1068B LCM
CF1068B LCM 给定一个正整数\(b (1\leq b \leq 10^{10})\). 把一个正整数a从1枚举到\(10^{18}\),求有多少种不同的\(\large \frac{[a,b ...
- dcoker做私有仓库
在官网上拉取私有仓库的镜像.(官网提供的镜像为:registry) docker pull registry 启动镜像,并且映射端口.(启动时加-p参数即可) docker -d -p 5000 ...
- selenium+python+unittest:一个类中只执行一次setUpClass和tearDownClass里面的内容(可解决重复打开浏览器和关闭浏览器,或重复登录等问题)
unittest框架是python自带的,所以直接import unittest即可,定义测试类时,父类是unittest.TestCase. 可实现执行测试前置条件.测试后置条件,对比预期结果和实际 ...