未名湖附近共有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

POJ Monthly–2004.05.15 Alcyone@pku

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79


#include <cstring>
#include <algorithm>
using namespace std;

const int maxn=15;
struct {
int id;
int deg;
}v[maxn];
bool (node a,node b)
{
return a.deg>=b.deg;
}
int e[maxn][maxn];大专栏  poj 1659 Frog's Neighborhoodr>
int main()
{
int T;
scanf("%d",&T);
while (T--)
{
int n;
scanf("%d",&n);
for (int i=0;i<n;i++)
{
scanf("%d",&v[i].deg);
v[i].id=i;
}
int flag=1;
memset(e,0,sizeof(e));
for (int k=0;k<n;k++)
{
sort(v+k,v+n,cmp);
int id=v[k].id;
int d1=v[k].deg;
if(d1>n-k-1)
{
flag=0;
continue;
}
for (int mm=1;mm<=v[k].deg;mm++)
{
int j=v[k+mm].id;
if(v[k+mm].deg<=0)
{
flag=0;
break;
}
v[k+mm].deg--;
e[id][j]=e[j][id]=1;
}
}
if(!flag)
{
cout<<"NO"<<endl;


}
else
{
cout<<"YES"<<endl;
for (int i=0;i<n;i++)
{
for (int j=0;j<n;j++)
{
if(j) printf(" ");
printf("%d",e[i][j]);
}
puts("");
}

}
if(T)
puts("");
}
return 0;
}

poj 1659 Frog's Neighborhood的更多相关文章

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

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

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

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

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

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

  4. Frogs‘ Neighborhood(POJ 1659 C/C++)

    poj 1659 Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路 ...

  5. poj 1659 Frogs' Neighborhood (DFS)

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

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

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

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

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

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

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

  9. Poj 1659.Frogs' Neighborhood 题解

    Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和 ...

随机推荐

  1. 用eclipse运行算法第四版的BinarySearch

    import java.util.Arrays; import edu.princeton.cs.algs4.In; import edu.princeton.cs.algs4.StdIn; impo ...

  2. 吴裕雄--天生自然TensorFlow2教程:创建Tensor

    import numpy as np import tensorflow as tf tf.convert_to_tensor(np.ones([2, 3])) tf.convert_to_tenso ...

  3. Android java项目中引用kotlin,混合开发工程配置

    https://www.jianshu.com/p/9220227cdfb3 buildscript { ext.kotlin_version = '1.2.71' repositories { go ...

  4. UML-如何画常用UML交互图?

    1.生命线框图(参与者) 2.消息表达式

  5. Python KNN 学习曲线

    学习曲线的目的是选择更好的模型参数.以最近邻算法为例,选取最近的多少个数据点,才能达到最优.可以控制训练集不动,调整最近的点的个数,绘制学习曲线. import matplotlib.pyplot a ...

  6. JavaSE--Comparator 和 Comparable 区别

    public interface Comparable<T> 此接口强行对实现它的每个类的对象进行整体排序.这种排序被称为类的自然排序,类的 compareTo 方法被称为它的自然比较方法 ...

  7. Django专题之ORM

    ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述 ...

  8. 洛谷 P1082 同余方程(exgcd)

    题目传送门 解题思路: 因为推导过程过于复杂,懒得写,所以题解传送门 AC代码: #include<iostream> #include<cstdio> using names ...

  9. Python程序中的进程操作--—--开启多进程

    Python程序中的进程操作-----开启多进程 之前我们已经了解了很多进程相关的理论知识,了解进程是什么应该不再困难了,刚刚我们已经了解了,运行中的程序就是一个进程.所有的进程都是通过它的父进程来创 ...

  10. springBoot中mybatis错误之 Property 'configuration' and 'configLocation' can not specified with together 解决

    mybatis.config-location与mybatis.config-locations不同 mybatis.config-location不加载全局配置文件