UVaOJ10720 - Graph Construction

本文探讨了如何通过一组整数来判断是否可以构成简单图的顶点度数序列。利用哈维尔-哈基米算法,文章提供了一个C++实现方案,用于验证输入的整数序列是否满足作为某简单图顶点度数的要求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem C

Graph 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 givenn 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 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


Problemsetter: Md. Bahlul Haider
Judge Solution: Mohammed Shamsul Alam
Special thanks to Tanveer Ahsan
#include<iostream>
#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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值