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 ...
随机推荐
- iOS 自己定义页面的切换动画与交互动画 By Swift
在iOS7之前,开发人员为了寻求自己定义Navigation Controller的Push/Pop动画,仅仅能受限于子类化一个UINavigationController,或是用自己定义的动画去覆盖 ...
- IOS中公布应用程序,进度条一直不走怎么处理
在IOS中公布应用程序非常是喜闻乐见. 近期1周.我更新了6次版本号.可是时不时的会卡住,进度条不走. 最后总结了几个原因. 1.在公布前你要确认自己的证书是否配置正确 2.DNS域名server有没 ...
- CPU调度算法
批处理系统中的调度算法: *需要考虑的因素: 1. 吞吐量 2. cpu利用率 3. 周转时间 4. 公平性* 1.先来先服务: FCFS: 优点:实现简单 缺点:可能造成周转时间长 2.最短作业优先 ...
- 在命令行上启用 64 位 Visual C++ 工具集
Visual C++ 包含可用于创建 apps 在 32 位上运行,64 位,或基于 ARM 的 windows 操作系统的编译器. 下面的列表描述了 cl.exe(Visual C++ 编译器)的各 ...
- 前端要给力之:语句在JavaScript中的值
文件夹 文件夹 问题是语句有值吗 那么说你骗我咯 有啥米用呢 研究这个是不是闲得那个啥疼 ES5ES6有什么差异呢 结论是ES6是改了规则但更合理 最后不不过if语句 这两天在写语言精髓那本书的第三版 ...
- map和string的使用方法
这个是别人写的map使用方法比較好能够看一下 http://www.cnblogs.com/anywei/archive/2011/10/27/2226830.html 怎样向数组中插入内容 http ...
- Unix环境高级编程——守护进程记录总结(从基础到实现)
一.概念及其特征 守护进程是系统中生存期较长的一种进程,常常在系统引导装入时启动,在系统关闭时终止,没有控制终端,在后台运行.守护进程脱离于终端是为了避免进程在执行过程中的信息在任何终端上显示并且进程 ...
- 【题解】P1407国家集训队稳定婚姻
[题解][P1407 国家集训队]稳定婚姻 很好的一道建模+图论题. 婚姻关系?很像二分图匹配呀,不过不管怎么办先建模再说.婚姻关系显然用图方面的知识解决.建图! 它给定的是字符串,所以我们使用\(a ...
- 同源策略 , CORS
一 . 同源策略 同源策略( Same origin policy ) 是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响,可以说Web是构建在同源 ...
- POJ - 1426 Find The Multiple 【DFS】
题目链接 http://poj.org/problem?id=1426 题意 给出一个数 要求找出 只有 0 和 1 组成的 十进制数字 能够整除 n n 不超过 200 十进制数字位数 不超过100 ...