码迷,mamicode.com
首页 > 其他好文 > 详细

Havel–Hakimi algorithm(判断度数序列是否可图)

时间:2019-08-07 21:02:04      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:city   负数   问题   就是   bit   als   ima   hide   should   

问题 J: Degree Sequence of Graph G

时间限制: 1 Sec  内存限制: 128 MB

题目描述

Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep love and yearns for the boundless oceans. After graduation, he came to a coastal city and got a job in a marine transportation company. There, he held a position as a navigator in a freighter and began his new life.

The cargo vessel, Wang Haiyang worked on, sails among 6 ports between which exist 9 routes. At the first sight of his navigation chart, the 6 ports and 9 routes on it reminded him of Graph Theory that he studied in class at university. In the way that Leonhard Euler solved The Seven Bridges of Königsberg, Wang Haiyang regarded the navigation chart as a graph of Graph Theory. He considered the 6 ports as 6 nodes and 9 routes as 9 edges of the graph. The graph is illustrated as below.
                                                                                                                                                技术图片
According to Graph Theory, the number of edges related to a node is defined as Degree number of this node.

Wang Haiyang looked at the graph and thought, “If arranged, the Degree numbers of all nodes of graph G can form such a sequence: 4, 4, 3,3,2,2, which is called the degree sequence of the graph. Of course, the degree sequence of any simple graph (according to Graph Theory, a graph without any parallel edge or ring is a simple graph) is a non-negative integer sequence”

Wang Haiyang is a thoughtful person and tends to think deeply over any scientific problem that grabs his interest. So as usual, he also gave this problem further thought, “as we know, any a simple graph always corresponds with a non-negative integer sequence. But whether a non-negative integer sequence always corresponds with the degree sequence of a simple graph? That is, if given a non-negative integer sequence, are we sure that we can draw a simple graph according to it.”

Let’s put forward such a definition: provided that a non-negative integer sequence is the degree sequence of a graph without any parallel edge or  ring, that is, a simple graph, the sequence is draw-possible,  otherwise, non-draw-possible. Now the problem faced with Wang Haiyang is how to test whether a non-negative integer sequence is draw-possible or not. Since Wang Haiyang hasn’t studied Algorithm Design course, it is difficult for him to solve such a problem. Can you help him?
 

 

输入

The first line of input contains an integer T, indicates the number of test cases. In each case, there are n+1 numbers; first is an integer n (n<1000), which indicates there are n integers in the sequence; then follow n integers, which indicate the numbers of the degree sequence.

 

输出

For each case, the answer should be “yes” or “no”, indicating this case is “draw-possible” or “non-draw-possible”.

 

样例输入

2
6 4 4 3 3 2 2
4 2 1 1 1

样例输出

yes
no

 


 

每次对点sort,然后对该点ai剔除,并且此后ai个点减一;举例如下:  

3  2 3 1 2 1

1.由大到小排序 :

3 3 2 2 1 1 

2.剔除第一个;

/*3 */      3 2 2 1 1 

3. 后面3个都减去1

/*3*/   2 1 1 0 1

4.再次排序

/*3*/   2 1 1 1 0

5.再剔除第一个

/*3 2*/  1 1 1 0

6. 后面2个每个减一

/*3 2*/  0 0 1 0

。。。。。最后直到全部是0就是可以成图,否则有负数,就不能成图。

 

int H(){
    for(int i=0;i<n-1; i++)
    {
        sort(a+i,a+n,greater<int>());
 
        if(a[i]+i>(n-1)) return 0;
 
        for(int j=i+1; j<=i+a[i] ; j++){
            --a[j];
            if(a[j]<0) return 0;
        }
    }
    if(a[n-1]!=0) return 0;
    return 1;
}

还有一种方法 是公式法:

技术图片

 

 题目AC代码如下(有点慢)

技术图片
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+50;
const int mod=998244353;
typedef long long ll;
int a[N];
int qian[N],hou[N];
int n;
int H(){
    for(int i=0;i<n-1; i++)
    {
        sort(a+i,a+n,greater<int>());
 
        if(a[i]+i>(n-1)) return 0;
 
        for(int j=i+1; j<=i+a[i] ; j++){
            --a[j];
            if(a[j]<0) return 0;
        }
    }
    if(a[n-1]!=0) return 0;
    return 1;
}
 
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        if(H()) printf("yes\n");
        else printf("no\n");
    }
 
    return 0;
}
View Code

 

Havel–Hakimi algorithm(判断度数序列是否可图)

标签:city   负数   问题   就是   bit   als   ima   hide   should   

原文地址:https://www.cnblogs.com/sylvia1111/p/11317678.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!