VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
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
Below you can see trees printed to the output in the first sample and the third sample.

构造。
每次先以1为根构造深度为h的一支树杈1-2-3...h,如果h==d那么就在2下面接上1个节点 如果h!=d 那么就在1下面接上d-h个节点。
注意h==d的时候。2 1 1是有解的。
/* ***********************************************
Author :guanjun
Created Time :2016/3/29 9:49:51
File Name :cfvk1c.cpp
************************************************ */
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>
#include <list>
#include <deque>
#include <stack>
#define ull unsigned long long
#define ll long long
#define mod 90001
#define INF 0x3f3f3f3f
#define maxn 100010
#define cle(a) memset(a,0,sizeof(a))
const ull inf = 1LL << ;
const double eps=1e-;
using namespace std;
priority_queue<int,vector<int>,greater<int> >pq;
struct Node{
int x,y;
};
struct cmp{
bool operator()(Node a,Node b){
if(a.x==b.x) return a.y> b.y;
return a.x>b.x;
}
}; bool cmp(int a,int b){
return a>b;
}
vector<int>v[maxn];
int main()
{
#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
#endif
//freopen("out.txt","w",stdout);
int n,d,h;
while(cin>>n>>d>>h){
if(d>h*){
puts("-1");continue;
}
int num=;
int vec,w;
if(d==h){
if(h==&&n>){
cout<<-<<endl;continue;
}
vec=,w=d-h+;
}
else vec=,w=d-h;
for(int i=;i<=h;i++){
v[num].push_back(num+);
num++;
}
for(int i=;i<=d-h;i++){
if(i==)v[vec].push_back(num+);
else v[num].push_back(num+);
num++;
//puts("-1");
}
//cout<<"num "<<num<<endl;
int mark=;
while(){
if(num==n)break;
for(int i=;i<=w;i++){
if(i==)v[vec].push_back(num+);
else v[num].push_back(num+);
num++;
if(num==n){
mark=;break;
}
}
if(mark)break;
}
for(int i=;i<=n;i++){
for(int j=;j<v[i].size();j++){
cout<<i<<" "<<v[i][j]<<endl;
}
}
}
return ;
}
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
Below you can see trees printed to the output in the first sample and the third sample.

VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3的更多相关文章
- 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 ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths
题目链接: http://codeforces.com/contest/673/problem/D 题意: 给四个不同点a,b,c,d,求是否能构造出两条哈密顿通路,一条a到b,一条c到d. 题解: ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors
题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths 构造
D. Bear and Two Paths 题目连接: http://www.codeforces.com/contest/673/problem/D Description Bearland has ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C. Bear and Colors 暴力
C. Bear and Colors 题目连接: http://www.codeforces.com/contest/673/problem/C Description Bear Limak has ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题
A. Bear and Game 题目连接: http://www.codeforces.com/contest/673/problem/A Description Bear Limak likes ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) E. Bear and Contribution 单调队列
E. Bear and Contribution 题目连接: http://www.codeforces.com/contest/658/problem/E Description Codeforce ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) D. Bear and Polynomials
D. Bear and Polynomials 题目连接: http://www.codeforces.com/contest/658/problem/D Description Limak is a ...
- VK Cup 2016 - Round 1 (Div. 2 Edition) B. Bear and Displayed Friends 树状数组
B. Bear and Displayed Friends 题目连接: http://www.codeforces.com/contest/658/problem/B Description Lima ...
随机推荐
- eclipse中AXIS2发布过程
Axis2服务端研究好几个小时,终于解决了 需要下载: 地址1: 可以从镜像站下载: 上海大学开源镜像站 地址2: 链接:从百度网盘下载; 密码:8nwu 其中第二个可以不用下: 解压后 将3,4解压 ...
- vector 类中的 push_back( ) 函数
函数名 push_back,算法语言里面的一个函数名,如: 1) c++中的vector头文件里面就有这个push_back函数: 2) 在vector类中作用为在vector尾部加入一个数据 ...
- 扰动法--*BZOJ3157: 国王奇遇记
求$\sum_{i=1}^ni^mm^i$.$n \leq 1e9,m \leq 200$. 其实我也不知道这东西为啥叫“扰动法”,大概是在黑暗的边缘试探?就是那种,人家再多一点就被您看破了,然后您就 ...
- Cards BZOJ 1004
Cards [问题描述] 小春现在很清闲,面对书桌上的N张牌,他决定给每张染色,目前小春只有3种颜色:红色,蓝色,绿色.他询问Sun有多少种染色方案,Sun很快就给出了答案.进一步,小春要求染出Sr张 ...
- grequests----golang的requests库
github.com/levigross/grequests: A Go "clone" of the great and famous Requests library 特点: ...
- AC日记——[USACO08DEC]干草出售Hay For Sale 洛谷 P2925
题目描述 Farmer John suffered a terrible loss when giant Australian cockroaches ate the entirety of his ...
- IntelliJ IDEA 使用的问题总结
第一个问题:idea 无法创建springboot的项目 1. 点击IDEA setting之后,找到Http Proxy 选择Atuo-detect proxy settings 之后点击 ...
- linux crontab 定时器
crontab -e 编辑定时器 crontab -l 显示当前定时器 crontab -r 删除当前定时器 格式 * * * * * command 第一列表示分钟1-59 第二列表示小时1-23 ...
- 【kotlin】kotlin中List中添加List怎么操作
如题,List集合添加一个List集合怎么操作 如上,现在有了List<A>,A类中有个字段List<B>, 新创建一个List<B>,想把LIst<A> ...
- Linux索引节点(Inode:no space for device)用满导致的一次故障
问题描写叙述 在storm測试环境集群上上nimbus和supervisor自己主动挂调.重新启动时显示no space for device,也不能创建,加入文件及文件夹,df -h查看 ilesy ...