POJ1659Frogs' Neighborhood(lavel定理)
| Time Limit: 5000MS | Memory Limit: 10000K | |||
| Total Submissions: 7260 | Accepted: 3132 | 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
Source
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct node
{
int indx,ans;
}a[15];
int cmp(node a,node b)
{
return a.ans>b.ans;
}
int main()
{
int map[15][15],t,n,flag=0;
scanf("%d",&t);
while(t--)
{
if(flag)printf("\n");flag=1;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i].ans); a[i].indx=i;
}
int tn=n;
memset(map,0,sizeof(map));
while(n)
{
sort(a,a+n,cmp);
int i;
for(i=0;i<n;i++)
if(a[i].ans==0)
{
n=i; break;
}
i=1;
while(a[0].ans&&i<n&&a[i].ans)
{
a[0].ans--; a[i].ans--;
map[a[0].indx][a[i].indx]=map[a[i].indx][a[0].indx]=1;
i++;
}
if(a[0].ans>0)break;
}
if(n)
printf("NO\n");
else
{
printf("YES\n");
for(int i=0;i<tn;i++)
{
for(int j=0;j<tn;j++)
if(j==0) printf("%d",map[i][j]);
else printf(" %d",map[i][j]);
printf("\n");
}
}
}
}
POJ1659Frogs' Neighborhood(lavel定理)的更多相关文章
- POJ 1659 Frogs' Neighborhood(Havel-Hakimi定理)
题目链接: 传送门 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Description 未名湖附近共有N个大小湖泊L ...
- POJ 1659 Frogs' Neighborhood(度序列组成)
意甲冠军 中国 依据Havel-Hakimi定理构图即可咯 先把顶点按度数从大到小排序 可图的话 度数大的顶点与它后面的度数个顶点相连肯定是满足的 出现了-1就说明不可图了 #include ...
- poj 1659 Frogs' Neighborhood Havel-Hakimi定理 可简单图定理
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098136.html 给定一个非负整数序列$D=\{d_1,d_2,...d_n\}$,若存 ...
- poj 1659 Frogs' Neighborhood (度序列)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 7295 Accepted: 31 ...
- POJ 1659 Frogs' Neighborhood (Havel定理构造图)
题意:根据图的度数列构造图 分析:该题可根据Havel定理来构造图.Havel定理对可图化的判定: 把序列排成不增序,即d1>=d2>=……>=dn,则d可简单图化当且仅当d’={d ...
- poj 1659 Frogs' Neighborhood 度序列可图化 贪心
题意: 对一个无向图给出一个度序列,问他是否可简单图化. 分析: 依据Havel定理,直接贪心就可以. 代码: //poj 1659 //sep9 #include <iostream> ...
- poj 1286 polya定理
Necklace of Beads Description Beads of red, blue or green colors are connected together into a circu ...
- POJ 1659 Frogs' Neighborhood(可图性判定—Havel-Hakimi定理)【超详解】
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9897 Accepted: 41 ...
- POJ 1659 Frogs' Neighborhood (Havel--Hakimi定理)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10545 Accepted: 4 ...
随机推荐
- vue移动端上拉加载更多
LoadMore.vue <template> <div class="load-more-wrapper" @touchstart="touchSta ...
- Nodejs源代码分析之Path
今天介绍一下nodejs Path的源代码分析,Path的API文档在https://nodejs.org/dist/latest-v5.x/docs/api/path.html,使用相对简单,在AP ...
- OC文件操作、获取文件属性
#import <Foundation/Foundation.h> //获取文件的属性 int main(int argc, const char * argv[]) { @autorel ...
- JQuery源码解析 目录
1. a.$查询符号就是放在沙盒里面的: window.jQuery = window.$ = jQuery; b.一个jQuery对象无 new 构造: eg: $("#text" ...
- 汉字的ascii
- JS中的发布订阅模式
一. 你是如何理解发布订阅模式的 JS中的设计模式: 单例模式:处理业务逻辑 构造原型模式:封装类库,组件,框架,插件等 类库:jQuery 只是提供了一些常用的方法,可以应用到任何的项目中,不具备业 ...
- <meta name="viewport" content="width=device-width,initial-scale=1.0">
meta name="viewport" content="width=device-width,initial-scale=1.0" 解释 <meta ...
- 微服务实践(五):微服务的事件驱动数据管理 - DockOne.io
原文:微服务实践(五):微服务的事件驱动数据管理 - DockOne.io [编者的话]本文是使用微服务创建应用系列的第五篇文章.第一篇文章介绍了微服务架构模式,并且讨论了使用微服务的优缺点:第二和第 ...
- 原生js大总结一
001.浅谈堆和栈的理解? js变量存储有栈存储和堆存储,基本数据类型的变量存储在栈中,引用数据类型的变量存储在堆中 引用类型数据的地址也存在栈中 当访问基础类型变量时,直接从栈中取值.当访问 ...
- 【习题 3-7 UVA - 1368 】DNA Consensus String
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 枚举每一位字母是什么. 从小到大枚举. 然后计算每一位的总贡献是多少. 取最小的那个输出. [代码] #include <bi ...