Frogs' Neighborhood
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 10545   Accepted: 4386   Special Judge

Description

未名湖附近共有N个大小湖泊L1L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N)。如果湖泊LiLj之间有水路相连,则青蛙FiFj互称为邻居。现在已知每只青蛙的邻居数目x1x2, ..., xn,请你给出每两个湖泊之间的相连关系。

Input

第一行是测试数据的组数T(0 ≤ T ≤ 20)。每组数据包括两行,第一行是整数N(2 < N < 10),第二行是N个整数,x1x2,..., xn(0 ≤ xi ≤ N)。

Output

对输入的每组测试数据,如果不存在可能的相连关系,输出"NO"。否则输出"YES",并用N×N的矩阵表示湖泊间的相邻关系,即如果湖泊i与湖泊j之间有水路相连,则第i行的第j个数字为1,否则为0。每两个数字之间输出一个空格。如果存在多种可能,只需给出一种符合条件的情形。相邻两组测试数据之间输出一个空行。

Sample Input

3
7
4 3 1 5 4 2 1
6
4 3 1 4 2 0
6
2 3 1 1 2 1

Sample Output

YES
0 1 0 1 1 0 1
1 0 0 1 1 0 0
0 0 0 1 0 0 0
1 1 1 0 1 1 0
1 1 0 1 0 1 0
0 0 0 1 1 0 0
1 0 0 0 0 0 0 NO YES
0 1 0 0 1 0
1 0 0 1 1 0
0 0 0 0 0 1
0 1 0 0 0 0
1 1 0 0 0 0
0 0 1 0 0 0 思路
http://www.cnblogs.com/wzjhoutai/p/6761606.html
这是一片不错的博客,详细介绍了Havel--Hakimi定理。
在执行删点的同时,把边连起来就行了.
代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int mp[][]; struct node
{
int id;
int num;
}a[]; bool cmp(node x,node y)
{
return x.num>y.num;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
memset(mp,,sizeof(mp));
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&a[i].num);
a[i].id=i+;
}
int tt=n;
int flag=;
for(int i=;i<n;i++){
sort(a,a+n,cmp);
if(a[].num<){flag=;break;}
int t=a[].num;a[].num=;
for(int j=;j<=t;j++){
a[j].num--;if(a[j].num<){flag=;break;}
mp[a[j].id][a[].id]=mp[a[].id][a[j].id]=;
}
if(flag){break;}
tt--;
}
if(flag==){printf("NO\n\n");continue;}
printf("YES\n");
for(int i=;i<=n;i++){
for(int j=;j<n;j++){
printf("%d ",mp[i][j]);
}
printf("%d\n",mp[i][n]);
}
printf("\n");
}
}
 

POJ 1659 Frogs' Neighborhood (Havel--Hakimi定理)的更多相关文章

  1. POJ1659 Frogs' Neighborhood(Havel–Hakimi定理)

    题意 题目链接 \(T\)组数据,给出\(n\)个点的度数,问是否可以构造出一个简单图 Sol Havel–Hakimi定理: 给定一串有限多个非负整数组成的序列,是否存在一个简单图使得其度数列恰为这 ...

  2. POJ 1659 Frogs' Neighborhood (Havel定理构造图)

    题意:根据图的度数列构造图 分析:该题可根据Havel定理来构造图.Havel定理对可图化的判定: 把序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d ...

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

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

  4. poj 1659 Frogs' Neighborhood (DFS)

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

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

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

  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 Havel-Hakimi定理 可简单图定理

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

随机推荐

  1. String 常见的十种方法!

    public class ZiFuChuan { public static void main(String[] args) { ZiFuChuanFangFa f=new ZiFuChuanFan ...

  2. mybatis-spring-1.2.2.jar下载地址

    http://www.java2s.com/Code/Jar/m/Downloadmybatisspring120jar.htm

  3. java 运行 .jar 文件乱码

    http://yang3wei.github.io/blog/2013/02/10/java-dfile-dot-encoding-equals-utf-8-gan-diao-luan-ma/ 启动时 ...

  4. Jenkins+PowerShell持续集成环境搭建(一)前期准备

    0. 系统要求 Windows Server:本文章使用的为Windows Server 2012 JDK:本文章使用为JDK 1.8 Windows PowerShell:本文章使用为PowerSh ...

  5. struts2 的struts.xml配置文件

    <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-/ ...

  6. UVA 690 Pipeline Scheduling

    https://vjudge.net/problem/UVA-690 题目 你有一台包含5个工作单元的计算机,还有10个完全相同的程序需要执行.每个程序需要$n(n<20)$个时间片来执行,可以 ...

  7. Nginx stream ssl

    L 115 端口监听 netstat -anp | (端口名)

  8. Luogu5280 ZJOI2019线段树(线段树)

    容易发现相当于求2m种操作序列所得的每种线段树tag数量之和.显然考虑每个点的贡献,也即有多少种方案会使该点上有tag.可以将点分为四类: 1.修改时被经过且有儿子被修改的节点 2.修改时被经过且没有 ...

  9. IDEA Failed to prepare an update: Temp directory inside installation

    具体错误: Connection Error Failed to prepare an update: Temp directory inside installation: F:\IDEA_Tool ...

  10. Django+Xadmin打造在线教育系统(五)

    课程相关功能实现 课程列表 创建课程相关的urls.py path("course/", include('course.urls', namespace="course ...