题意:构造出一个 n 个结点,直径为 m,高度为 h 的树。

析:先构造高度,然后再构造直径,都全了,多余的边放到叶子上,注意直径为1的情况。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} vector<P> ans; int main(){
int h;
scanf("%d %d %d", &n, &m, &h);
bool ok = true;
int cnt = 1, last = 0;
for(int i = 0; i < h; ++i){
last = cnt;
ans.push_back(P(cnt, cnt+1));
++cnt;
if(cnt > n) ok = false;
}
int idx = 0;
if(m > h){ ans.push_back(P(1, cnt+1)); ++cnt; ++idx; }
if(cnt > n) ok = false;
for(int i = 1; i < m-h; ++i){
ans.push_back(P(cnt, cnt+1));
++cnt;
++idx;
if(cnt > n) ok = false;
}
while(cnt < n && m > 1) ans.push_back(P(last, cnt+1)), ++cnt;
if(idx > h || cnt != n) ok = false;
if(!ok){ printf("-1\n"); return 0; }
for(int i = 0; i < ans.size(); ++i)
printf("%d %d\n", ans[i].first, ans[i].second);
return 0;
}

  

CodeForces 658C Bear and Forgotten Tree 3 (构造)的更多相关文章

  1. Codeforces 658C Bear and Forgotten Tree 3【构造】

    题目链接: http://codeforces.com/contest/658/problem/C 题意: 给定结点数,树的直径(两点的最长距离),树的高度(1号结点距离其他结点的最长距离),写出树边 ...

  2. 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 ...

  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 ...

  4. [Codeforces 639B] Bear and Forgotten Tree 3

    [题目链接] https://codeforces.com/problemset/problem/639/B [算法] 当d > n - 1或h > n - 1时 , 无解 当2h < ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) E - Bear and Forgotten Tree 2 链表

    E - Bear and Forgotten Tree 2 思路:先不考虑1这个点,求有多少个连通块,每个连通块里有多少个点能和1连,这样就能确定1的度数的上下界. 求连通块用链表维护. #inclu ...

  9. 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 ...

随机推荐

  1. HihoCoder1403 后缀数组一·重复旋律1

    后缀数组一·重复旋律 时间限制:5000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi平时的一大兴趣爱好就是演奏钢琴.我们知道一个音乐旋律被表示为长度为 N 的数构成的数列. 小Hi ...

  2. 1151 LCA in a Binary Tree(30 分)

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  3. QString的拼接

    QString的append()函数则提供了类似的操作,例如: 1. str = "User: ";     2. str.append(userName);     3. str ...

  4. VMware tools的使用

    现在的开发环境基本上都是lnmp,所以我们需要在windows下使用虚拟机来搭建开发环境, 但是 在本地写好代码以后,需要上传到虚拟机的运行目录,每次修改都需要上传??? 不存在的!!! 使用Vmwa ...

  5. 一:HTML文本编译器 kindeditor-4.1.10 的使用 SpringMVC+jsp的实现

    一:我用的kindeditor版本是4.1.10 下载完成打开目录结构如下: 二:下面是工程目录也很重要,          三: 好了,准备工作已经做好了,现在就直接上代码了. 首先是页面JSP代码 ...

  6. n年的一次聚会

    今日聚会有a,b,b1,c,d 五人一起吃饭. 先谈谈各自的变化吧. a 毕业之后,他爸给他买了一个京a的牌照,然后出印度留学,然后回到北京,现在算一个中层领导,不过比较忙,刚刚聚餐完毕就立马回去加班 ...

  7. HDU1584(蜘蛛牌)

    蜘蛛牌 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. 2017-09-17 python 学习笔记

    Mock 库 介绍: http://blog.csdn.net/chdhust/article/details/50663729   说明mock能做什么. 可以考虑在调试方法时使用 Mock 库

  9. python-xlrd api

    1.导入模块 import xlrd from xlrd import open_workbook 2.打开Excel文件读取数据 data = xlrd.open_workbook('excelFi ...

  10. 2011-03-17免Oracle客户端连远程Oracle的方法

    1.http://www.oracle.com/technetwork/topics/winsoft-085727.html上下载对应版本的instanctclinet zip包 34M 解压后92M ...