题意:根据图的度数列构造图

分析:该题可根据Havel定理来构造图。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 <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn =;
int G[maxn][maxn];
struct Node{
int d,id;
bool operator <(const Node &rhs)const{return d>rhs.d;}
}p[maxn]; int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int T,N,M,u,v,tmp;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
memset(G,,sizeof(G));
for(int i=;i<=N;++i){
scanf("%d",&p[i].d);
p[i].id = i;
}
bool flag = true;
while(true){
sort(p+,p+N+);
if(!p[].d) break;
int u = p[].id;
for(int j=;p[].d &&j<=N;++j){
int v =p[j].id;
if(!p[j].d) continue;
G[u][v] = G[v][u] = ;
p[].d--;
p[j].d--;
}
if(p[].d>){
flag = false;
break;
}
}
if(!flag) printf("NO\n");
else{
printf("YES\n");
for(int i=;i<=N;++i){
for(int j=;j<=N;++j){
printf("%d ",G[i][j]);
}
printf("\n");
}
}
printf("\n");
}
return ;
}

POJ 1659 Frogs' Neighborhood (Havel定理构造图)的更多相关文章

  1. POJ 1659 Frogs' Neighborhood(Havel-Hakimi定理)

    题目链接: 传送门 Frogs' Neighborhood Time Limit: 5000MS     Memory Limit: 10000K Description 未名湖附近共有N个大小湖泊L ...

  2. poj 1659 Frogs' Neighborhood Havel-Hakimi定理 可简单图定理

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098136.html 给定一个非负整数序列$D=\{d_1,d_2,...d_n\}$,若存 ...

  3. poj 1659 Frogs' Neighborhood (DFS)

    http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total S ...

  4. POJ 1659 Frogs' Neighborhood(可图性判定—Havel-Hakimi定理)【超详解】

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9897   Accepted: 41 ...

  5. POJ 1659 Frogs' Neighborhood (Havel--Hakimi定理)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10545   Accepted: 4 ...

  6. poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6076   Accepted: 26 ...

  7. poj 1659 Frogs' Neighborhood( 青蛙的邻居)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9639   Accepted: 40 ...

  8. poj 1659 Frogs' Neighborhood(出入度、可图定理)

    题意:我们常根据无向边来计算每个节点的度,现在反过来了,已知每个节点的度,问是否可图,若可图,输出一种情况. 分析:这是一道定理题,只要知道可图定理,就是so easy了  可图定理:对每个节点的度从 ...

  9. Poj 1659.Frogs' Neighborhood 题解

    Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和 ...

随机推荐

  1. Windows 动态库创建和使用 part 2

    一.Windows动态库的创建: 1.先选择 "DLL" 和 “控项目” 2.添加一个头文件,一个源文件  CppDll.h,CppDll.cpp,一个模块定义文件 CppDll. ...

  2. Axure9 v9.0.0.3629 ~ v9.0.0.3633 授权密钥 【2019.02.05】

    现在提供一个支持v9.0.0.3629.v9.0.0.3630.v9.0.0.3631.v9.0.0.3632.v9.0.0.3633的授权码(后续的Beta更新版本应该能继续使用) 被授权人:zd4 ...

  3. 蓝桥杯 第四届C/C++预赛真题(5) 前缀判断(水题)

    题目标题:前缀判断 如下的代码判断 needle_start指向的串是否为haystack_start指向的串的前缀,如不是,则返回NULL. 比如:"abcd1234" 就包含了 ...

  4. Origin8.0使用心得(不定时更新)

    如何画双Y轴坐标? 网上的教程很多,有点鱼龙混杂的感觉,今天整理一下最简单的方法,备忘. 有3组数据,1组自变量,2组因变量.如何在同一张图下使用同一个X轴,但左边的Y轴与右边的Y轴的刻度不一样? 最 ...

  5. PAXOS: libevent_paxos

    PAXOS实现 -- libevent_paxos 该文章是项目的一部分.主要讲PAXOS算法的实现.                                                  ...

  6. c++ 类初始化

    一. 成员初始化列表的位置. 成员初始化列表的位置位于构造函数的函数体和参数表之间.构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟一个放在括号中的初始化式,初始化 ...

  7. iOS UITextView 输入内容实时更新cell的高度

    iOS UITextView 输入内容实时更新cell的高度 2014-12-26 11:37 编辑: suiling 分类:iOS开发 来源:Vito Zhang'blog  11 4741 UIT ...

  8. Caused by: com.mysql.cj.core.exceptions.InvalidConnectionAttributeException: The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. You must configure either the

    mysql6.0里面改成新的配置方式: hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect #old #driverClassNam ...

  9. [LintCode] 最后一个单词的长度

    class Solution { public: /** * @param s A string * @return the length of last word */ int lengthOfLa ...

  10. phantomjs学习之截图

    1.创建pageload.js文件: pageload.js var page = require('webpage').create(); var address = 'https://www.ba ...