Frogs' Neighborhood
Time Limit: 5000MS | Memory Limit: 10000K | |||
Total Submissions: 7920 | Accepted: 3392 | 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
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
using namespace std;
const int INF=0x5fffffff;
const int MS=;
const double EXP=1e-;
struct node
{
int degree;
int id;
bool operator <(const node &b) const
{
return degree>b.degree;
}
}nodes[MS]; int edges[MS][MS]; int main()
{
int i,j,k;
int n,T;
cin>>T;
while(T--)
{
cin>>n;
for(i=;i<n;i++)
{
cin>>nodes[i].degree;
nodes[i].id=i;
}
memset(edges,,sizeof(edges));
int flag=;
for(k=;k<n&&flag;k++)
{
sort(nodes+k,nodes+n);
i=nodes[k].id;
int du=nodes[k].degree;
if(du>n-k-)
flag=;
for(int s=;s<=du&&flag;s++)
{
j=nodes[s+k].id;
if(nodes[s+k].degree<=)
flag=;
nodes[s+k].degree--;
edges[i][j]=edges[j][i]=; }
}
if(flag)
{
cout<<"YES"<<endl;
for(i=;i<n;i++)
{
for(j=;j<n;j++)
{
if(j)
cout<<" ";
cout<<edges[i][j];
}
cout<<endl;
}
}
else
cout<<"NO"<<endl;
if(T)
cout<<endl;
}
return ;
}
Frogs' Neighborhood的更多相关文章
- 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 (贪心 + 判断度数序列是否可图)
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 Total Submissions: 9897 Accepted: 41 ...
- POJ 1659 Frogs' Neighborhood (Havel--Hakimi定理)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10545 Accepted: 4 ...
- POJ1659 Frogs' Neighborhood(青蛙的邻居) Havel-Hakimi定理
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 8729 Accepted: 36 ...
- poj1659 Frogs' Neighborhood
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 10239 Accepted: 4 ...
- poj 1659 Frogs' Neighborhood( 青蛙的邻居)
Frogs' Neighborhood Time Limit: 5000MS Memory Limit: 10000K Total Submissions: 9639 Accepted: 40 ...
- POJ1659 Frogs' Neighborhood(Havel定理)
给一个无向图的度序列判定是否可图化,并求方案: 可图化的判定:d1+d2+……dn=0(mod 2).关于具体图的构造,我们可以简单地把奇数度的点配对,剩下的全部搞成自环. 可简单图化的判定(Have ...
随机推荐
- index structure
1. wordlist 0) 0, 1byte 1) token-id(delta), 8byte 2) doclist-offset(delta), 8byte 3) doc_count, 4byt ...
- POJ 1410 Intersection(判断线段交和点在矩形内)
Intersection Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9996 Accepted: 2632 Desc ...
- spring 的properties解析
一般使用PropertyPlaceholderConfigurer来替换占位符,例如: <bean class="org.springframework.beans.factory.c ...
- Cisco SDM
SDM连接方式:http+telnet / https+ssh 要使用SDM对CISCO设备实现集中式管理,必须在设备上键入如下命令: 步骤1: 要启用路由器的HTTP/HTTPS 服务器,请 ...
- javascript日期格式化方法汇总
本文给大家汇总介绍了javascript格式化日期时间的几种常用方法,个人对最后一种个性化输出时间比较有兴趣,基本上只要项目中能用到都是使用这种,推荐给小伙伴们. 方法一: ? 1 2 3 4 5 6 ...
- psutil documentation
Quick links Home page Blog Download Forum What’s new About From project’s home page: psutil (python ...
- android 自定义用相机拍照后的照片存储位置
1.imageUri = Uri.fromFile(new File(Environment .getExternalStorageDirectory()+ File.separator + getP ...
- python sleep
Python Sleep休眠函数 Python 编程中使用 time 模块可以让程序休眠,具体方法是time.sleep(秒数),其中"秒数"以秒为单位,可以是小数,0.1秒则代表 ...
- hdoj 5335 Walk Out
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5335 #include<stdio.h> #include<cstring> ...
- linux中less命令使用
less与cat和more的区别: cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此经常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面 ...