HDU 2454 Degree Sequence of Graph G(Heavel定理)

来源:互联网 发布:python from 上级目录 编辑:程序博客网 时间:2024/06/06 13:55

Degree Sequence of Graph G

Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2717 Accepted Submission(s): 1168

Problem Description
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 Knoigsberg, 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?

Input
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.

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

Sample Input
2
6 4 4 3 3 2 2
4 2 1 1 1

Sample Output
yes
no

Source
2008 Asia Regional Harbin

Recommend
gaojie

题目大意:

  给你一张图中各个节点的度数,问能否构成一个简单图。

解题思路

  裸的Heavel定理。
  没有自环没有重边的图叫做简单图。
  如果一个度数序列存在一张无向图满足这个度数序列则称这个度数序列可图化。如果这张图是简单图,则称这个度数序列是可简单图化。
  对于可图化的判定,我们只需要判断是否deg[i] mod2==0即可。
  对于可简单图化的判定就是Heavel定理的内容了。

Heavel定理:

  把序列排成不增序,即deg[1]>=deg[2]>=>=d[V],则d可简单图化当且仅当d=d[21]d[31]d[(d1+1)1]d[(d1+2)]d[(d1+3)]d[V]可简单图化。简单的说,把d排序后,找出度最大的点(设度为d1),把它与度次大的d1个点之间连边,然后这个点就可以不管了,一直继续这个过程,直到建出完整的图,或出现负度或剩下的节点数小于当前点剩余度数。

AC代码:

#include <stdio.h>#include <vector>#include <iostream>#include <string.h>#include <algorithm>using namespace std;#define LL long longconst int MAXV=1000+3;int V, deg[MAXV];int main(){    int T_T;    scanf("%d", &T_T);    while(T_T--)    {        scanf("%d", &V);        for(int i=0;i<V;++i)            scanf("%d", &deg[i]);        bool ok=true;        for(int i=0;i<V;++i)        {            sort(deg+i, deg+V, greater<int>());            if(deg[i]<0 || i+deg[i]>=V)            {                ok=false;                break;            }            for(int j=1;j<=deg[i];++j)                --deg[i+j];        }        puts(ok?"yes":"no");    }    return 0;}
阅读全文
0 0
原创粉丝点击