题目链接: 传送门

Graph Construction

Time Limit: 3000MS     Memory Limit: 65536K

Description

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
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

bool cmp(int x,int y)
{
    return x>y;
}

int main()
{
    int N;
    while (~scanf("%d",&N) && N)
    {
        int ans[10005] = {0};
        bool flag = true;
        for (int i = 0;i < N;i++)
        {
            scanf("%d",&ans[i]);
        }
        while (flag)
        {
            sort(ans,ans+N,cmp);
            int tmp = ans[0];
            if (tmp == 0)
            {
                break;
            }
            for (int i = 1;i <= tmp;i++)
            {
                ans[i]--;
                if (ans[i] < 0)
                {
                    flag = false;
                    break;
                }
            }
            ans[0] = 0;
            if (!flag)
            {
                break;
            }
        }
        if (!flag)
        {
            printf("Not possible\n");
        }
        else
        {
            printf("Possible\n");
        }
    }
    return 0;
}

UVa 10720 - Graph Construction(Havel-Hakimi定理)的更多相关文章

  1. UVA 10720 Graph Construction 贪心+优先队列

    题目链接: 题目 Graph Construction Time limit: 3.000 seconds 问题描述 Graph is a collection of edges E and vert ...

  2. UVa 10720 - Graph Construction

    题目大意:给n个整数, 分别代表图中n个顶点的度,判断是否能构成一张图. 看到这个题后,除了所有数之和应该为偶数之外,没有别的想法了,只好在网上搜解题报告了.然后了解了Havel-Hakimi定理.之 ...

  3. POJ1659 Frogs' Neighborhood(Havel–Hakimi定理)

    题意 题目链接 \(T\)组数据,给出\(n\)个点的度数,问是否可以构造出一个简单图 Sol Havel–Hakimi定理: 给定一串有限多个非负整数组成的序列,是否存在一个简单图使得其度数列恰为这 ...

  4. UVA10720 Graph Construction 度序列可图性

    Luogu传送门(UVA常年上不去) 题意:求一个度序列是否可变换为一个简单图.$\text{序列长度} \leq 10000$ 题目看起来很简单,但是还是有一些小细节需要注意首先一个简单的结论:一张 ...

  5. uva 193 Graph Coloring(图染色 dfs回溯)

    Description You are to write a program that tries to find an optimal coloring for a given graph. Col ...

  6. UVa 1515 - Pool construction(最小割)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. uva 10733 The Colored Cubes<polya定理>

    链接:http://uva.onlinejudge.org/external/107/10733.pdf 题意: N 种颜色可以涂成多少种立方体~ 思路: 使正六面体保持不变的运动群总共有: 1.不变 ...

  8. UVA 1515 Pool construction 最大流跑最小割

    Pool construction You are working for the International Company for Pool Construction, a constructio ...

  9. UVA 11609 - Anne's game cayley定理

    Lily: “Chantarelle was part of my exotic phase.”Buffy: “It’s nice. It’s a mushroom.”Lily: “It is? Tha ...

随机推荐

  1. java并发:线程同步机制之计数器&Exechanger

    第一节 CountDownLatch (1)初识CountDownLatch (2)详述CountDownLatch CountDownLatch是通过一个计数器来实现的,计数器的初始值为线程的数量. ...

  2. java并发:同步容器&并发容器

    第一节 同步容器.并发容器 1.简述同步容器与并发容器 在Java并发编程中,经常听到同步容器.并发容器之说,那什么是同步容器与并发容器呢?同步容器可以简单地理解为通过synchronized来实现同 ...

  3. Jquery 页面首次加载方式

    $(document).ready(function(){ alert("111"); }); $(function(){ alert("222"); }); ...

  4. jQuery Ajax 处理 HttpStatus

    今天同事碰到一个问题:当服务端Session失效后用ajax请求数据,页面端无法提示和执行跳转.我最先想到是,在后端用js输出一个跳转.发现输出没有效果,因为ajax是异步请求, 需要在success ...

  5. Javascript将构造函数扩展为简单工厂

    一般而言,在Javascript中创建对象时需要使用关键字new(按构造函数去调用),但是某些时候,开发者希望无论new关键字有没有被显式使用,构造函数都可以被正常调用,即构造函数同时还具备简单工厂的 ...

  6. VC维含义

    VC维含义的个人理解 有关于VC维可以在很多机器学习的理论中见到,它是一个重要的概念.在读<神经网络原理>的时候对一个实例不是很明白,通过这段时间观看斯坦福的机器学习公开课及相关补充材料, ...

  7. java中的@Override标签,小细节大作用

    转载:http://www.cnblogs.com/octobershiner/archive/2012/03/09/2388370.html 一般用途                         ...

  8. 【Alpha版本】冲刺阶段——Day 7

    我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...

  9. Maven遇到的错误汇总

    使用工具是MyEclipse10: 1.创建Maven项目出错 1.项目名带有Maven Webapp 解决方案: 2.缺少jar could not resolve archetype :   Co ...

  10. impdp导入时卡死分析方法

    来源于: http://blog.csdn.net/yfleng2002/article/details/7973997 http://www.cnblogs.com/songling/archive ...