【Codeforces 639B】Bear and Forgotten Tree 3
【链接】 我是链接,点我呀:)
【题意】
【题解】
首先,因为高度是h
所以肯定1下面有连续的h个点依次连成一条链。->用了h+1个点了
然后,考虑d这个约束。
会发现,形成d的这个路径,它一定是经过节点1比较好。
因为这条路径有两种可能->
1.经过了1节点
2.没有经过1节点,那么肯定是1的某个子树里面,但是如果它的子树里再来一条长度为d的路径,肯定没有比经过1来的好,因为如果在1的子树里面的话有增加树的高度h的风险。
为了降低这个超过h的风险,那么我们还是优先让这个路径经过节点1.
然后我们来构造这条长度为d的路径。因此还得在1节点的另外一个子树上创建d-h个节点。
同样接成一条链。
(但此时要判断这个d-h的高度会不会超过h,超过了的话那就无解了)
如果上面都没问题,那么高度h和d的要求就都满足。
但是别忘了还有一个节点个数的要求。
我们已经用了d+1个节点了。
还有n-d-1个节点没用。
这些点的话,可以这样,全都接在h号节点上(也即全都接在倒数第二层的节点上
(这里不是接成一条链了
这样就能做到既不会改变h,也不会改变d了。
但是有种情况要特判一下。
就是d = h =1的时候,这个时候只有两个节点,如果还有剩余的节点,那么就没办法再接了。因此也无解。
【代码】
#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define LL long long
using namespace std;
const int N = 1e4;
int n,d,h;
int main()
{
//freopen("D:\\rush.txt","r",stdin);
ios::sync_with_stdio(0),cin.tie(0);
cin >> n >> d >> h;
if (d-h>h){
cout<<-1<<endl;
return 0;
}
if (d==1){//1<=h<=d so h==d
if (n>=3){
cout<<-1<<endl;
return 0;
}
}
int cur = 1;
rep1(i,1,h){
cout<<cur<<' '<<cur+1<<endl;
cur++;
}
int now = 1;
rep1(i,1,d-h){
cout<<now<<' '<<cur+1<<endl;
now = ++cur;
}
rep1(i,1,n-d-1)
cout<<h<<' '<<++cur<<endl;
return 0;
}
【Codeforces 639B】Bear and Forgotten Tree 3的更多相关文章
- 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 ...
- 【codeforces 791D】 Bear and Tree Jumps
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之 ...
- 【30.36%】【codeforces 740D】Alyona and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【32.89%】【codeforces 574D】Bear and Blocks
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【codeforces 764C】Timofey and a tree
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 514E】Darth Vader and Tree
[题目链接]:http://codeforces.com/problemset/problem/514/E [题意] 无限节点的树; 每个节点都有n个儿子节点; 且每个节点与其第i个节点的距离都是ai ...
- 【codeforces 791C】Bear and Different Names
[题目链接]:http://codeforces.com/contest/791/problem/C [题意] 给你n-k+1个限制 要求 a[i]..a[i]+k-1里面有相同的元素,或全都不同; ...
- 【codeforces 791B】Bear and Friendship Condition
[题目链接]:http://codeforces.com/contest/791/problem/B [题意] 给你m对朋友关系; 如果x-y是朋友,y-z是朋友 要求x-z也是朋友. 问你所给的图是 ...
- 【codeforces 791A】Bear and Big Brother
[题目链接]:http://codeforces.com/contest/791/problem/A [题意] 给你两个数字a和b; a每次乘3,b每次乘2 问你什么时候a第一次大于b [题解] 傻逼 ...
随机推荐
- Mahout算法调用展示平台2.1
软件版本号: windows7: Tomcat7.JDK7.Spring4.0.2.Struts2.3.Hibernate4.3.myeclipse10.0.easyui:Linux(centos6. ...
- light oj1074
Description The people of Mohammadpur have decided to paint each of their houses red, green, or blue ...
- Linux(redhat 5.8)下 安装jetty 部署 使用
首先须要安装JDK,自行解决 下载jetty安装包http://dist.codehaus.org/jetty/ 我下载的是8.1.15 tar -xzvf *.tar.gz 解压完之后,基本就能够用 ...
- C#遍历DataSet与DataSet元素实现代码
C#中的Dataset就像一个数据库,有多个表(Table),一般只有一个表,然后每个表中有行(DataRow)和列(DataColumn),DataRow[DataColumn]可以得到某行某列数据 ...
- hdu 1532 Drainage Ditches(最大流)
Drainage Dit ...
- bzoj 1026 [ SCOI2009 ] windy数 —— 数位DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1026 蛮简单的数位DP,预处理 f[i][j] 表示 i 位数,以 j 开头的 windy ...
- bzoj3555 [Ctsc2014]企鹅QQ——字符串哈希
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3555 很久以前就讲过哈希,但一直没写过题,所以这是哈希第一题! 哈希就是把一个字符串映射成一 ...
- windows php文件下载地址
http://windows.php.net/downloads/releases/archives/
- 分享的js代码,从w3c上拓下来的
<!DOCTYPE html><html><head> <title></title> <script>window._bd_s ...
- go的语言结构
一.文件名.关键字与标识符 1.1 文件名 1.go 的源文件已 .go 为后缀名 2.文件名已小写组成 如:simple.go 3.如多个部分组成可用"_" 分割 4.不要包含有 ...