UVa 10720:Graph Construction

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=113&page=show_problem&problem=1661

原题:

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.

样例输入:

4 3 3 3 3
6 2 4 5 5 2 1
5 3 2 3 2 1
0

样例输出:

Possible
Not possible
Not possible

题目大意:

给出一张有n个点的图,然后再给出每个点的度数。求这个图是否可能存在。

更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

分析与总结:

也是赤裸裸的Havel-Hakimi定理的应用。

代码:

/*
 * UVa: 10720 - Graph Construction
 * 贪心,Havel-Hakimi定理
 * Time: 0.040s
 * Author: D_Double
 *
 */
#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;
}

作者:csdn博客 shuangde800

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索graph
, of
, The
construct
construction、construction bank、construction paper、fuzor construction、under construction,以便于您获取更多的相关知识。

时间: 2024-08-30 08:10:50

UVa 10720:Graph Construction的相关文章

UVa 193:Graph Coloring

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=129 原题: You are to write a program that tries to find an optimal coloring for a given graph. Colors are applied to the nodes

uva 10720 - Graph Construction

点击打开链接uva 10720 题目意思:   给定n个顶点的度,判断当前的这些顶点能否构成图 解题思路:   1: 贪心 2: Havel定理(证明可图化)   可简单图化的判定:把序列排成不增序,即d1>=d2>=-->=dn,则d可简单图化当且仅当d'={d2-1,d3-1,--d(d1+1)-1, d(d1+2),d(d1+3),--dn}可简单图化.简单的说,把d排序后,找出度最大的点(设度为d1),把它与度次大的d1个点之间连边,然后这个点就可以不管了,一直继续这个过程,直到

算法题:UVa 11069 A Graph Problem (斐波那契)

A Graph Problem Time limit: 2s Given an undirected graph of the following form with n nodes, 1 ≤ n ≤ 76: Your task is to calculate the number of subsets of nodes of the graph with the following properties: no nodes in the subset should be connected i

UVa 10397:Connect the Campus (最小生成树)

链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1338 题目: Problem E Connect the Campus Input: standard input Output: standard output Time Limit: 2 seconds Many new buildings are

UVa 10004:Bicoloring

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105 题目类型:搜索 题目: In 1976 the ``Four Color Map Theorem" was proven with the assistance of a computer. This theorem states that every map can be colored using on

UVa 10305:Ordering Tasks , 经典的拓扑排序

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=105&page=show_problem&problem=1246 题目类型: 拓扑排序 题目: John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is onl

UVa 208:Firetruck,双向搜索进行剪枝

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=144 类型: 回溯法 原题: The Center City fire department collaborates with the transportation department to maintain maps of the city

UVa 140:Bandwidth

题目链接: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=108&page=show_problem&problem=76 类型: 回溯 原题: Given a graph (V,E) where V is a set of nodes and E is a set of arcs in VxV, and an ordering on the elements in

UVa 1422:Processor 任务处理问题

题目大意:有n个任务送到处理器处理,每个任务信息包括r,d,w,r代表开始时间,w代表必须要结束的时间,w指需要多少时间处理. 其中处理器的处理速度可以变速,问处理器最小需要多大速度才能完成工作? 输入: 3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25