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 ...
随机推荐
- vim学习4
分频 参考 http://coolshell.cn/articles/1679.htm
- 几个经常使用的cmd命令
compmgmt.msc 计算机管理 devmgmt.msc 设备管理器 diskmgmt.msc 磁盘管理工具 dfrg.msc 磁盘碎片整理 eventvwr.msc 事件查看器 fsm ...
- ontouch-控件添加ontouch监听事件
1,代码public class CalculatorViewPager extends ViewPager {}中 package com.android.calculator2; import a ...
- redhat6.5安装10201解决办法
rpm --import /etc/pki/rpm-gpg/RPM*yum install -y --skip-broken compat-libstdc++* elfutils-libelf* g ...
- LayUI-Table表格渲染
记项目中又一表格使用方法,项目首选是使用BootstrapTable的,但是经过多番查证与调试,始终没有把固定列的功能调试成功,找到的成功的例子原样照搬都不行,文件引入也都没有问题,实在搞不懂了,如果 ...
- j2ee,jsp,servlet文件下载server端
1.getOutputStream() has already been called for this response 报错的原因: 使用tomcat容器调用response.getOutputS ...
- CSS笔记 - fgm练习 2-7 - 简易选项卡
练习地址 http://www.fgm.cc/learn/lesson2/07.html <style> body,ul,li{margin:0;padding:0;} body{font ...
- 1.Python字符编码
1.编码简介 编码的种类情况 ASCII 占1个字节,只支持英文 GB2312 占2个字节,支持6700+汉字 GBK GB2312的升级版,支持21000+汉字 ks_c_5601-1987 韩国编 ...
- FTP 访问的形式
主要是扼要的列举一下访问的方式,不涉及太具体的内容.大家可以在百度上搜索一下具体的操作方法. 主要有: 1. 网页浏览器中输入 ftp://192.168.0.111的形式. 2. 资源管理器中输入f ...
- C#游戏开发高速入门 2.1 构建游戏场景
C#游戏开发高速入门 2.1 构建游戏场景 假设已经计划好了要编写什么样的游戏,在打开Unity以后.要做的第一件事情就是构建游戏场景(Scene).游戏场景就是玩家游戏时,在游戏视图中看到的一切. ...