首页 > 其他 > 详细

快捷键

时间:2014-03-11 19:42:29      阅读:397      评论:0      收藏:0      [点我收藏+]

raph 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 nintegers, 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 nvertices 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

A graphic sequence is a sequence of numbers which can be the degree sequence of some graph. A sequence can be checked to determine if it is graphic using GraphicQ[g] in the Mathematica package Combinatorica` .

Erd?s and Gallai (1960) proved that a degree sequence bubuko.com,布布扣 is graphic iff the sum of vertex degrees is even and the sequence obeys the property

bubuko.com,布布扣

for each integer bubuko.com,布布扣 (Skiena 1990, p. 157), and this condition also generalizes to directed graphs. Tripathi and Vijay (2003) showed that this inequality need be checked only for as many bubuko.com,布布扣 as there are distinct terms in the sequence, not for all bubuko.com,布布扣.



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,num[10010],sum[10010];
bool cmp(int a,int b){
    return b<a;
}
int main(){

    while(~scanf("%d",&n)&&n){
           sum[0] = 0;
            for(int i = 1; i <= n; i++){
                scanf("%d",&num[i]);
            }
            sort(num+1,num+n+1,cmp);
            for(int i = 1; i <= n; i++)
                sum[i] = num[i]+sum[i-1];
            if(sum[n]%2==0){
                bool flag = 1;
                for(int i = 1; i <= n; i++){
                    int k = i*(i-1);
                    if(k>=sum[i]) continue;
                    for(int j = i+1; j <= n; j++){
                        k += min(i,num[j]);
                    }
                  //  cout<<k<<"   "<<sum[i]<<" "<<i<<endl;
                    if(sum[i] > k){
                        flag = 0;
                        break;
                    }
                }
                if(flag)
                    cout<<"Possible"<<endl;
                else
                    cout<<"Not possible"<<endl;
            }else{
                cout<<"Not possible"<<endl;
            }

    }
    return 0;
}



快捷键,布布扣,bubuko.com

快捷键

原文:http://blog.csdn.net/a49688448/article/details/21012679

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!