【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 [题解] 傻逼 ...
随机推荐
- U4704 函数
U4704 函数 题目背景 设gcd(a,b)为a和b的最大公约数,xor(a,b)为a异或b的结果. 最大公约数 异或 题目描述 kkk总是把gcd写成xor.今天数学考试恰好出到了gcd(a,b) ...
- luogu1268 树的重量
题目大意 给出一棵树上每两个叶子节点之间的距离,求树的总边权和. 题解 定义节点a到b的简单路径长度为[a,b],树中节点c要到达路径[a,b]所要经过的距离为dist(c, [a,b]),在树中,与 ...
- linux select函数:Linux下select函数的使用详解【转】
本文转载自;http://www.bkjia.com/article/28216.html Linux下select函数的使用 Linux下select函数的使用 一.Select 函数详细介绍 Se ...
- P1043 数字游戏
P1043 数字游戏 题目描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n ...
- poj--1274--The Perfect Stall(匈牙利裸题)
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21868 Accepted: 980 ...
- poj2594——最小路径覆盖
Description Have you ever read any book about treasure exploration? Have you ever see any film about ...
- css 浮动问题详解
浮动(float),一个我们即爱又恨的属性.爱,因为通过浮动,我们能很方便地布局: 恨,浮动之后遗留下来太多的问题需要解决,特别是IE6-7(以下无特殊说明均指 windows 平台的 IE浏览器). ...
- C指针基础知识
指针的声明 C语言声明格式:"类型 变量名;" 基本类型:int hoge; 指针类型:int *pointer; 区别在于: 声明 含义 int hoge; 声明整数类型的变量 ...
- Django分页器及自定义分页器
Django的分页器 view from django.shortcuts import render,HttpResponse # Create your views here. from app0 ...
- Django html页面 'ascii' codec can't encode characters in position 8-10: ordinal not
用Django开发的页面,之前用的是python3.X,后来又换成python2.X后各种报错,编码问题,于是在所有python文件开头加了编码:#coding=utf-8 但是后来发现,有些文件加了 ...