UVA 10720 Graph Construction 贪心+优先队列
题目链接:
题目
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 贪心+优先队列的更多相关文章
- UVa 10720 - Graph Construction(Havel-Hakimi定理)
题目链接: 传送门 Graph Construction Time Limit: 3000MS Memory Limit: 65536K Description Graph is a coll ...
- UVa 10720 - Graph Construction
题目大意:给n个整数, 分别代表图中n个顶点的度,判断是否能构成一张图. 看到这个题后,除了所有数之和应该为偶数之外,没有别的想法了,只好在网上搜解题报告了.然后了解了Havel-Hakimi定理.之 ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- 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 ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- uva 1615 高速公路(贪心,区间问题)
uva 1615 高速公路(贪心,区间问题) 给定平面上n个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个点,都有一个选出的点离它的欧几里得距离不超过D.(n<=1e5) 对于每个 ...
- 贪心+优先队列 HDOJ 5360 Hiking
题目传送门 /* 题意:求邀请顺序使得去爬山的人最多,每个人有去的条件 贪心+优先队列:首先按照l和r从小到大排序,每一次将当前人数相同的被邀请者入队,那么只要能当前人数比最多人数条件小,该人能 被邀 ...
- [POJ1456]Supermarket(贪心 + 优先队列 || 并查集)
传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 #include <queue> #include <cstdio> #include <i ...
随机推荐
- 十、Notepad++正则表达式使用
推荐个正则表达式在线测试的工具http://ccmpp.com/Regex/ Notepad++正则表达式使用 2011-01-06 10:01:35| 分类: 文档 | 标签:正则表达式 替换 no ...
- 对索引像素格式的图片进行Setpixel(具有索引像素格式的图像不支持SetPixel)解决方案
最近编写了一个验证码识别软件.其中对png.jpg图片进行二值化处理时,出现了错误:具有索引像素格式的图像不支持SetPixel解决方案.从字面上来看,这说明我对一个具有索引色的图片进行了直接RGB颜 ...
- 20150309—bs的保存状态
http:保存状态方式,传值方式 session:(会话) 默认过期时间20分钟(20分内无任何操作自动销毁),针对用户独立,一般用来存储少量信息的 存值:session[“name”]=data;( ...
- 20140527-ASP.NET中尖括号百分号用法
1.<%=%> 里面放的变量名,如:<div><h1>Hello World</h1><p>Welcome to Beginning ASP ...
- (转)MongoDB 实现currentOp定时捕获
问题描述: 当分析生产环境发生的性能问题时,常常因为没有实时的依据而无从下手.那么笔者通过捕获db.currentOp()到文件,并作为定时任务,可供后续分析. 解决方法: 步骤一:Shell脚本记录 ...
- (转)一网打尽当下NoSQL类型、适用场景及使用公司
摘要:对比传统关系型数据库,NoSQL有着更为复杂的分类——键值.面向文档.列存储以及图数据库.这里就带你一览NoSQL各种类型的适用场景及一些知名公司的方案选择. 在过去几年,关系型数据库一直是数据 ...
- 反射-Reflect
1.得到一份Class(同一个类在JVM中只有一份字节码) 三种方式:类名.class, Class.forName("全限定名");, 对象.getClass(); 基本类型 i ...
- HDU 5093 Battle ships(二分图最大匹配)
题意:一个m行n列的图由#.*.o三种符号组成,分别代表冰山.海域.浮冰,问最多可放的炮舰数(要求满足以下条件) 1.炮舰只可放在海域处 2.两个炮舰不能放在同一行或同一列(除非中间隔着一个或多个冰山 ...
- copy con
在DOS系统中,一些计算机设备也有文件名,叫做设备文件,可以用对应的文件名来操作它.后来的WINDOWS也保留了这些设备文件.比如con表示console(控制台,简单的说就是键盘和屏幕),com ...
- iOS ARC下循环引用的问题 -举例说明strong和weak的区别
strong:适用于OC对象,作用和非ARC中的retain作用相同,它修饰的成员变量为强指针类型weak:适用于OC对象,作用和非ARC中的assign作用相同,修饰的成员变量为弱指针类型assig ...