Frogs' Neighborhood
Time Limit: 5000MS   Memory Limit: 10000K
Total Submissions: 8729   Accepted: 3676   Special Judge

Description

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

Input

第一行是测试数据的组数T(0 ≤ T ≤ 20)。每组数据包括两行,第一行是整数N(2 < N < 10),第二行是N个整数,x1x2,..., 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 题目链接:http://poj.org/problem?id=1659



分析:
  给定一个非负整数序列,问是不是一个可图的序列,也就是说能不能根据这个序列构造一个图。
利用Havel-Hakimi定理。
  (1)某次对剩下的序列进行非递增排序后,最大的度数degree超过了剩下的顶点数
  (2)对最大度数后面的degree个数依次减1,出现了负数。
  出现以上2种情况之一,则判定该序列不可图。



#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 15
struct vertex
{
int degree;
int index;
}V[N];
bool cmp(vertex x,vertex y)
{
return x.degree>y.degree;
}
int main()
{
int i,j,t,k,T,n,flag;
int Edge[15][15];
cin>>T;
while(T--)
{
cin>>n;
for(i=0;i<n;i++)
{
cin>>V[i].degree;
V[i].index=i;
}
memset(Edge,0,sizeof(Edge));
flag=1;
for(k=0;k<n&&flag;k++)
{
sort(V+k,V+n,cmp);
i=V[k].index;
if(V[k].degree>n-k-1) flag=0;
for(t=1;t<=V[k].degree&&flag;t++)
{
j=V[k+t].index;
V[k+t].degree-=1;
if(V[k+t].degree<0) flag=0;
Edge[i][j]=Edge[j][i]=1;
}
}
if(flag)
{
cout<<"YES"<<endl;
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
cout<<Edge[i][j]<<" ";
cout<<Edge[i][j]<<endl;
}
cout<<endl;
}
else cout<<"NO"<<endl<<endl;
}
return 0;
}

  


POJ1659 Frogs' Neighborhood(青蛙的邻居) Havel-Hakimi定理的更多相关文章

  1. POJ1659 Frogs' Neighborhood(Havel–Hakimi定理)

    题意 题目链接 \(T\)组数据,给出\(n\)个点的度数,问是否可以构造出一个简单图 Sol Havel–Hakimi定理: 给定一串有限多个非负整数组成的序列,是否存在一个简单图使得其度数列恰为这 ...

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

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

  3. poj1659 Frogs' Neighborhood

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

  4. POJ1659 Frogs' Neighborhood(Havel定理)

    给一个无向图的度序列判定是否可图化,并求方案: 可图化的判定:d1+d2+……dn=0(mod 2).关于具体图的构造,我们可以简单地把奇数度的点配对,剩下的全部搞成自环. 可简单图化的判定(Have ...

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

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

  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 Description 未名湖附近共有N个大小湖泊L ...

  8. Frogs' Neighborhood

    Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 7920   Accepted: 33 ...

  9. poj 1659 Frogs' Neighborhood (DFS)

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

随机推荐

  1. vue -- 九宫格抽奖

    html: <div class="line_item" :class="index == 1 ? 'active' : 'white_item'"> ...

  2. AJAX简单实例

    越用AJAX越觉得它的强大.好用. 平常我们提交表单,是直接通过action属性,直接向后台提交数据. 我们也可以用AJAX向后台提交数据.例如: 这是一个表单,两个字段:notice,scort,保 ...

  3. wamp 安装redis扩展

    phpredis扩展下载地址  http://windows.php.net/downloads/pecl/snaps/redis/ 1.选择redis DLL文件扩展 phpinfo 查看VC版本 ...

  4. jquery+jquery.pagination+php+ajax 无刷新分页

    <!DOCTYPE html> <html ><head><meta http-equiv="Content-Type" content= ...

  5. 第三章 列表(b)无序列表

  6. e-olymp Problem11 Big accuracy

    传送门:点我 Big accuracy The rational fraction m/n is given. Write it in the decimal notation with k digi ...

  7. Max Points on a Line (HASH TABLE

    QUESTIONGiven n points on a 2D plane, find the maximum number of points that lie on the same straigh ...

  8. 使用pyqt写了一个检查大数据环境的gui

    通过pyqt做了一个大数据最佳实践检查的gui界面 1.首先是需要用到的模块 from PyQt5.QtWidgets import QApplication from PyQt5.QtWidgets ...

  9. jQuery 与js判断是否单选复选选中

    js判断复选:这段代码昨天网上查看的资料没保存出处,抱歉 var obj=document.getElementsByName("diseaseSet"); //选择所有name= ...

  10. echarts故障统计多维柱状图 堆叠柱状图 柱状图Demo2

    黑底:echarts链接:http://gallery.echartsjs.com/editor.html?c=xnP8JPeu4R option = { backgroundColor: 'blac ...