zoukankan      html  css  js  c++  java
  • 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
    ",ans);
        for(int ii=2;ii<=k+1 ;ii++ ){
            printf("%d %d
    ",1,ii);
        }
        for(int ii=k+2;ii<=n ;ii++ ){
            printf("%d %d
    ",ii-k,ii);
        }
    }
    
    

    Wrong Answer Cases

    Test 1

    没有输出长度

    What I Learn

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

    Reference

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

  • 相关阅读:
    使用jQuery对象
    jQuery插件
    使用jQuery函数
    jQuery的两把利器
    初始jQuery
    BOM——特效
    BOM的介绍
    DOM——节点操作
    miaov- 自动生成正V反V大于号V小于号V楼梯等图案
    H5 -- 本地存储计数器的值 和前端校验用户
  • 原文地址:https://www.cnblogs.com/MalcolmMeng/p/10940034.html
Copyright © 2011-2022 走看看