poj 1659 Frogs' Neighborhood (度序列)
| Time Limit: 5000MS | Memory Limit: 10000K | |||
| Total Submissions: 7295 | Accepted: 3150 | 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
度序列:若把图G全部顶点的度数排成一个序列S,则称S为图G的度序列。
思路:把度数从大到小排序{d1,d2,d3.....,dn}。把首项d1与它后面的d1项连边。删除首项(置为零)。这d1项的度数均减一,若某项度数为负。则该序列不可图。
#include"stdio.h"
#include"string.h"
#include"algorithm"
using namespace std;
#define N 15
struct node
{
int deg,id;
}g[N];
bool cmp(node a,node b)
{
return a.deg>b.deg;
}
int main()
{
int i,j,k,u,v,n,T;
int e[N][N];
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(e,0,sizeof(e));
for(i=0;i<n;i++)
{
scanf("%d",&g[i].deg);
g[i].id=i;
}
int flag=1;
for(k=0;k<n;k++) //n次遍历
{
sort(g,g+n,cmp); //度数递减排列
//printf("%d\n",g[0].deg);
if(g[0].deg>n-1)
{
flag=0;break;
}
u=g[0].id;
for(i=1;i<=g[0].deg;i++)
{
g[i].deg--;
if(g[i].deg<0)
flag=0;
v=g[i].id;
e[u][v]=e[v][u]=1;
}
g[0].deg=0;
}
if(flag)
{
printf("YES\n");
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
printf("%d ",e[i][j]);
}
printf("%d\n",e[i][j]);
}
}
else
printf("NO\n");
if(T)
puts("");
}
return 0;
}
poj 1659 Frogs' Neighborhood (度序列)的更多相关文章
- poj 1659 Frogs' Neighborhood 度序列可图化 贪心
题意: 对一个无向图给出一个度序列,问他是否可简单图化. 分析: 依据Havel定理,直接贪心就可以. 代码: //poj 1659 //sep9 #include <iostream> ...
- POJ 1659 Frogs' Neighborhood(度序列组成)
意甲冠军 中国 依据Havel-Hakimi定理构图即可咯 先把顶点按度数从大到小排序 可图的话 度数大的顶点与它后面的度数个顶点相连肯定是满足的 出现了-1就说明不可图了 #include ...
- poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 6076 Accepted: 26 ...
- POJ 1659 Frogs' Neighborhood(Havel-Hakimi定理)
题目链接: 传送门 Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Description 未名湖附近共有N个大小湖泊L ...
- 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定理 可简单图定理
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4098136.html 给定一个非负整数序列$D=\{d_1,d_2,...d_n\}$,若存 ...
- poj 1659 Frog's Neighborhood
未名湖附近共有N个大小湖泊L1, L2, -, Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只 ...
- POJ 1659 Frogs' Neighborhood(可图性判定—Havel-Hakimi定理)【超详解】
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9897 Accepted: 41 ...
- poj 1659 Frogs' Neighborhood( 青蛙的邻居)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9639 Accepted: 40 ...
随机推荐
- js下firstElementChild firstChild 以及childNodes和children方法
一. <div> <p>123</p> </div> 在上面这段代码中,如果使用以下js代码 var oDiv=document.getElementB ...
- Git 在小团队中的管理流程(转)
目标读者:了解 Git 的基本概念,能够使用 Git 进行基本的本地和远程操作. 有关 Git 的基础知识可以参见 知乎回答-怎样使用 GitHub?,天猪(刘勇)给出了一些很好的学习资料. 本文介绍 ...
- APNS 那些事!
之前在消息推送中间件APush里实现了对APNS的桥接.并利用业余时间阅读了官方指南Local and Push Notification Programming Guide.蛮有心得的.稍作总结.分 ...
- LeetCode77:Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 - n. For example, ...
- linux(readhat) yum源安装
在安装測试环境的时候遇到了一个问题,/etc/yum/repos.d中不存在文件或目录,无法更新yum源. 解决方法: (一.配置网络yum源) 1.首先在/etc/yum/repos.d/文件夹下创 ...
- RMAN 备份
backup database; --备份整库 backup database format '\xxxxxx\xxx_%U'; --备份整库到指定路劲 backup tablespace users ...
- fedora 搭建pptp vpn server
1 首先去sourceforge上下载pptpd的源码 http://sourceforge.net/projects/poptop/files/?source=navbar 2 对源码进行编译 ./ ...
- ANT编译Android Eclipse工程
将Android SDK的tools/和platform-tools/目录包含在可执行文件的搜索路径中.Windows下,将其添加到PATH环境变量中 切换到Android Eclipse项目目录下, ...
- 11661 - Burger Time?
Burger Time? Everybody knows that along the more important highways there are countless fast food ...
- VS2008下编译BOOST 1.39的ASIO库
由于全部编译BOOST库需要的时间太长,而且耗费空间,况且我只需要用ASIO库,所以就没有必要全部编译了. boost库到www.boost.org上下载. 编译很简单,假设你的boost存放的目录是 ...