[贪心]UVA10720 - Graph Construction
2014-04-06 11:43
232 查看
Problem C | Graph Construction |
Time Limit | 2 Seconds |
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 |
Judge Solution: Mohammed Shamsul Alam
Special thanks to Tanveer Ahsan[/align]
题意:给出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;
}
相关文章推荐
- Cocopods安装和使用
- Win8.1自带的微软拼音输入法卡顿打不出字的解决方法
- win8.1系统网速慢的原因及加快网速的几种方法
- 1_Linux基础知识及命令――――登陆登出及图形化界面的初始化
- 系统时钟
- .net 开发技术 + 棋王竞猜游戏娱乐网站(www.51qiwang.com)
- ubuntu 13.10 64 安装subclipse
- 如何得到plsql中执行时报错的SQL的位置行号
- 程序缘 Flex AIR之旅 --软件安装
- java String对象比较之equals和==的区别
- 亲子操打开孩子感官能力
- PS CS6 打不开RAW格式文件
- Java动态代理一------动态类Proxy的使用
- linux上修改主机名对oracle启动关闭的影响
- Win8.1系统自带的微软拼音输入法无法设置的解决方法
- JAVA读取Excel的实现(利用jxl.jar读取excel)
- vim配合ctags和cscope生成tag
- Ubuntu下git服务器搭建
- 【原创】如何获得近10年的1分钟完整历史数据并导入MT4
- HDU1498 枚举+二分图类棋盘问题(最大匹配)