Poj1659frogs 'neighborhood (lavel theorem)

Source: Internet
Author: User
Frogs 'neighborhood
Time limit:5000 Ms   Memory limit:10000 K
Total submissions:7260   Accepted:3132   Special Judge

Description

Near the unnamed LakeNLarge and small lakesL1,L2 ,...,Ln(Including unnamed lakes), each lakeLiA frog lives inFi(1 ≤IN). If the lakeLiAndLJIf there is a waterway connection between them, then the frogFiAndFJThey are known as neighbors. We now know the number of neighbors for each frog.X1,X2 ,...,XN, Please give the connection between every two lakes.

Input

The first row is the number of test data groups.T(0 ≤T≤ 20 ). Each group of data includes two rows. The first row is an integer N (2 <N<10), the second line isNIntegers,X1,X2 ,...,XN (0 ≤XIN).

Output

For each group of input test data, if there is no possible connection, output "no ". Otherwise, output "yes" and useN×NIndicates the adjacent relationship between lakes.IWith lakesJIf there is a waterway connection between themIThe number of rowsJThe number is 1, otherwise it is 0. A space is output between two numbers. If there are multiple possibilities, you only need to provide a situation that meets the conditions. An empty row is output between two adjacent groups of test data.

Sample Input

374 3 1 5 4 2 1 64 3 1 4 2 0 62 3 1 1 2 1 

Sample output

YES0 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 NOYES0 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 [email protected]
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;struct node{    int indx,ans;}a[15];int cmp(node a,node b){    return a.ans>b.ans;}int main(){    int map[15][15],t,n,flag=0;    scanf("%d",&t);    while(t--)    {        if(flag)printf("\n");flag=1;        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%d",&a[i].ans); a[i].indx=i;        }        int tn=n;        memset(map,0,sizeof(map));        while(n)        {            sort(a,a+n,cmp);            int i;            for(i=0;i<n;i++)            if(a[i].ans==0)            {                n=i; break;            }            i=1;            while(a[0].ans&&i<n&&a[i].ans)            {                a[0].ans--; a[i].ans--;                map[a[0].indx][a[i].indx]=map[a[i].indx][a[0].indx]=1;                i++;            }            if(a[0].ans>0)break;        }        if(n)        printf("NO\n");        else        {            printf("YES\n");            for(int i=0;i<tn;i++)            {                for(int j=0;j<tn;j++)                if(j==0) printf("%d",map[i][j]);                else printf(" %d",map[i][j]);                printf("\n");            }        }    }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.