Havel–Hakimi algorithm(判断度数序列是否可图)

问题 J: Degree Sequence of Graph G

时间限制: 1 Sec  内存限制: 128 MB

题目描述

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 Königsberg, 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?

输入

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.

输出

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

样例输入

复制样例数据

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

样例输出

yes
no


每次对点sort,然后对该点ai剔除,并且此后ai个点减一;举例如下:

3  2 3 1 2 1

1.由大到小排序 :

3 3 2 2 1 1

2.剔除第一个;

/*3 */      3 2 2 1 1

3. 后面3个都减去1

/*3*/   2 1 1 0 1

4.再次排序

/*3*/   2 1 1 1 0

5.再剔除第一个

/*3 2*/  1 1 1 0

6. 后面2个每个减一

/*3 2*/  0 0 1 0

。。。。。最后直到全部是0就是可以成图,否则有负数,就不能成图。

int H(){
    for(int i=0;i<n-1; i++)
    {
        sort(a+i,a+n,greater<int>());

        if(a[i]+i>(n-1)) return 0;

        for(int j=i+1; j<=i+a[i] ; j++){
            --a[j];
            if(a[j]<0) return 0;
        }
    }
    if(a[n-1]!=0) return 0;
    return 1;
}

还有一种方法 是公式法:

题目AC代码如下(有点慢)

#include<bits/stdc++.h>
using namespace std;
const int N=1e6+50;
const int mod=998244353;
typedef long long ll;
int a[N];
int qian[N],hou[N];
int n;
int H(){
    for(int i=0;i<n-1; i++)
    {
        sort(a+i,a+n,greater<int>());

        if(a[i]+i>(n-1)) return 0;

        for(int j=i+1; j<=i+a[i] ; j++){
            --a[j];
            if(a[j]<0) return 0;
        }
    }
    if(a[n-1]!=0) return 0;
    return 1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d",&a[i]);
        }
        if(H()) printf("yes\n");
        else printf("no\n");
    }

    return 0;
}

原文地址:https://www.cnblogs.com/sylvia1111/p/11317678.html

时间: 2024-08-27 21:41:42

Havel–Hakimi algorithm(判断度数序列是否可图)的相关文章

关于Havel算法判断度数序列能否构成简单图的思考

问题描述: Given a list of n natural numbers d1, d2,...,dn, show how to decide in polynomial time whether there exists an undirected graph G = (V, E) whose node degrees are precisely the numbers d1,d2,···,dn. G should not contain multiple edges between th

poj 1659 Frogs&#39; Neighborhood (Havel-Hakimi定理,判断序列是否可图)

链接:poj 1659 中文题不必解释题意... 其实质是给定一个度序列,判断是否可图, 若可图,输出YES,并输出各顶点之间的连边的情况 否则,输出NO 思路:判断一个序列是否可图,直接利用Havel-Hakimi定理即可 判断任意一个序列是否可图的具体过程: (1)先将序列由大到小排序 (2)设最大的度数为 t ,将最大项删除,然后把最大度数后 (不包括自己)的 t 个度数分别减1(意思就是把度数最大的点与后几个点连边) (3)重复上述两步,如果最大度数t超过了剩下顶点的个数, 或者序列中出

【编程题目】判断整数序列是不是二元查找树的后序遍历结果,如果是,构建该二元查找树

判断整数序列是不是二元查找树的后序遍历结果题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回 true,否则返回 false.例如输入 5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果:8/ \6 10/ \ / \5 7 9 11因此返回 true.如果输入 7.4.6.5,没有哪棵树的后序遍历的结果是这个序列,因此返回 false. 做这个题目的时候最开始傻了,想着从前到后根据数字的大小关系判断.后来幡然醒悟,根据后序遍历的特点.序列最后一

华为机试题 二叉查搜索树 判断两序列是否为同一二叉搜索树序列

描述: 判断两序列是否为同一二叉搜索树序列 输入 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树. 接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树. 输出: 如果序列相同则输出YES,否则输出NO 样例输入: 2 567432 543267 576342 0 样例输出: YES NO 本题思路:根

微软算法100题09 判断整数序列是不是二元查找树的后序遍历结果

9. 判断整数序列是不是二元查找树的后序遍历结果题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回true,否则返回false.例如输入5.7.6.9.11.10.8,由于这一整数序列是如下树的后序遍历结果:8/ \6 10/ \ / \5 7 9 11因此返回true.如果输入7.4.6.5,没有哪棵树的后序遍历的结果是这个序列,因此返回false. 思路:如果一个数组为BST的后序遍历结果 则最后一个元素必然为该BST的根节点 因为BST的特性是左子树必然全部小

数据结构与算法问题 判断两序列是否为同一二叉搜索树序列

题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树. 接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树. 输出: 如果序列相同则输出YES,否则输出NO 样例输入: 2 567432 543267 576342 0 样例输出: YES NO 来源:

9判断整数序列是不是二元查找树的后序遍历结果

转载请注明出处:http://www.cnblogs.com/wuzetiandaren/p/4252095.html 声明:现大部分文章为寻找问题时在网上相互转载,此博是为自己做个记录记录,方便自己也方便有类似问题的朋友,本文的思想也许有所借鉴,但源码均为本人实现,如有侵权,请发邮件表明文章和原出处地址,我一定在文章中注明.谢谢. 题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果.如果是返回true,否则返回false. 解题思路: 1.输入一个整型数组a,根据该数组创建二

hdu 4885 (n^2*log(n)判断三点共线建图)+最短路

题意:车从起点出发,每次只能行驶L长度,必需加油到满,每次只能去加油站或目的地方向,路过加油站就必需进去加油,问最小要路过几次加油站. 开始时候直接建图,在范围内就有边1.跑最短了,再读题后发现,若几个点共线,且都在范围内,那么中间有点的俩头的点就不能有边,否则与条件相悖.关键是怎么用n^2*logn,的复杂度判断三点共线:点先按X排序,考察每个点i时候,第二个点j,若直线ij斜率已经存在,则不能添加了,查找是否存在,用容器就行(map\set)都是logn的,所以满足要求.之后最短路即可. #

poj 2762 Going from u to v or from v to u? (判断是否是弱联通图)

题意:给定一个有向图有m条单向边,判断是否任意两点都可达(a能到b或者b能到a或者互相可达),即求 弱联通分量. 算法: 先缩点求强连通分量.然后重新建图,判断新图是否是一条单链,即不能分叉,如果分叉了就会存在不可达的情况. 怎么判断是否是单链呢? 就是每次入度为0的点都只有一个,即每次队列里只有一个点. (    o(╯□╰)o.....好像已经是第二次用pair记录原图的点对,然后存pair的vector忘记清空导致wa来wa去! ) #include<cstdio> #include&l