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;elsenum=2;for(int j=1;j<=n;j++){if(tag[j]==0){cout<<num<<" "<<j<<endl;}}return 0;}

转载于:https://www.cnblogs.com/dacc123/p/8228715.html

Code Forces Bear and Forgotten Tree 3 639B相关推荐

  1. 【VK Cup 2016 - Round 1 (Div 2 Edition)C】【构造】Bear and Forgotten Tree 3 构造一棵树直径为d且点1的深度为h

    Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. CodeForces 658C Bear and Forgotten Tree 3(构造)

    题意:构造一棵树,有N个点,直径为d,深度为h 思路:首先构造一个长度为d的链,然后把其中一个距离边上为h的点变为根.然后我们就不停的在距离根为h上面的那一点不停的加点就好了,使得新加入的点的距离也为 ...

  3. 图论 ---- E. Bear and Forgotten Tree 2(判补图的联通性技巧 图遍历的优化 条件拆分)

    题目大意 题目大意: 给你nnn个点,mmm对关系表示(ai,bi)(a_i,b_i)(ai​,bi​)之间是没有边的问你能否构建出一颗树满足111号点的度数为kkk? 解题思路: 如果没有后面的条件 ...

  4. Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]

    题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...

  5. Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树所有左右子数 ...

  6. 【Code forces】63B Settlers' Training

    http://codeforces.com/problemset/problem/63/B 给你一串数字,直到所有数字都变为k为止,相同的数为一组,在一次中,所有不同的数都加1 1 2 2 3  →  ...

  7. 【题解】CF#611 H-New Year and Forgotten Tree

    有趣啊~手玩一下这棵树,发现因为连边只对相连点的位数有限制,我们可以认为是在往一棵已经有 m 个结点的树上挂叶子结点直到满足要求.(m = log(10) n).注意由于 m 超级无敌小,我们可以直接 ...

  8. Code Forces 448C Painting Fence 贪婪的递归

    略有上升称号,最近有很多问题,弥补啊,各类竞赛滥用,来不及做出了所有的冠军.这个话题 这是一个长期记忆的主题.这是不是太困难,基本技能更灵活的测试,每次我们来看看这个问题可以被删除,处理然后分段层,贪 ...

  9. code forces 436 C. Bus

    C. Bus time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

最新文章

  1. 值得期待的.Net Micro Framework 3.0
  2. 20款效果非常棒的 jQuery 插件分享
  3. Java Daemon线程
  4. 设计模式-观察者模式(Observer)
  5. 数据结构(单链表的相关操作)
  6. BugkuCTF-MISC题妹子的陌陌
  7. Python中的_main_与_init_详解
  8. Docker快速搭建JIRA缺陷管理平台
  9. 面试高级测试工程师修炼之接口测试平台开发
  10. python+webdriver(二)
  11. 请检查网站服务器是否正常.,请检查服务器地址是否正确
  12. 查看自己本地IP地址方法
  13. vue项目rem 大屏可视化适配
  14. 淘宝前后端分离实践(PPT)
  15. MacBook文本转语音代码
  16. PyTorch之填充操作
  17. 打包安卓apk后的V1和V2签名
  18. 我们的征途是星辰大海 ( 蓝桥杯~算法提高 )
  19. 2021年儋州市高考成绩查询,儋州召开2020年高考中考总结会暨2021年高考中考动员会...
  20. 《交互媒体专题设计》之疫情模拟 设计文档

热门文章

  1. 窗口键 键位码_键盘上这些被冷落的键位居然有这么强大的功能
  2. vscode 高效使用指南
  3. 后勤问题怎么办。。。(求刊登)
  4. LARS 算法简介-机器学习
  5. tf.concat()详解
  6. dataframe构建
  7. 点击事件如何传递到Activity中
  8. HarmonyOS Java工程目录结构
  9. Ubuntu 系统如何修改主机名
  10. Android 相对布局别自己快遗忘的属性layout_alignRight,layout_alignBottom,layout_alignTop,layout_alignLeft