解题思考L. Degree Sequence of Graph G可图性判断

Problem Description
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 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 illustrated 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,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?

Input
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.

Output
For each case, the answer should be “yes”or “no” indicating this case is “draw-possible” or “non-draw-possible”

翻译题解:

王海洋是一个坚强而乐观的中国年轻人。 虽然在北方内陆城市哈尔滨出生长大,但他对无边无际的海洋有着深深的热爱和向往。 毕业后,他来到一个沿海城市,在一家海运公司找到了一份工作。 在那里,他担任了货船的领航员,开始了他的新生活。  

“王海洋”号货轮在6个港口间航行,其中有9条航线。 当他第一眼看到航海图时,上面的6个港口和9条航线让他想起了他在大学时学习过的图论。 王海洋把航海图看作是图论中的一种图,就像欧拉解出了“克诺斯堡七桥”一样。 他认为这6个端口就是6个节点,9条路由就是图的9条边。 图表如下所示。  

根据图论,与节点相关的边数定义为该节点的度数。  

王海洋看着图,心想,如果把图G的所有节点的度数排列起来,就可以形成这样一个序列:4、4、3、3、2、2,这就是图的度序列。 当然,任何简单图的度序列(根据图论,一个没有平行边或环的图就是一个简单图)是非负整数序列?  

王海洋是一个很有思想的人,凡是他感兴趣的科学问题,他都会深入思考。 和往常一样,他也对这个问题做了进一步的思考,我们知道,任何一个简单的图总是对应一个非负整数序列。 但是一个非负整数序列是否总是与一个简单图的度序列相对应呢? 也就是说,如果给定一个非负整数序列,我们确定可以根据它画出一个简单的图吗?  

我们提出这样一个定义:假设一个非负整数序列是一个没有任何平行边或环的图的度序列,即一个简单图,该序列是可画的,否则为不可画的。 现在王海洋面临的问题是如何检验一个非负整数序列是否可画。 由于王海洋没有学习过算法设计这门课,解决这个问题对他来说是很困难的。 你能帮他吗?  

输入  

输入的第一行包含一个整数T,表示测试用例的数量。 在每种情况下,都有n+1个数字; 第一个是整数n (n<1000),表示序列中有n个整数; 然后跟随n个整数,表示度序列的数。  

输出  

对于每一种情况,答案应该是“是”或“否”,表明这种情况是“可能抽取”或“不可能抽取”。  

 

输入样例

2
6 4 4 3 3 2 2
4 2 1 1 1
 

输出样例

yes
no

思考:考察可图性 

 

解决代码:

#include<bits/stdc++.h>
using	namespace	std;
int	judge(int	a[],int	n);
bool	cmp(int	a,int	b)
{
	return	a>b;
}
int	main()
{
	int	N;
	cin>>N;
	while(N--)
	{
		int	n;
		cin>>n;
		int	a[n+1];
		int	k=1;
		for(int	i=0;i<n;i++)						
			cin>>a[i];
		sort(a,a+n,cmp);
		for(int	j=0;j<n;j++)
		{
			for(int	i=1;i<=a[j];i++)
			{
				a[j+i]=a[j+i]-1;
			}
			if(judge(a,n))
			{
				k=0;
				break;
			}
			sort(a+j+1,a+n,cmp);
		}
		/*for(int	i=0;i<n;i++)
			cout<<a[i]<<endl;*/
		if(k)
			cout<<"yes"<<endl;
		else
			cout<<"no"<<endl;
	}
}
int	judge(int	a[],int	n)
{
	for(int	i=0;i<n;i++)
	{
		if(a[i]<0)
			return	1;//报告为失败 
	}
	return	0; 
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值