hdu 2454 Degree Sequence of Graph G

点击打开链接

Degree Sequence of Graph G

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1997    Accepted Submission(s): 850

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

 

Recommend

gaojie   |   We have carefully selected several similar problems for you:  2448 2452 2451 2453 2455 

 

题目大意:

给你 n 个读书序列,让你判断是否能够组成简单图。。。

解题思路:
Havel定理:

序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d2-1,d3-1,……d(d1+1)-1,
d(d1+2),d(d1+3),……dn}可简单图化。简单的说,把d排序后,找出度最大的点(设度为d1),把它与度次大的d1个点之间连边,然后这个点就可以不管了,一直继续这个过程,直到建出完整的图,或出现负度等明显不合理的情况。

然而首先判断的是度数之和必须是偶数,(度数之和是边数的二倍)。。。

上代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
using namespace std;

#define MM(a) memset(a,0,sizeof(a))

typedef long long LL;
typedef unsigned long long ULL;
const int maxn = 1e5+5;
const int mod = 1e9+7;
const double eps = 1e-8;
const int INF = 0x3f3f3f3f;
LL gcd(LL a, LL b)
{
    if(b == 0)
        return a;
    return gcd(b, a%b);
}
bool cmp(int a, int b)
{
    return a > b;
}
int arr[maxn];
int main()
{
    int T, m;
    cin>>T;
    while(T--)
    {
        cin>>m;
        int sum = 0;
        for(int i=0; i<m; i++)
        {
            cin>>arr[i];
            sum += arr[i];
        }
        if(sum % 2)
            puts("no");
        else
        {
            bool yes, ok;
            yes = ok = false;
            for(int i=0; i<m; i++)
            {
                sort(arr, arr+m, cmp);
                if(arr[0] == 0)
                {
                    yes = true;
                    break;
                }
                for(int j=1; j<=arr[0]; j++)
                {
                    arr[j]--;
                    if(arr[j] < 0)
                    {
                        ok = true;
                        break;
                    }
                }
                if(ok)
                    break;
                arr[0] = 0;
            }
            if(ok)
                puts("no");
            if(yes)
                puts("yes");
        }
    }
    return 0;
}
时间: 2024-08-23 12:14:06

hdu 2454 Degree Sequence of Graph G的相关文章

hdu 5400 Arithmetic Sequence

click here~~ ***Arithmetic Sequence*** Problem Description A sequence b1,b2,⋯,bn are called (d1,d2)-arithmetic sequence if and only if there exist i(1≤i≤n) such that for every j(1≤j<i),bj+1=bj+d1 and for every j(i≤j<n),bj+1=bj+d2. Teacher Mai has a

hdu 2062 Subset sequence【有点康拓展开的意思】

Subset sequence Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3441    Accepted Submission(s): 1740 Problem Description Consider the aggregate An= { 1, 2, -, n }. For example, A1={1}, A3={1,2,

hdu 1711 Number Sequence

点击打开链接hdu1711 思路:KMP kmp算法: 1 kmp是用来匹配字符串,只能够匹配单一的字符串 2 kmp的算法的过程:   1:假设文本串的长度为n,模式串的长度为m:   2:先例用O(m)的时间去预处理next数组,next数组的意思指的是当前的字符串匹配失败后要转到的下一个状态:   3:利用o(n)的时间去完成匹配: 3 时间复杂度为o(n+m)即o(n): 代码: #include<algorithm> #include<iostream> #include

HDU2454 图的基本性质

                Degree Sequence of Graph G                                             Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)                                                                 Total Submission(s

算法:poj 2749 &amp;amp; hdu 1815 Building roads(2-SAT + 二分,好题)

[题目大意] 有n个牛棚, 还有两个中转站S1和S2, S1和S2用一条路连接起来. 为了使得任意 牛棚两个都可以有道路联通,现在要让每个牛棚都连接一条路到S1或者S2. 有a对牛棚互相有仇恨, 所以不能让他们的路连接到同一个中转站.还有b对牛棚互相喜欢,所以他们的路必须连到同一个中专站. 道路的长度是两点的曼哈顿距离. 问最小的任意两牛棚间的距离中的最大值是多少? [思路] 两天前看了这题,当时没什么想法,今天又翻看了一下,想出来了. 每个 牛棚有可以选择S1或者S2,并且有矛盾对,是2-SA

算法:HDU 4115 Eliminate the Conflict (2-SAT判断)

[题目大意] Bob和Alice玩剪刀石头布,一个玩n轮,Alice已经知道了Bob每次要出什么,1代表 剪刀,2代表石头,3代表布,然后Bob对Alice作出了一些限制: 给m行,每行是a b k,如果k是0,表 示Alice第a次和b次出的拳必须相同,如果k是1,表示Alice第a次和b次出的拳必须不相同. 一但 Alice破坏了这个限制规则,或者输了一局,那么Alice就彻底输了. 问Alice可不可能赢? [ 分析] 因为Alice一次都不能输,所以根据Bob出的拳,Alice只可以赢或

算法:HDU 3715 Go Deeper(2-SAT + 二分)

[题目大意] 有一个递归代码: go(int dep, int n, int m) begin     output the value of dep. if dep < m and x[a[dep]] + x[b[dep]] != c[dep] then go(dep + 1, n, m) end 关键是看第四行, 如果满足条件dep < m and x[a [dep]] + x[b[dep]] != c[dep] 那么就可以进入下一层递归, x数组只取{0, 1}, c数组取{ 0,1,2

算法:HDU 3062 Party (2-SAT入门学习)

Problem Description 有n对夫妻被邀请参加一个聚会,因为场地的问题,每对夫妻中只有1人可以 列席.在2n 个人中,某些人之间有着很大的矛盾(当然夫妻之间是没有矛盾的),有矛盾的2个人是不会同 时出现在聚会上的.有没有可能会有n 个人同时列席? Input n: 表示有n对夫妻被邀请 (n<= 1000) m: 表示有m 对矛盾关系 ( m < (n - 1) * (n -1)) 在接下来的m行中,每行会有4个数字,分别是 A1,A2,C1,C2 A1,A2分别表示是夫妻的编号

【LeetCode从零单排】No133. clon graph (BFS广度优先搜索)

背景 (以下背景资料转载自:http://www.cnblogs.com/springfor/p/3874591.html?utm_source=tuicool) DFS(Dpeth-first Search)顾名思义,就是深度搜索,一条路走到黑,再选新的路.记得上Algorithm的时候,教授举得例子就是说,DFS很像好奇的小孩,你给这个小孩几个盒子套盒子,好奇的小孩肯定会一个盒子打开后继续再在这个盒子里面搜索.等把这一套盒子都打开完,再打开第二套的.Wikipedia上的讲解是:"Depth