Degree Sequence of Graph G(Havel定理)

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.
Degree Sequence of Graph G(Havel定理)_第1张图片
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

思路:
第一步:按照降序排序,
第二步:将第一个值给x,再将第一个数变为0
第三步:从第二个数开始,连续x个数减一,如果有了为<0的,那么就不可能构成简单图了。

重复上述操作n次。
Ps:度数的总和为偶数,并且每个点最多的度应该为n-1

#include 
#include 
using namespace std;

int a[1005];

bool cmp(int a,int b){
	return a > b;
}
int fun(int n){
	int ans = 0;
	for(int i = 0;i < n;i++){
		if(a[i] > n-1) return 0;
		ans += a[i];
	}
	if(ans%2 != 0) return 0;
	for(int i = 0;i < n;i++){
		sort(a,a+n,cmp);
		int x = a[0];
		a[0] = 0;
		int j = 1;
		while(x--){
			a[j]--;
			if(a[j] < 0) return 0;
			j++;
		}
	}
	return 1;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		for(int i = 0;i < n;i++){
			scanf("%d",a+i);
		}
		if(fun(n)){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
	return 0;
}

你可能感兴趣的:(POJ,Havel定理)