Graph Construction uva+贪心+Havel-Hakimi定理
2014-07-17 23:01
246 查看
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 |
代码:
#include<iostream> #include<cstdio> #include<algorithm> #define MMAX 10003 using namespace std; int p[MMAX]; int n; int cmp(const void *a ,const void *b){ return *(int*)b-*(int*)a; }///构造qsort的辅助函数 int main(){ while(~scanf("%d",&n)&&n){ int f=0; for(int i=0;i<n;i++){ scanf("%d",&p[i]); } for(int i=0;i<n;i++){ qsort(&p[i],n-i,sizeof(p[0]),cmp); if(p[i]>=n-i) {f=1;break;} if(!p[i]) {break;} for(int j=i+1;j<=i+p[i];j++){ p[j]--; if(p[j]<0) {f=1;break;} } if(f) break; } if(!f)printf("Possible\n"); else printf("Not possible\n"); } return 0;}
相关文章推荐
- uva10720(Graph Construction)贪心+havel定理
- UVA - 10720 Graph Construction 公式+贪心
- Havel-Hakimi定理(判断一个序列是否可图) UVA 10720
- uva 10720 - Graph Construction(贪心-Havel-Hakimi)
- 2013长沙 G Graph Reconstruction (Havel-Hakimi定理)
- uva10720 - Graph Construction(Havel-Hakimi定理)
- UVA-10720 Graph Construction(Havel-Hakimi定理)
- UVa10720 ( Graph Construction)(判断是否可化为简单图)
- uva10720 - Graph Construction(图重建)
- uva 10720 Graph Construction()
- ZOJ 3732 Graph Reconstruction Havel_Hakimi定理
- UVA 10720 Graph Construction(贪心 + Havel-Hakimi定理)
- UVa 10720 - Graph Construction(Havel-Hakimi定理)
- uva--10720Graph Construction +Havel--Hakimi定理
- POJ 1659 Frogs' Neighborhood (贪心+Havel-Hakimi定理)
- uva 11054 Wine trading in Gergovia(贪心)
- uva 11491 Erasing and Winning 奖品的价值(贪心)
- uva 10020 Minimal coverage(贪心-最小覆盖问题)
- 习题8-12 UVA - 1153 Keep the Customer Satisfied 顾客是上帝(优先队列贪心)
- UVa 1611 Crane (构造+贪心)