题目链接:

题目

Graph Construction

Time limit: 3.000 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 n integers, you have to find out whether this n numbers can represent the degrees of n vertices

of a graph.

输入

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.

输出

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.

样例

input

4 3 3 3 3

6 2 4 5 5 2 1

5 3 2 3 2 1

0

output

Possible

Not possible

Not possible

题意

给你每个顶点的度数,问能不能组成无环无平行边的无向图。

题解

贪心做:

从最大度数的那个点(假设度数是x)做开始,在剩下的数中选出度最大的前x个数,如果有哪个节点的度数为0则不可能构成图,否则把这些节点度数减1继续做。 直到所有的点的度数都为0。

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std; int main(){
int n;
while(scanf("%d",&n)==1&&n){
priority_queue<int> pq;
int x;
for(int i=0;i<n;i++){
scanf("%d",&x);
pq.push(x);
}
bool su=1;
while(!pq.empty()){
int u=pq.top(); pq.pop();
if(u==0) break;
vector<int> pool;
for(int i=0;i<u;i++){
if(pq.empty()){
su=0; break;
}
int v=pq.top(); pq.pop();
if(v==0){
su=0; break;
}
v--;
pool.push_back(v);
}
if(!su) break;
for(int i=0;i<pool.size();i++){
pq.push(pool[i]);
}
}
if(su) puts("Possible");
else puts("Not possible");
}
return 0;
}

UVA 10720 Graph Construction 贪心+优先队列的更多相关文章

  1. UVa 10720 - Graph Construction(Havel-Hakimi定理)

    题目链接: 传送门 Graph Construction Time Limit: 3000MS     Memory Limit: 65536K Description Graph is a coll ...

  2. UVa 10720 - Graph Construction

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

  3. UVA 11134 - Fabled Rooks(贪心+优先队列)

    We would like to place  n  rooks, 1 ≤  n  ≤ 5000, on a  n×n  board subject to the following restrict ...

  4. hihoCoder 1309:任务分配 贪心 优先队列

    #1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN,  ...

  5. C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列

    C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...

  6. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  7. uva 1615 高速公路(贪心,区间问题)

    uva 1615 高速公路(贪心,区间问题) 给定平面上n个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D.(n<=1e5) 对于每个 ...

  8. 贪心+优先队列 HDOJ 5360 Hiking

    题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...

  9. [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

    传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...

随机推荐

  1. WScript.SendKeys()的sendkeys发送组合键以及特殊字符

    SendKeys.Send("^+{TAB}"); 使用SendKeys将键击和组合键击发送到活动应用程序.此类无法实例化.若要发送一个键击给某个类并立即继续程序流,请使用Send ...

  2. UIView总结---对UIView头文件中的大部分信息进行中文注释

    @interface UIView : UIResponder<NSCoding, UIAppearance, UIAppearanceContainer, UIDynamicItem> ...

  3. js正则表达式的验证示例

    //验证邮箱的JS正则 <script type="text/javascript"> $(function() { $("#inputemail" ...

  4. WCF之事务

    2阶段提交协议. 事务先提交给协调者,由协调者分发给各个RM,在一段规定的时间后.由RM询问各个RM是否提交还是终止操作.RM根据自己的状态来决定提交/终止.协调者根据RM的结果,决定操作的提交/终止 ...

  5. GDAL读取tiff文件/C++源码

    // gdal_geotiff.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include "gdal_priv.h&quo ...

  6. 分布式MySQL 数据库

    http://zhangxugg-163-com.iteye.com/blog/1666673 而本文所描述的 federated属于 MySQL的一种特殊引擎,利用它可将本地数据表映射至远程 MyS ...

  7. bzoj 1132 POI2008 Tro

    大水题=_=,可我想复杂了…… 很裸的暴力,就是加了个小优化…… 叉积求面积 :abs(xi*yj - yi*xj) 所以去掉绝对值,把 xi 和 xj 提出来就可以求和了 去绝对值加个极角排序,每次 ...

  8. ABAP OO与ALV结合方式探索(1)

    用OO来开发,尤其是在复杂业务的开发过程中 从程序设计的角度而言,应该更简单一点 而ALV是二次开发中登场很高的一个控件 最近做了一些尝试,探索OO的代码和ALV的结合使用   使用控件型的ALV A ...

  9. [windows phone开发]新生助手的开发过程与体会二

    上一讲咱们谈了新生助手主页的基本的设计,今天我们谈一谈关于展现实景地图时等动画的设计,即Storyboard的应用. 在Windows phone中,Storyboard类表示通过时间线控制动画,并为 ...

  10. 《自动共享LDAP用户并且访问其家目录》RHEL6

    实验的目的: 实现ldap服务器上的ldap用户被客户端访问,自动挂载到客户端,并且可以访问ldap用户的家目录. 服务端: 1.只需要配置文件: Iptables –F       关闭selinu ...