uva 10720 Graph Construction
2013-08-15 20:53
337 查看
Problem C | Graph Construction |
Time Limit | 2 Seconds |
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 |
用的Havel-Hakimi定理
#include <iostream> #include <cstring> #include <cstdio> #include<cmath> #include <algorithm> using namespace std; int n; int a[10010]; bool cmp(int a,int b) { return a>b; } int solve() { int i,j,k; for(i=0;i<n-1;i++) { sort(a+i,a+n,cmp); int tmp=a[i]; if(i+tmp>=n) return 0; for(j=i+1;j<=i+tmp;j++) { a[j]--; if(a[j]<0) return 0; } } if(a[n-1]!=0) return 0; return 1; } int main() { int i; while(~scanf("%d",&n)&&n) { for(i=0;i<n;i++) scanf("%d",&a[i]); if(solve()) printf("Possible\n"); else printf("Not possible\n"); } return 0; }
相关文章推荐
- UVa10720
- UVa 10720 - Graph Construction
- UVA 10720 - Graph Construction
- UVa10720 ( Graph Construction)(判断是否可化为简单图)
- uva 10720 Graph Construction
- UVa 10720 - Graph Construction
- uva 10720 Graph Construction()
- UVA 10720 Graph Construction
- uva 10720 Graph Construction
- uva 10720 - Graph Construction
- [贪心]UVA10720 - Graph Construction
- UVA 10720 Graph Construction
- UVa 10720 - Graph Construction(Havel-Hakimi定理)
- UVa10720 - Graph Construction
- UVA10720- Graph Construction
- UVA 10720 Graph Construction
- uva 10720
- uva10720(Graph Construction)贪心+havel定理
- uva10720 - Graph Construction(图重建)
- UVa:10720 Graph Construction