codeforces 827B. High Load

Codeforces Tutorial

B. High Load

Problem Analysis

只要贪心地从根节点引出k条边,每条边再往下延伸,并使得每个叶子节点的深度差不超过1,就可以了。

理解了问题的意思,构造的思路也有了,关键在构造的步骤。
构造一个星型的图的思路是:将图转换成以1为根节点的树,然后从根节点开始引出k条边,然后逐层加点连边,最后一定的结果一定满足条件。
注意,除了根节点层和底层,所有层的节点数都等于k。利用这一点,可以计算树的直径。

Acepted Code

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<cmath>
#include<map>
#include<istream>
#include<cassert>
#include<set>
#include<queue>
#define DEBUG(x) cout<<#x<<" = "<<x<<endl
#define DEBUG2(x,y) cout<<#x<<" = "<<x<<" , "\
<<#y<<" = "<<y<<endl
using namespace std;
typedef long long ll;
const int MAXN=2e5+10;
int n,k;
int main()
{
//    freopen("in.txt","r",stdin);
    scanf("%d%d",&n,&k);
    n--;
    int len1=n/k;
    int len2=(n%k)?len1+1:len1;
    int ans=(n%k>=2)?len2*2:len1+len2;
    n++;
    printf("%d\n",ans);
    for(int ii=2;ii<=k+1 ;ii++ ){
        printf("%d %d\n",1,ii);
    }
    for(int ii=k+2;ii<=n ;ii++ ){
        printf("%d %d\n",ii-k,ii);
    }
}

Wrong Answer Cases

Test 1

没有输出长度

What I Learn

  • 按照树的思路构造图的方法
  • 整除和树每一层的节点数的关系

Reference

https://blog.csdn.net/qq_36306833/article/details/77951734

转载于:https://www.cnblogs.com/MalcolmMeng/p/10940034.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值