POJ 1659 Frogs' Neighborhood (Havel--Hakimi定理)
| Time Limit: 5000MS | Memory Limit: 10000K | |||
| Total Submissions: 10545 | Accepted: 4386 | Special Judge | ||
Description
未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N)。如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居。现在已知每只青蛙的邻居数目x1, x2, ..., xn,请你给出每两个湖泊之间的相连关系。
Input
第一行是测试数据的组数T(0 ≤ T ≤ 20)。每组数据包括两行,第一行是整数N(2 < N < 10),第二行是N个整数,x1, x2,..., 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定理)的更多相关文章
- POJ1659 Frogs' Neighborhood(Havel–Hakimi定理)
题意 题目链接 \(T\)组数据,给出\(n\)个点的度数,问是否可以构造出一个简单图 Sol Havel–Hakimi定理: 给定一串有限多个非负整数组成的序列,是否存在一个简单图使得其度数列恰为这 ...
- POJ 1659 Frogs' Neighborhood (Havel定理构造图)
题意:根据图的度数列构造图 分析:该题可根据Havel定理来构造图.Havel定理对可图化的判定: 把序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d ...
- POJ 1659 Frogs' Neighborhood(可图性判定—Havel-Hakimi定理)【超详解】
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9897 Accepted: 41 ...
- poj 1659 Frogs' Neighborhood (DFS)
http://poj.org/problem?id=1659 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total S ...
- POJ 1659 Frogs' Neighborhood(Havel-Hakimi定理)
题目链接: 传送门 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Description 未名湖附近共有N个大小湖泊L ...
- poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 6076 Accepted: 26 ...
- poj 1659 Frogs' Neighborhood( 青蛙的邻居)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9639 Accepted: 40 ...
- poj 1659 Frogs' Neighborhood(出入度、可图定理)
题意:我们常根据无向边来计算每个节点的度,现在反过来了,已知每个节点的度,问是否可图,若可图,输出一种情况. 分析:这是一道定理题,只要知道可图定理,就是so easy了 可图定理:对每个节点的度从 ...
- poj 1659 Frogs' Neighborhood Havel-Hakimi定理 可简单图定理
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098136.html 给定一个非负整数序列$D=\{d_1,d_2,...d_n\}$,若存 ...
随机推荐
- lr分布式测试--笔记
LR 架构: controller做测试控制 scenario做测试场景的控制 vuser模拟用户和 load generator做负载产生 通过MI listener 跨防火墙监听 来实现分布式测试 ...
- easyui datagrid动态修改editor时动态绑定combobox的数据
需求在 datagrid 编辑框中开启一个combobox ,但是里面的数据需要开启的时候才会知道,数据会根据其他因数变更 参考原文 :http://blog.csdn.net/donggua369 ...
- 如何设置C-Lodop打印控件的端口
Lodop是一款功能强大的打印控件,在一些浏览器不再支持np插件之后,Lodop公司又推出了C-Lodop,C-Lodop是以服务的方式解决web打印,摆脱了对浏览器的依赖,支持了所有的浏览器. 该控 ...
- Oracle 查询字段不包含多个字符串方法
开发过程中遇到个需求,用户要提取的数据列中不包含 YF.ZF.JD的字符串, 方法1:select * from table where order_no not like '%YF%' and ...
- Add Languages to Your Xamarin Apps with Multilingual App Toolkit
With Xamarin, you can extend your cross-platform apps with support for native speakers, reaching mar ...
- Tuple Class
Inheritance Hierarchy System.Object System.Tuple Methods Name Description Create<T1>(T1) ...
- A Simple Problem with Integers(线段树区间更新模板)
最基本的线段树的区间更新及查询和 用tag(lazy)数组来“延缓”更新,查询或添加操作必须进行pushdown操作,即把tag从p传到lp和rp并清楚tag[p],既然得往lp和rp递归,那么就可以 ...
- vuex2.0 基本使用(2) --- mutation 和 action
我们的项目非常简单,当点击+1按钮的时候,count 加1,点击-1按钮的时候,count 减1. 1, mutation The only way to actually change state ...
- codeforces611C
New Year and Domino CodeForces - 611C 他们说:“每一年都像多米诺骨牌,一个接一个地倒下去”.但是,一年能够像多米诺骨牌那样放在网格中吗?我不这么认为. Zydsg ...
- python史上最全学习路线图
ps:盘它 python入门教程 关注微信公众号,回复"python入门"获取视频下载地址