HDU 2454 Degree Sequence of Graph G(Havel定理判断単純図の存在)

5619 ワード

テーマリンク:http://acm.hdu.edu.cn/showproblem.php?pid=2454
Problem Description
Wang Haiyang is a strong and optimistic Chinese youngster.Although born and brought up in the northen inland city Harbin,he has deep love and yearns for the boundless oceans.After graduation,hers cant and cant cantヘヘヘドa position as a navigaton in a freighter and began his new life.
The cargo vessel、Wang Haiyang workd on、sails among 6 pots between which exist 9 routes.At the first sight of his navigation chart、the 6 portand 9 routes on it reminded Graph of Graphth theory theory the hestudit.Wang Haiyang regarded the navigation chart as.Therory.He consided the 6 portas 6 nodes and 9 routes as 9 edges of the graph.The graph is illusted as below.
 
Acctording to Graph Thory,the number of edges related to a node is defined as Degree number of this node.
Wang Haiyang looked the graphh and thought、If arraranged、the Degreenumbes of all nodes of graapph G can form suquence:4,4,3,2,2,which iscaled the degreesegreesequence of the grapapapaparerererererererererereapapapapapadededededededededededededegrapapapapapapapapaparererererererererererererererererererererereaaaapapapapapapapapapapapapapapapapapapapapapapapaparerererererererererering is a simple graph)is a non-negative integer sequence?
Wang Haiyang is a thought ful person and tens to think deeply over any scientific proble m that grabs his interest.So as usual、he also gave this problem further thout、As we knowany a simple graph always corese ponds a non-negative integer sequence.But whether a non-negative integer sequence always cores ponds the degree sequence of a simple graphh?That is、if given a non-negative integer sequence、are we sure that we can draw a simple grap accoding 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 graphh、the sequence draw-posshere、wiotenon-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 Algothm Design proversign proffice.
 
Input
The first line of input contains an integer T,indicates the number of test cases.In each case,there re n+1 numbers;first is an integer n(n<1000)、which indicates there re n integers in the sequence;the n follown integers,which indicate the numbers of the degree sequence.
 
Output
For each case,the answer shound 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

题意:

给出一个图的每一个点的度数,求能否构成一个简单图;

PS:

Havel定理:http://baike.baidu.com/view/8698382.htm?fr=aladdin


给定一个非负整数序列{dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化。进一步,若图为简单图,则称此序列可简单图化
可图化的判定:d1+d2+……dn=0(mod 2)。关于具体图的构造,我们可以简单地把奇数度的点配对,剩下的全部搞成自环。
可简单图化的判定(Havel定理):把序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d2-1,d3-1,……d(d1+1)-1, d(d1+2),d(d1+3),……dn}可简单图化。简单的说,把d排序后,找出度最大的点(设度为d1),把它与度次大的d1个点之间连边,然后这个点就可以不管了,一直继续这个过程,直到建出完整的图,或出现负度等明显不合理的情况。


代码如下:

#include
#include
using namespace std;
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int t,n,i,j;
    int a[1010];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        int sum = 0;
        for(i=0; i=n)
                break;
        }
        if(i