您的位置:首页 > 产品设计 > UI/UE

hdu 2454 Degree Sequence of Graph G

2011-08-25 10:50 513 查看
给你点的度的序列,问你是否能构成一个简单图。

我开始想的很简单,当然很随意的就WA掉了。

后来觉得应该有判定方法,百度搜了下还真有 = =。。见这里http://wenku.baidu.com/view/4108997101f69e3143329415.html

后来看网上还有一种方法就是拓排了,把对应大的度数的点在其余每个点都减,类似拓排。

我好笨 = =。。。怎么没多想想呢。

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <limits.h>
#include <string.h>
#include <string>
#include <algorithm>
#define MID(x,y) ( ( x + y ) >> 1 )
#define L(x) ( x << 1 )
#define R(x) ( x << 1 | 1 )
#define BUG puts("here!!!")
#define STOP system("pause")

using namespace std;

const int MAX = 1010;
int deg[MAX];
bool check(int n)
{
	for(int i=1; i<=n; i++)
	{
		int s = 0;
		for(int k=1; k<=i; k++)
			s += deg[k];
		int s2 = 0;
		for(int k=i+1; k<=n; k++)
			s2 += min(i, deg[k]);
		
		if( s > i*(i-1) + s2 )	
			return false;
	}
	return true;
}

int main()
{
	int ncases, n;
	
	scanf("%d", &ncases);
	
	while( ncases-- )
	{
		scanf("%d", &n);
		int sum = 0;
		for(int i=1; i<=n; i++)
		{
			scanf("%d", °[i]);
			sum += deg[i];
		}
		
		sort(deg+1, deg+n+1, greater<int>());
		
		if( sum % 2 == 1 )
		{
			printf("no\n");
			continue;
		}
		
		if( check(n) )
			printf("yes\n");
		else
			printf("no\n");

	}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: