POJ 1659 Frogs & #39; Neighborhood (degree sequence diagram), pojfrogs

Source: Internet
Author: User

POJ 1659 Frogs 'neighborhood (degree sequence diagram), pojfrogs

Meaning Chinese

According to the Havel-Hakimi theorem, let's just figure it out. First, if we sort the vertices in descending order of degrees, then the vertices with higher degree will be connected to the vertices behind it, which must be satisfied- 1 indicates that the graph is unavailable.

#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N = 20;int mat[N][N], ord[N];bool cmp(int i, int j){    return mat[i][0] > mat[j][0];}int main(){    int cas, i, j, k, t, n;    scanf("%d", &cas);    while(cas--)    {        memset(mat, 0, sizeof(mat));        scanf("%d", &n);        for(i = 1; i <= n; ++i)        {            scanf("%d", &mat[i][0]);            ord[i] = i;        }        for(i = 1; i <= n; ++i)        {            sort(ord + i, ord + n + 1, cmp);            t = ord[i];            if(mat[t][0] < 0) break;            for(j = 1; j <= mat[t][0]; ++j)            {                k = ord[i + j];                mat[t][k] = mat[k][t] = 1;                --mat[k][0];            }        }        if(i <= n) printf("NO\n");        else        {            printf("YES\n");            for(i = 1; i <= n; ++i)            {                for(int j = 1; j <= n; ++j)                    printf("%d ", mat[i][j]);                printf("\n");            }        }        if(cas) printf("\n");    }    return 0;}

Frogs 'neighborhood

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 Alcyone @ pku



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.