uva 10720 Graph Construction

來源:互聯網
上載者:User

原題:
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 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
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
大意:
給你一個n個值,代表有n個頂點。然後給你n個值,分別表示該頂點的度。(是個無向圖)
現在問你給你的這n個值是否能構成圖。

#include <bits/stdc++.h>using namespace std;//fstream in,out;int n;int a[10001];int cmp(const int &x,const int &y){    return x>y;}int main(){    ios::sync_with_stdio(false);    int flag;    while(cin>>n,n)    {        flag=1;        for(int i=1;i<=n;++i)        {            cin>>a[i];            if(a[i]>n)                flag=0;        }        if(n==1&&a[1]!=0||flag==0)        {            cout<<"Not possible"<<endl;            continue;        }        sort(a+1,a+1+n,cmp);        for(int i=1;i<=n;++i)        {            int index=a[i];            if(index==0)                break;            for(int j=i+1;j<=n&&j<=i+index;++j)            {                if(a[j]==0&&a[i]>0)                {                    flag=0;                    break;                }                --a[j];                --a[i];            }            if(!flag)                break;            if(a[i]>0)            {                flag=0;                break;            }            sort(a+1+i,a+1+n,cmp);        }        if(flag)            cout<<"Possible"<<endl;        else            cout<<"Not possible"<<endl;    }    return 0;}

解答:
首先如果某個頂點的度大於頂點的個數那肯定是不可能的,而且如果頂點只有一個,那麼這個頂點的度只能是0。
排除上述情況後,對給的頂點進行從大到小的排序。比如第一個頂點是a[1]=3,那麼就讓–a[2],–a[3],–a[4],相當於去掉了一個頂點,並把它所串連的節點都摘下去一個邊,然後對剩下的頂點再次排序。如果某個頂點不能摘去和它度數相等的邊,那就說明這個圖不能構成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.