Link:
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_ problem&problem=1661
Original title:
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 the adjacency matrix or by adjacency list. There are some 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 to more than one edge, and also does Not have edges with the same endpoint.
Any graph can is represented by n number of integers. But the reverse is not always true. If you are are given n integers, you have to find out whether this n numbers can represent the degrees of n vertices of a grap H.
Input
Each line would start with the number n (≤10000). The next n integers would represent the degrees of n vertices of the graph. A 0 input for n would indicate end of input which should is not processed.
Output
If the n integers can represent a graph then print "Possible". Otherwise print "not possible". Output for each test case should is on separate line.
Sample input:
4 3 3 3 3
6 2 4 5 5 2 1
5 3 2 3 2 1
0
Sample output:
Possible
Not possible
Not possible
The main effect of the topic:
Give a graph with n dots, and then give the degree of each point. Find out if this diagram is possible.
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Analysis and Summary:
is also a stark application of the Havel-hakimi theorem.
Code:
* * uva:10720-graph construction * Greed, Havel-hakimi theorem * time:0.040s * author:d_double * * * */#include <i
ostream> #include <algorithm> #include <cstdio> #define MAXN 10005 using namespace std;
int arr[maxn],n;
BOOL Havel_hakimi () {for (int i=0; i<n-1; ++i) {sort (arr+i,arr+n,greater<int> ());
if (I+arr[i] >= N) return false;
for (int j=i+1; j<=i+arr[i]; ++j) {--arr[j];
if (Arr[j] < 0) return false;
} if (arr[n-1]!=0) return false;
return true;
int main () {while (scanf ("%d", &n) &&n) {int sum=0;
BOOL Flag=true;
for (int i=0; i<n; ++i) {scanf ("%d", &arr[i]);
if (arr[i]>n-1) Flag=false;
Sum + + arr[i];
} if (sum%2!=0) Flag=false;
if (sum==0 | | flag&&havel_hakimi ()) printf ("possible\n"); else printf ("Not possible\n");
return 0; }
Author: csdn Blog shuangde800