题意:构造出一个 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. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  2. Python 2.7_爬取CSDN单页面博客文章及url(二)_xpath提取_20170118

    上次用的是正则匹配文章title 和文章url,因为最近在看Scrapy框架爬虫 需要了解xpath语法 学习了下拿这个例子练手 1.爬取的单页面还是这个rooturl:http://blog.csd ...

  3. LeetCode Maximum Average Subarray I

    原题链接在这里:https://leetcode.com/problems/maximum-average-subarray-i/description/ 题目: Given an array con ...

  4. C# winform窗口打开特效及窗口位置居中

    在启动一个程序时,我们希望窗口显示的位置处于屏幕的正中心,可以如下设置:  MainForm mainForm = new MainForm();  mainForm.StartPosition =  ...

  5. Hibernate检索方式(转载)

    我们在项目应用中对数据进行最多的操作就是查询,数据的查询在所有ORM框架中也占有极其重要的地位. 那么,如何利用Hibernate查询数据呢?Hibernate为我们提供了多种数据查询的方式,又称为H ...

  6. DropShadowEffect导致下拉框控件抖动

    <!--<Border.Effect> <DropShadowEffect Direction="180" BlurRadius="1" ...

  7. Asp.net工作流workflow实战之工作流启动与继续(三)

    工作流帮助类: //让工作流继续沿着书签的位置向下执行value是向书签传递参数 wfc.ResumeBookmark(bookmarkName,value); //把传过来的数据value作为输出参 ...

  8. laravel 多个where的连接使用

    在查询的时候需要用到多个where条件来查询 1.直接多个where连接 ->where()->where() 2.把查询条件 放到where数组$where中 然后 ->where ...

  9. php 字符串的分割

    http://blog.sina.com.cn/s/blog_71ed1b870102uysa.html

  10. PHP7卓越性能背后的原理有哪些?

    作者:韩天峰链接:http://www.zhihu.com/question/38148900/answer/75115687来源:知乎 PHP7在运行原理上与PHP5相比并没有变化,这与hhvm不同 ...