E. Tree Constructing

You are given three integers nn, dd and kk.

Your task is to construct an undirected tree on nn vertices with diameter dd and degree of each vertex at most kk, or say that it is impossible.

An undirected tree is a connected undirected graph with n−1n−1 edges.

Diameter of a tree is the maximum length of a simple path (a path in which each vertex appears at most once) between all pairs of vertices of this tree.

Degree of a vertex is the number of edges incident to this vertex (i.e. for a vertex uu it is the number of edges (u,v)(u,v) that belong to the tree, where vv is any other vertex of a tree).

Input

The first line of the input contains three integers nn, dd and kk (1≤n,d,k≤4⋅1051≤n,d,k≤4⋅105).

Output

If there is no tree satisfying the conditions above, print only one word "NO" (without quotes).

Otherwise in the first line print "YES" (without quotes), and then print n−1n−1 lines describing edges of a tree satisfying the conditions above. Vertices of the tree must be numbered from 11 to nn. You can print edges and vertices connected by an edge in any order. If there are multiple answers, print any of them.1

题意:

  要构建一颗树,树的直径要是d,就是树上的节点最远的距离为d,每个节点的度最多为k,一共有n个节点。

思路:

  先判断可不可以建出符合要求的树,可以的话,先把直径建好,再到这条直径上挂上子树。

  

  注意子树的深度变化,还有建树的过程中,并不一定是建的如此之满,所以当节点数用完时,要及时退出。

AC代码(C++)

#include<iostream>
#include<queue>
using namespace std;
int n,d,k,flag,t;
int quick_pow(int a,int b)
{
int ans=0,t=1;
for(int i=0;i<b;i++){
t*=a;
ans+=t;
if(ans>=n){return -1;}
}
return ans;
} void build(int s,int h,int de)//de是子树的深度,s是父节点
{
if(h>de){return;}
if(t>=n){return;}
t++;
printf("%d %d\n",s,t);
if(t>=n){return;}
int o=t;
if(h>de-1){return;}
for(int i=0;i<k-1;i++){
build(o,h+1,de);
if(t>=n){return;}
}
} int main()
{
scanf("%d%d%d",&n,&d,&k);
if(n<d+1){printf("NO");return 0;}
if(d&1){
t=quick_pow(k-1,d/2);
if(t==-1){printf("YES\n");}
else{
t=2*(t+1);
if(t<n){printf("NO");return 0;}
}
}
else {
t=quick_pow(k-1,d/2-1);
if(t==-1){printf("YES\n");}
else{
t=k*t+1+k;
if(t<n){printf("NO");return 0;}
}
}
if(t!=-1){printf("YES\n");}
int de=-1;
t=d+1;
for(int i=1;i<=d;i++){
printf("%d %d\n",i,i+1);
if(t>=n){continue;}
if(d&1){
if(i<=d/2+1){de++;}
else if(i>d/2+2){de--;}
}
else {
if(i<=d/2+1){de++;}
else de--;
}
for(int j=0;j<k-2;j++){
build(i,1,de);if(t>=n){break;}
}
}
}

  

