Link: HDU 2454 degree sequence of graph G
Question: Give the degree of N points (simple graph) and ask if a graph can be drawn. (In fact, it is to show whether a sequence of non-negative strings has a corresponding graph)
I have never seen this theorem.
Havel theorem is a sequence that gives a series of non-negative values. an undirected graph maps the degrees of each point in the graph to the sequence one by one. This sequence is called regionalization. A simple graph is a simple graph.
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.
Note:
1. The total degree of the graph is an even number.
2. The degree of each point does not exceed the N-1.
3. Non-descending sorting, and edge deletion in sequence. It is invalid if there is a negative degree.
AC code:
#include<stdio.h>#include<algorithm>using namespace std;bool cmp(int a,int b){ return a>b;}int main(){ int t,n,i,j; int a[1010],flag; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=0; i<n; i++) scanf("%d",&a[i]); for(i=0; i<n; i++) { if(a[i]>n) break; } if(i<n) { printf("no\n"); continue; } for(i=0; i<n; i++) { int cnt=0; flag=0; sort(a,a+n,cmp); for(j=1; j<n; j++) { if(cnt==a[0]) break; a[j]--; if(a[j]<0) { flag=1; break; } cnt++; } if(cnt==0) break; if(flag) break; a[0]-=cnt; } if(flag) { printf("no\n"); continue; } for(i=0; i<n; i++) { if(a[i]) break; } if(i<n) printf("no\n"); else printf("yes\n"); } return 0;}/*44 3 2 1 1*/
HDU 2454 degree sequence of graph G (Havel theorem can be identified by a simple graph)