[貪心]UVA10720 - Graph Construction

Problem C

Graph Construction

Time Limit

2 Seconds

Graph is a collection of edges E and vertices V. Graph has a wide variety of applications in computer. There are different ways to represent graph in computer. It can be represented by adjacency matrix or by adjacency list. There are some other ways to represent graph. One of them is to write the degrees (the numbers of edges that a vertex has) of each vertex. If there are n vertices then n integers can represent that graph. In this problem we are talking about simple graph which does not have same endpoints for more than one edge, and also does not have edges with the same endpoint.

Any graph can be represented by n number of integers. But the reverse is not always true. If you are given n integers, you have to find out whether this n numbers can represent the degrees of n vertices of a graph.

Input
Each line will start with the number n (≤ 10000). The next n integers will represent the degrees of n vertices of the graph. A 0 input for n will indicate end of input which should not be processed.

Output
If the n integers can represent a graph then print “Possible”. Otherwise print “Not possible”. Output for each test case should be on separate line.

Sample Input

Output for Sample Input

4 3 3 3 3
6 2 4 5 5 2 1
5 3 2 3 2 1
0

Possible
Not possible
Not possible


Problemsetter: Md. Bahlul Haider
Judge Solution: Mohammed Shamsul Alam
Special thanks to Tanveer Ahsan

題意:給出N個頂點的度數,判斷這N個頂點是否構成圖。

思路:爲保證其儘可能爲圖,要優先考慮大度數之間的點優先連接。在這裏可以採取依次減度數的方法推斷能否構成圖。如:3,3,2,2,1五個點,從度數最大的開始考慮,先去掉3,則後面緊接的三個點度數依次減去1,剩下的四個點排序後爲2,1,1,1,再去掉度數最大的2,後面的兩個點一次減去1,排序後剩下三點爲1,0,0,1後的一點度數再減去1,則爲-1,明顯不成立,爲Not possible。

ps:簡直坑,提交了好多次,總是WA,找了好久的bug,發現最後的Not possible 中的possible首字母打成了大寫!!血的教訓,太粗心了,下次注意!

代碼:

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

class Node
{
public:
	string str;
	int cost;
}node[10005];

bool cmp(Node s1,Node s2)
{
	if(s1.cost!=s2.cost) return s1.cost<s2.cost;
	else return s1.str<s2.str;
}

int main()
{
	int num,pos;
	cin>>num;
	for(pos=1;pos<=num;pos++)
		{
			int x,y,z,r,t;
			cin>>x>>y>>z;
			char ch;
			for(int i=0;i<z;i++)
				{
					int work=x;
					string st="";
					while(cin>>ch&&ch!=':')
						{
							st=st+ch;
						}
					node[i].str=st;
					cin>>r>>ch>>t;
					int val=0;
					while(t<((work+1)/2*r)&&(work-(work+1)/2)>=y)
						{
							val=val+t;
							work=work-(work+1)/2;
						}
					val=val+(work-y)*r;
					node[i].cost=val;
				}
			sort(node,node+z,cmp);
			cout<<"Case "<<pos<<endl;
			for(int i=0;i<z;i++)
				{
					cout<<node[i].str<<" "<<node[i].cost<<endl;
				}
		}
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章