Code-force 1003 E Tree Constructing的更多相关文章

  1. pycharm debug后会出现 step over /step into/step into my code /force step into /step out 分别表示

    1.debug,全部打印 2.打断点debug,出现单步调试等按钮,只运行断点前 3.setup over 调试一行代码 4.setup out 运行断点后面所有代码 5.debug窗口显示调试按钮 ...

  2. 【Code Force】Round #589 (Div. 2) D、Complete Tripartite

    题目链接 大致题意 把一个图分成三块,要求任意两块之间是完全图,块内部没有连线 分析 首先根据块内没有连线可以直接分成两块 假定点1是属于块1的,那么所有与点1连接的点,都不属于块1:反之则是块1的 ...

  3. code force 424 A - Office Keys

    There are n people and k keys on a straight line. Every person wants to get to the office which is l ...

  4. code force 403C.C. Andryusha and Colored Balloons

    C. Andryusha and Colored Balloons time limit per test 2 seconds memory limit per test 256 megabytes ...

  5. code force 403B.B. The Meeting Place Cannot Be Changed

    B. The Meeting Place Cannot Be Changed time limit per test 5 seconds memory limit per test 256 megab ...

  6. code force 401B. Game of Credit Cards

    B. Game of Credit Cards time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  7. Tree Constructing CodeForces - 1003E(构造)

    题意: 就是让构造一个直径为d的树  每个结点的度数不能超过k 解析: 先构造出一条直径为d的树枝 然后去遍历这条树枝上的每个点  为每个点在不超过度数和直径的条件下添加子嗣即可 #include & ...

  8. CF1003E Tree Constructing 构造+树论

    正解:构造 解题报告: 传送门! 这题麻油翻译鸭,,,那就先大概港下题意趴QAQ 构造一棵n个点,直径为d,每个点点度不超过k的树 这题其实我jio得还是比较简单的趴,,, 首先构造出一条直径,就是一 ...

  9. Code Force 21B Intersection

    B. Intersection time limit per test1 second memory limit per test256 megabytes inputstandard input o ...

随机推荐

  1. PHP从入门到精通(二)

     PHP从入门到精通 之PHP中的函数 各位开发者朋友大家好,自上次更新PHP的相关知识,得到了大家的广泛支持.PHP的火爆程度不言而喻,函数作为PHP中极为重要的部分,应诸位的支持,博主继续跟进更新 ...

  2. 个人作业week7——前端开发感想总结

    个人作业week7——前端开发感想总结 1. 反思 首先要谈谈在这次团队项目的工作中,我这边出现过的较为严重的一个问题:我和HoerWing (后端担当)合作时,最初因为我没有使用github(始终连 ...

  3. 《Linux内核分析》第六周学习总结

    <Linux内核分析>第六周学习总结                         ——进程的描述和进程的创建 姓名:王玮怡  学号:20135116 一.理论部分 (一)进程的描述 1 ...

  4. Linux内核分析——第十八章 调试

    第十八章    调试 18.1 准备开始 1.在用户级的程序里,bug表现比较直接:在内核中却不清晰. 2.内核级开发的调试工作远比用户级开发艰难的多. 3.准备工作需要的是: (1)一个bug (2 ...

  5. 20135323符运锦----第七周:Linux内核如何装载和启动一个可执行程序

    可执行程序的装载 一.预处理.编译.链接和目标文件的格式 1.可执行程序是怎么得来的 ①编译器预处理 gcc -E -o XX.cpp XX.c (-m32)// 注:把include的文件包含进来, ...

  6. IT行业的创新的读后感

    一.什么是创新 创新是以新思维.新发明和新描述为特征的一种概念化过程.它原意有三层含义,第一,更新:第二,创造新的东西:第三,改变.创新是人类特有的认识能力和实践能力,是人类主观能动性的高级表现形式, ...

  7. 利用ini_set()函数实现对php配置文件的修改

    PHP的配置文件是php.ini,如果要开启或者关闭扩展,还有设置一些模块的相关配置是,就得对该文件进行修改, 修改的方法也很简单,打开php.ini找到对应项直接修改,修改之后需要重新启动才能生效. ...

  8. 安裝CentOS7后修復win7引导

    想尝试双系统的心情想必大家都能理解,但是安装了双系统之后的收尾工作也是必不可少的,由于对Linux并不算很熟悉,所以在这方面花了不少时间,这里将CentOS7下修復windows7引导的解决方案记录下 ...

  9. Java并发—synchronized关键字

    synchronized关键字的作用是线程同步,而线程的同步是为了防止多个线程访问一个数据对象时,对数据造成的破坏. synchronized用法 1. 在需要同步的方法的方法签名中加入synchro ...

  10. ThreadPoolExecutor使用详解

    ThreadPoolExecutor机制  一.概述 1.ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线 ...