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

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

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #define debug() puts("++++");
  18. #define gcd(a, b) __gcd(a, b)
  19. #define lson l,m,rt<<1
  20. #define rson m+1,r,rt<<1|1
  21. #define freopenr freopen("in.txt", "r", stdin)
  22. #define freopenw freopen("out.txt", "w", stdout)
  23. using namespace std;
  24.  
  25. typedef long long LL;
  26. typedef unsigned long long ULL;
  27. typedef pair<int, int> P;
  28. const int INF = 0x3f3f3f3f;
  29. const LL LNF = 1e16;
  30. const double inf = 0x3f3f3f3f3f3f;
  31. const double PI = acos(-1.0);
  32. const double eps = 1e-8;
  33. const int maxn = 100 + 10;
  34. const int mod = 1000000007;
  35. const int dr[] = {-1, 0, 1, 0};
  36. const int dc[] = {0, 1, 0, -1};
  37. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  38. int n, m;
  39. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  40. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  41. inline bool is_in(int r, int c){
  42. return r >= 0 && r < n && c >= 0 && c < m;
  43. }
  44.  
  45. vector<P> ans;
  46.  
  47. int main(){
  48. int h;
  49. scanf("%d %d %d", &n, &m, &h);
  50. bool ok = true;
  51. int cnt = 1, last = 0;
  52. for(int i = 0; i < h; ++i){
  53. last = cnt;
  54. ans.push_back(P(cnt, cnt+1));
  55. ++cnt;
  56. if(cnt > n) ok = false;
  57. }
  58. int idx = 0;
  59. if(m > h){ ans.push_back(P(1, cnt+1)); ++cnt; ++idx; }
  60. if(cnt > n) ok = false;
  61. for(int i = 1; i < m-h; ++i){
  62. ans.push_back(P(cnt, cnt+1));
  63. ++cnt;
  64. ++idx;
  65. if(cnt > n) ok = false;
  66. }
  67. while(cnt < n && m > 1) ans.push_back(P(last, cnt+1)), ++cnt;
  68. if(idx > h || cnt != n) ok = false;
  69. if(!ok){ printf("-1\n"); return 0; }
  70. for(int i = 0; i < ans.size(); ++i)
  71. printf("%d %d\n", ans[i].first, ans[i].second);
  72. return 0;
  73. }

  

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. mac下cocos+android在.bash_profile文件里的配置

    既包含了已经消失了的老板本"cocos"软件相关的配置,也包含当时最新的cocos2d-x-3.11引擎包的相关配置 支持把cocos引擎相关代码预编译出库文件存放到prebuil ...

  2. Zijian-lv #3 树句节狗提

    如你所见,这是一道狗题 一棵树,多次询问与一个点距离至少为 $k$ 的点的权值和 $n,q \leq 2525010$ sol: 长链剖分 需要注意的是这道题卡空间 我把我所有的 vector 换成链 ...

  3. Milking Time(DP)

    个人心得:一开始自己找状态,是这么理解的,只要前面一个满足就等于此时的值加上d(n-1),否则就是不挖此时的比较d(n-1)和 d(n-2)+cost,不过仔细一想忽略了很多问题,你无法确定n-2和此 ...

  4. BZOJ3075,LG3082 [USACO13MAR]项链Necklace

    题意 Bessie the cow has arranged a string of N rocks, each containing a single letter of the alphabet, ...

  5. bzoj 3709: [PA2014]Bohater 贪心

    题目: 在一款电脑游戏中,你需要打败\(n\)只怪物(从\(1\)到\(n\)编号).为了打败第\(i\)只怪物,你需要消耗\(d_i\)点生命值,但怪物死后会掉落血药,使你恢复\(a_i\)点生命值 ...

  6. Maven实现直接部署Web项目到Tomcat7

    如题目,自动部署到Web服务器,直接上过程. 1.Tomcat7的用户及权限配置:在conf目录下,找到tomcat-users.xml,添加manager权限的用户. <role rolena ...

  7. 高效C#编码优化

    1.foreach VS for 语句 Foreach 要比for具有更好的执行效率Foreach的平均花费时间只有for的30%.通过测试结果在for和foreach都可以使用的情况下,我们推荐使用 ...

  8. EM算法以及推导

    EM算法 Jensen不等式 其实Jensen不等式正是我们熟知的convex函数和concave函数性质,对于convex函数,有 \[ \lambda f(x) + (1-\lambda)f(y) ...

  9. nfs cron shell 作业

    作业一: nginx反向代理三台web服务器,实现负载均衡 所有的web服务共享一台nfs的存储 2台服务器 nginx [lb] :101.200.206.6 nginx [web]:101.200 ...

  10. Solaris10如何确认DirectIO是否已经启用

    对于Oracle而言,如果数据库存储在UFS文件系统上,启用DirectIO能够提高数据库性能.Oracle有个参数filesystemio_options可以控制数据库是否使用DirectIO.  ...