Problem Description

Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep love and yearns for the boundless oceans. After graduation, he came to a coastal city and got a job in a marine transportation company. There, he held a position as a navigator in a freighter and began his new life.

The cargo vessel, Wang Haiyang worked on, sails among 6 ports between which exist 9 routes. At the first sight of his navigation chart, the 6 ports and 9 routes on it reminded him of Graph Theory that he studied in class at university. In the way that Leonhard Euler solved The Seven Bridges of Knoigsberg, Wang Haiyang regarded the navigation chart as a graph of Graph Theory. He considered the 6 ports as 6 nodes and 9 routes as 9 edges of the graph. The graph is illustrated as below.



According to Graph Theory, the number of edges related to a node is defined as Degree number of this node.

Wang Haiyang looked at the graph and thought, If arranged, the Degree numbers of all nodes of graph G can form such a sequence: 4, 4, 3,3,2,2, which is called the degree sequence of the graph. Of course, the degree sequence of any simple graph (according to Graph Theory, a graph without any parallel edge or ring is a simple graph) is a non-negative integer sequence?

Wang Haiyang is a thoughtful person and tends to think deeply over any scientific problem that grabs his interest. So as usual, he also gave this problem further thought, As we know, any a simple graph always corresponds with a non-negative integer sequence. But whether a non-negative integer sequence always corresponds with the degree sequence of a simple graph? That is, if given a non-negative integer sequence, are we sure that we can draw a simple graph according to it.?

Let's put forward such a definition: provided that a non-negative integer sequence is the degree sequence of a graph without any parallel edge or ring, that is, a simple graph, the sequence is draw-possible, otherwise, non-draw-possible. Now the problem faced with Wang Haiyang is how to test whether a non-negative integer sequence is draw-possible or not. Since Wang Haiyang hasn't studied Algorithm Design course, it is difficult for him to solve such a problem. Can you help him?

Input

The first line of input contains an integer T, indicates the number of test cases. In each case, there are n+1 numbers; first is an integer n (n<1000), which indicates there are n integers in the sequence; then follow n integers, which indicate the numbers of the degree sequence.

Output

For each case, the answer should be "yes"or "no" indicating this case is "draw-possible" or "non-draw-possible"

Sample Input

2
6 4 4 3 3 2 2
4 2 1 1 1

Sample Output

yes
no

Source

2008 Asia Regional Harbin


思路

根据Havel-Hakimi定理 :

  • 非递增排序当前数列\(d[n]\)
  • 让\(k=d[1]\),将k从数组中移除
  • 从第2个开始的前K个元素都-1
  • 不断重复上述过程直到序列出现负数=不可图全都为0=可图

详细见代码注释

代码

#include<bits/stdc++.h>
using namespace std;
int a[1001];
int main()
{
int t;
cin >> t;
while(t--)
{
int n;
cin >> n;
for(int i=1;i<=n;i++)
cin >> a[i];
sort(a+1,a+n+1,[](int x,int y)->bool{ return x>y;});
bool flag = false;
while(true)
{
int k = a[1];
if(k < 0 || a[n] < 0)//如果之前出现了负数那么排序后最后一个肯定小于0不符合
{
flag = false;
break;
}else
if(k==0)
{
flag = true;
break;
}else
{
for(int i=2;k>0;i++)//从第2个起接下来k个元素都-1
{
a[i]--;
if(a[i] < 0)
{
flag = false;
break;
}
k--;
}
a[1] = 0;
sort(a+1,a+n+1,[](int x,int y)->bool{ return x>y;});
}
if(flag) break;
}
if(flag)
cout << "yes" << endl;
else
cout << "no" << endl;
}
return 0;
}

