poj 1659

Description

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

Input

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

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

提示:Havel-Hakimi定理

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <memory.h> using namespace std; const int maxn = 12; class Pair
{
public:
int m_id;
int val;
}; bool cmp(Pair a, Pair b)
{
return a.val > b.val;
} int main()
{
Pair p[maxn];
int ans[maxn][maxn];
int T;
scanf("%d", &T);
while (T--)
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
p[i].m_id = i + 1;
scanf("%d", &p[i].val);
}
memset(ans, 0, sizeof(ans));
bool flag = true;
int num = n;
while (num--)
{
sort(p, p + n, cmp);
if (p[0].val == 0)
break;
for (int i = 1; i <= p[0].val; i++)
{
if (p[i].val > 0 && i < n)
{
ans[p[0].m_id][p[i].m_id] = ans[p[i].m_id][p[0].m_id] = 1;
p[i].val--;
}
else
{
flag = false;
break;
}
}
if (!flag)
break;
p[0].val = 0;
}
if (flag)
{
puts("YES");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (j == 1)
printf("%d", ans[i][j]);
else
printf(" %d", ans[i][j]);
}
putchar('\n');
}
}
else
puts("NO");
if (T > 0)
putchar('\n');
}
return 0;
}

Frogs‘ Neighborhood(POJ 1659 C/C++)的更多相关文章

  1. poj 1659 Frogs' Neighborhood (DFS)

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

  2. poj 1659 Frogs' Neighborhood (贪心 + 判断度数序列是否可图)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 6076   Accepted: 26 ...

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

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

  4. POJ 1659 Frogs&#39; Neighborhood(度序列组成)

    意甲冠军  中国 依据Havel-Hakimi定理构图即可咯  先把顶点按度数从大到小排序  可图的话  度数大的顶点与它后面的度数个顶点相连肯定是满足的  出现了-1就说明不可图了 #include ...

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

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

  6. poj 1659 Frogs&#39; Neighborhood (度序列)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 7295   Accepted: 31 ...

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

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 10545   Accepted: 4 ...

  8. poj 1659 Frogs' Neighborhood( 青蛙的邻居)

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 9639   Accepted: 40 ...

  9. poj 1659 Frogs&#39; Neighborhood 度序列可图化 贪心

    题意: 对一个无向图给出一个度序列,问他是否可简单图化. 分析: 依据Havel定理,直接贪心就可以. 代码: //poj 1659 //sep9 #include <iostream> ...

随机推荐

  1. centos8平台安装gitosis服务

    一,git服务器端:准备gitosis需要的各依赖软件 1,确认openssh是否存在?如不存在,以下列命令进行安装 [root@yjweb ~]# yum install openssh opens ...

  2. scrapy-下载器中间件 随机切换user_agent

    from faker import Faker class MySpiderMiddleware(object): def __init__(self): self.fake = Faker() de ...

  3. 生成流水号(20060210-0001)的SQL函数

    create table t_sql(id int identity(1,1),code char(13),[name] nvarchar(10)) go create function f_crea ...

  4. JavaScript 的用法

    内建的 JavaScript 对象可用于全局属性和函数​ 顶层函数(全局函数) 函数 描述 decodeURI() 解码某个编码的 URI. decodeURIComponent() 解码一个编码的 ...

  5. CentOS8安装本地mail工具-mailx-12.5-29.el8.x86_64

    概述 服务器需要发告警邮件 查找是否已安装 [root@C8-1 ~]# type mail -bash: type: mail: not found [root@C8-1 ~]# which mai ...

  6. 不可不知的资源管理调度器Hadoop Yarn

    Yarn(Yet Another Resource Negotiator)是一个资源调度平台,负责为运算程序如Spark.MapReduce分配资源和调度,不参与用户程序内部工作.同样是Master/ ...

  7. git学习(十一) idea git pull 解决冲突

    测试如下: 先将远程的代码修改,之后更新: 之后将工作区修改的代码(这里修改的代码跟远程修改的位置一样)提交到本地,之后拉取远程的代码,会发现有冲突: Accept Yours 就是直接选取本地的代码 ...

  8. ceil中有-0啊

          这里主要是有一点: 1 Math.ceil(d1)  ceil 方法上有这么一段注释:If the argument value is less than zero but greater ...

  9. mysql query cache 查询缓存

    查看本博文,并进行验证(验证结果与博文一致): https://blog.csdn.net/carmazhao/article/details/7088530 mysql默认是开启查询缓存的. 设置查 ...

  10. h5 语义话标签的意义

    使用语义话标签的意义 语义类标签对开发者更为友好,使用语义类标签增强了可读性,即便是在没有 CSS 的时 候,开发者也能够清晰地看出网页的结构,也更为便于团队的开发和维护. 除了对人类友好之外,语义类 ...