codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)
题目链接:
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 题意: 给一棵树的节点数目n,直径d,最大深度h,问是否存在一棵树满足这些条件,存在的话任意输出一棵树;
比赛时先是被人hack,发现了一处错误改了后又交了一发,但最后还是挂在了system test上; 思路: 两种情况不存在树,一种是d>2*h,一种是d==1&&h==1&&n>2时;可以先生成最大深度的一条链,再在次基础上把最大直径的链生成;其余的都和最大深度的节点的父节点相连接就满足要求了; AC代码:
/*
2014300227 658C - 38 GNU C++11 Accepted 46 ms 2176 KB
*/ #include <bits/stdc++.h>
using namespace std;
int n,d,h;
int main()
{
scanf("%d%d%d",&n,&d,&h);
if(d>*h||(h==&&d==&&n>))cout<<"-1"<<"\n";
else
{
for(int i=;i<=h;i++)
{
printf("%d %d\n",i,i+);
}
if(d==h)
{
if(d<n-)
{
for(int i=h+;i<=n;i++)
{
printf("%d %d\n",h,i);
}
}
}
else
{ cout<<""<<" "<<h+<<"\n";
for(int i=h+;i<=d;i++)
{
printf("%d %d\n",i,i+);
}
for(int i=d+;i<=n;i++)
{
printf("%d %d\n",h,i);
} } } return ;
}
codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)的更多相关文章
- 【Codeforces 639B】Bear and Forgotten Tree 3
[链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...
- Codeforces 193E - Fibonacci Number(打表找规律+乱搞)
Codeforces 题目传送门 & 洛谷题目传送门 蠢蠢的我竟然第一眼想套通项公式?然鹅显然 \(5\) 在 \(\bmod 10^{13}\) 意义下并没有二次剩余--我真是活回去了... ...
- Codeforces Round #493 (Div. 2) C. Convert to Ones 乱搞_构造_好题
题意: 给你一个长度为 nnn 的 010101串 ,你有两种操作: 1.将一个子串翻转,花费 XXX 2.将一个子串中的0变成1,1变成0,花费 YYY 求你将这个01串变成全是1的串的最少花费. ...
- Codeforces 1186F - Vus the Cossack and a Graph 模拟乱搞/欧拉回路
题意:给你一张无向图,要求对这张图进行删边操作,要求删边之后的图的总边数 >= ceil((n + m) / 2), 每个点的度数 >= ceil(deg[i] / 2).(deg[i]是 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- UML--组件图,部署图
组件图用于实现代码之间的物理结构,详细来说,就是实现代码交互.通过接口,将不同的软件,程序连接在一起. [理解] 1.组件的定义相当广泛,包含:源码,子系统,动态链接库,Activex控件. 2.组件 ...
- hint指定index的深入理解
http://czmmiao.iteye.com/blog/1480247创建一个表,含有位图index和b-tree index SQL> create table t as select o ...
- 超出用省略号function()
//判断是否微信浏览器 function isWeiXin() { var ua = window.navigator.userAgent.toLowerCase(); if (ua.match(/M ...
- python多进程生成缩略图
在img目录下7张图片 分别是 11.jpg 22.jpg 33.jpg 44.jpg 55.jpg 66.jpg 77.jpg #encoding=utf-8 import os import ti ...
- RF --系统关键字开发
需求: 接收一个目录路径,自动遍历目录下以及子目录下的所有批处理(.bat) 文件并执行. 首先在..\Python27\Lib\site-packages 目录下创建 CustomLibrary 目 ...
- Netty(五):Netty中如何序列化数据
JDK提供了ObjectOutputStream和ObjectInputStream,用于通过网络对POJO的基本数据类型和图进行序列化和反序列化.该API并不复杂,而且可以被应用于任何实现了java ...
- Ruby on Rails 路由解析
为了更好的阅读体验.欢迎訪问 作者博客原文 Route是什么 Rails中URL的约定严格基于RESTful风格的.client的请求事实上是在操作一些资源.同一资源的不同的请求动作(GET, POS ...
- RGB颜色空间alpha混合的方法
http://blog.csdn.net/xhhjin/article/details/6444782http://blog.csdn.net/xhhjin/article/details/64454 ...
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes the given c ...
- WCF: 以Json格式返回对象
1.先建一个WCF Service 建一个ServiceContract接口 1 [ServiceContract] public interface IJsonWCFService { /// &l ...