Hdoj 2454.Degree Sequence of Graph G 题解的更多相关文章

  1. HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)

    主题链接:pid=2454">http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiya ...

  2. hdu 2454 Degree Sequence of Graph G (推断简单图)

    ///已知各点的度,推断是否为一个简单图 #include<stdio.h> #include<algorithm> #include<string.h> usin ...

  3. HDU 2454"Degree Sequence of Graph G"(度序列可图性判断)

    传送门 参考资料: [1]:图论-度序列可图性判断(Havel-Hakimi定理) •题意 给你 n 个非负整数列,判断这个序列是否为可简单图化的: •知识支持 握手定理:在任何无向图中,所有顶点的度 ...

  4. HDU 2454 Degree Sequence of Graph G——可简单图化&&Heavel定理

    题意 给你一个度序列,问能否构成一个简单图. 分析 对于可图化,只要满足度数之和是偶数,即满足握手定理. 对于可简单图化,就是Heavel定理了. Heavel定理:把度序列排成不增序,即 $deg[ ...

  5. hdu 2454 Degree Sequence of Graph G(可简单图化判定)

    传送门 •Havel-Hakimi定理: 给定一个非负整数序列{d1,d2,...dn},若存在一个无向图使得图中各点的度与此序列一一对应,则称此序列可图化. 进一步,若图为简单图,则称此序列可简单图 ...

  6. 【Havel 定理】Degree Sequence of Graph G

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=2454 [别人博客粘贴过来的] 博客地址:https://www.cnblogs.com/debug ...

  7. POJ 2553 The Bottom of Graph 强连通图题解

    Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...

  8. Rabin_Karp(hash) HDOJ 1711 Number Sequence

    题目传送门 /* Rabin_Karp:虽说用KMP更好,但是RK算法好理解.简单说一下RK算法的原理:首先把模式串的哈希值算出来, 在文本串里不断更新模式串的长度的哈希值,若相等,则找到了,否则整个 ...

  9. 洛谷P3104 Counting Friends G 题解

    题目 [USACO14MAR]Counting Friends G 题解 这道题我们可以将 \((n+1)\) 个边依次去掉,然后分别判断去掉后是否能满足.注意到一点, \(n\) 个奶牛的朋友之和必 ...

随机推荐

  1. Two distinct points CodeForces - 1108A (签到)

    You are given two segments [l1;r1][l1;r1] and [l2;r2][l2;r2] on the xx-axis. It is guaranteed that l ...

  2. Day5 Pyhton基础之编码与解码(四)

    1.编码与解码 1.1现在常用的编码类型

  3. 泛函p121可分Hilbert空间都同构于l^2

    如何理解最后面两句话, L^2与l^2同构 L^2里面 有理系数多项式 是可数稠密子集 所以L^2可分 可分Hilbert空间都同构于 l^2 傅里叶级数是一个稠密的子集

  4. [2017BUAA软工助教]团队alpha得分总表

    一.累计得分 项目 介绍 采访 贡献分 功能 技术 α例会 α发布 α测试 α展示 α事后 合计 满分 10 10 10 10 10 50 10 10 150 10 280 hotcode5 10 9 ...

  5. 05 Hadoop 设置块的大小

    1.是在hdfs的配置文件中配置 2.是在app程序中设置 注意:假设配置文件的最大是   20K   最小是 10K   文件大小为72  块数就是 4 在程序中设置最大为15K    切割块数  ...

  6. Java 异常处理的误区和经验总结

    Java 异常处理的误区和经验总结   1 本文着重介绍了 Java 异常选择和使用中的一些误区,希望各位读者能够熟练掌握异常处理的一些注意点和原则,注意总结和归纳.只有处理好了异常,才能提升开发人员 ...

  7. Linux 下面 PG 的 uuid-ossp 包安装办法

    1. pgsql 安装 时报错, 如图示: 详细信息为: 执行SQL为: CREATE EXTENSION IF NOT EXISTS "uuid-ossp" 错误纤细信息为: C ...

  8. Linux安装mysql5.6

    安装mysql5.6https://www.cnblogs.com/wangdaijun/p/6132632.html

  9. Codeforces 1154G Minimum Possible LCM

    题目链接:http://codeforces.com/problemset/problem/1154/G 题目大意: 给定n个数,在这些数中选2个数,使这两个数的最小公倍数最小,输出这两个数的下标(如 ...

  10. Jmeter安装与使用(压测)

    一.介绍 Apache JMeter是100%纯JAVA桌面应用程序,被设计为用于测试客户端/服务端结构的软件(例如web应用程序).它可以用来测试静态和动态资源的性能,例如:静态文件,Java Se ...