Bear and Forgotten Tree 3
limit 2s,256M
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.
sameple
input
8 4 2
output
4 8
5 7
2 3
8 1
2 1
5 6
1 5
题意
要求构造一棵树,满足要求
树的节点个数为n
树的直径(其中两点间最大距离)为d
树的高度(根节点到其他点的最大距离)为h
题解
首先想到的是先构造出长为h的链,再把d-h的链构造出来,剩下的节点都直接连在1上
可以推出两个限制条件
1.d>=2*h;
2.d==1当且仅当n==2
还有一种情况,h==d,此时其余节点不能挂在1上,也不能挂在链的尾端,应该挂在其他节点上。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5;
void solve(int n,int d,int h)
{
int i;
if (h==d)
{
for (i=1;i<=h;++i)
{
printf("%d %d\n",i,i+1);
}
++i;
for (;i<=n;++i)
{
printf("%d %d\n",2,i);
}
}
if (h!=d)
{
for (i=1;i<=h;++i)
{
printf("%d %d\n",i,i+1);
}
++i;
printf("%d %d\n",1,i);
for(;i<=d;++i)
{
printf("%d %d\n",i,i+1);
}
++i;
for (;i<=n;++i)
{
printf("%d %d\n",1,i);
}
}
}
int main(void)
{
#ifdef ex1
freopen("in.txt","r",stdin);
#endif
int n,d,h;
scanf("%d%d%d",&n,&d,&h);
if (d>2*h) printf("-1");
else if (d<2 && n!=2) printf("-1");
else
{
solve (n,d,h);
}
}
比赛的时候最后时刻才想起来h==d && h!=n-1的情况,匆匆忙忙没有想到解决方法,非常可惜的没过……