Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2454
Problem descriptionwang 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 partitioned strated 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, 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?
Inputthe 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.
Outputfor each case, the answer shoshould be "yes" or "no" indicating this case is "Draw-possible" or "non-draw-possible"
Sample Input
26 4 4 3 3 2 24 2 1 1 1
Sample output
yesno
Source2008 Asia Regional Harbin
Question:
Give the degree of each vertex in a graph and determine whether a simple graph can be formed;
PS:
Havel theorem: http://baike.baidu.com/view/8698382.htm? Fr = Aladdin
Given a non-negative integer sequence {DN}, if an undirected graph maps the degrees of each point in the graph to the sequence one by one, this sequence is called regionalization. Further, if the figure is a simple image, it is called this sequence. This sequence can be simply visualized and visualized: d1 + D2 + ...... DN = 0 (mod 2 ). For the construction of a specific graph, we can simply pair the vertices with an odd number, and make all the rest into a self-ring. Havel theorem: arrange sequences in ascending order, that is, D1 >=d2 >=……> = DN, then D can be simple graph when and only when d '= {d2-1, d3-1 ,...... D (d1 + 1)-1, D (d1 + 2), D (d1 + 3 ),...... DN} can be simply illustrated. Simply put, after sorting D, find the point with the highest degree (set the degree to D1) and link it to the D1 point with the highest degree, then, you can ignore this point and continue the process until you create a complete graph, or when the negative degree is obviously unreasonable.
The Code is as follows:
#include<cstdio>#include<algorithm>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; i++) { scanf("%d",&a[i]); sum+=a[i]; } if(sum%2) { printf("no\n"); continue; } for(i=0; i<n; i++) { if(a[i]>=n) break; } if(i<n) { printf("no\n"); continue; } int flag = 0; for(i=0; i<n; i++) { int cnt=0; sort(a,a+n,cmp); for(j=1; j<n; j++) { if(cnt==a[0]) break; a[j]--; cnt++; if(a[j] < 0) { flag = 1; break; } } if(flag) break; if(cnt==0) break; a[0]-=cnt; } for(i=0; i<n; i++) { //printf("%d ",a[i]); if(a[i]) break; } //printf("\n"); if(i<n || flag) printf("no\n"); else printf("yes\n"); } return 0;}/*44 3 2 1 1*/
HDU 2454 degree sequence of graph G (Havel theorem judges the existence of a simple graph)