Code Forces Bear and Forgotten Tree 3 639B
B. Bear and Forgotten Tree 3
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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”.
Input
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.
Output
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.
Examples
input
5 3 2
output
1 2
1 3
3 4
3 5
input
8 5 2
output
-1
input
8 4 2
output
4 8
5 7
2 3
8 1
2 1
5 6
1 5
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
using namespace std;
#define MAX 150000
int n,d,h;
int v[MAX+5];
int tag[MAX+5];
int main()
{
scanf("%d%d%d",&n,&d,&h);
if(d>2*h||d<h||(d==1&&h==1&&n!=2))
{printf("-1\n");return 0;}
int i;
memset(v,-1,sizeof(v));
memset(tag,0,sizeof(tag));
for( i=1;i<=h;i++)
{
v[i]=i+1;
tag[i]=1;tag[i+1]=1;
cout<<i<<" "<<v[i]<<endl;
}
if(d-h>0)
{
v[1]=i-1+2;
cout<<1<<" "<<v[1]<<endl;
tag[i-1+2]=1;
}
for(int j=i+2-1;j<d-h-1+i+2-1;j++)
{
v[j]=j+1;
cout<<j<<" "<<v[j]<<endl;
tag[j]=1;tag[j+1]=1;
}
int num;
if(h==1)
num=1;
else
num=2;
for(int j=1;j<=n;j++)
{
if(tag[j]==0)
{
cout<<num<<" "<<j<<endl;
}
}
return 0;
}
Code Forces Bear and Forgotten Tree 3 639B的更多相关文章
- 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 ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表
E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...
- 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 ...
- 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 ...
- 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 ...
- [Codeforces 639B] Bear and Forgotten Tree 3
[题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...
- 【Codeforces 639B】Bear and Forgotten Tree 3
[链接] 我是链接,点我呀:) [题意] [题解] 首先,因为高度是h 所以肯定1下面有连续的h个点依次连成一条链.->用了h+1个点了 然后,考虑d这个约束. 会发现,形成d的这个路径,它一定 ...
- CodeForces 658C Bear and Forgotten Tree 3 (构造)
题意:构造出一个 n 个结点,直径为 m,高度为 h 的树. 析:先构造高度,然后再构造直径,都全了,多余的边放到叶子上,注意直径为1的情况. 代码如下: #pragma comment(linker ...
随机推荐
- shell脚本分析 nginx日志访问次数最多及最耗时的页面
当服务器压力比较大,跑起来很费力时候.我们经常做站点页面优化,会去查找那些页面访问次数比较多,而且比较费时. 找到那些访问次数高,并且比较耗时的地址,就行相关优化,会取得立竿见影的效果的. 下面是我在 ...
- Atitit. Dwr 抛出异常error解决方案
Atitit. Dwr 抛出异常error解决方案 1. Dwr3的处理机制..setErrorHandler 1 2. remote Mteh try catch 1 3. 林吧,子好java 处 ...
- Windows版变色龙
打包安装版本更新源地址: http://www.insanelymac.com/forum/files/file/59-chameleon-22-svn/ 一.使用方法:1.安装Windows版变色龙 ...
- trk压力测试工具(测试tcp)
wrk 是web站点压力测试工具 针对tcp协议的压力测试工具,没有找到合适的. 自己写一个,起名 trk.
- Dubbo源代码实现三:注册中心Registry
我们知道,对于服务治理框架来说,服务通信(RPC)和服务管理两部分必不可少,而服务管理又分为服务注册.服务发现和服务人工介入,我们来看看Dubbo框架的结构图(来源网络): 图中可以看出,服务提供者P ...
- poj3067 Japan(树状数组)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3067">http://poj.org/problem? id=3067 Descri ...
- SenCha Touch 与 EXTJS 安装Myeclipse 插件
http://www.cnblogs.com/jirimutu01/default.html 关于SenchaEclipsePlugin插件的安装和使用 使用过eclipse开发java程序的人都知道 ...
- 从此sudo不再输密码
#sudo visudo 最后一次输入密码. 在最后一行加入: xxx ALL=NOPASSWD: ALL xxx即为你当前使用的用户名,Ctrl+X,保存退出. 从此告别每次都要输密码的时代.
- javax.Servlet的包中,属于类的是。(选择1项)
javax.Servlet的包中,属于类的是.(选择1项) A.Servlet B.GenericServlet C.ServletRequest D.ServletContext 解答:B Serv ...
- 【noip模拟题】天神下凡(贪心)
vijos某次模拟赛原题... 处理出每个圆的一级祖先就行了... 其实没有那么麻烦,贪心即可出解. 我们将每个圆转换成线段后按左端点小右端点大的方法排序 然后维护一个栈: 对于每一个圆i 如果栈顶右 ...