1. 程式人生 > >Code-force 1003 E Tree Constructing

Code-force 1003 E Tree Constructing

rect contains ac代碼 using conn them connect input 不可

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 (1n,d,k4?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