Poj 1659: frogs 'neighborhood (Havel-Hakimi theorem)

Source: Internet
Author: User

Frogs 'neighborhood
Time limit:5000 Ms   Memory limit:10000 K
Total submissions:6898   Accepted:3006   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 


The meaning of this question is actually to give a sequence of non-negative integers and ask if it is a sequence of integers, that is, whether it can be based on this sequence

Column to construct a graph. This is to make a diagram based on the methods in the Havel-Hakimi theorem, and determine whether or not there is an unreasonable situation in the diagram.

Situation. That is, when the number of vertices is less than 0, or the maximum degree is greater than the number of vertices or the remaining Vertex Tree appears.


For example, the first group of data:

7

4 3 1 5 4 2 1


Sorted, 5 4 4 3 2 1

Change the maximum degree to 0 and 5 to 0, and then the last 5 numbers-1; that is, 0 3 3 2 1 0 1

Sort 3 3 2 1 1 0 0 ----> 0 2 1 0 1 0 0

--------> 2 1 1 0 0 0 -----> 0 0 0 0 0 0

This is because there is no unreasonable number .. Yes


Look at the second group of data

6

4 3 1 4 2 0

Sorted: 4 4 3 2 1 0 -----> 0 3 2 1 0 ---------> 3 2 1 0 0 --------> 0 1 0-1 0 0 0 0

It cannot be constructed because a negative number is displayed, that is, it is 0;


Note that the figure in the output result may be different from the sample...



# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # include <vector> # include <queue> # include <cmath> using namespace STD; const int maxn = 20; struct node {int degree; // degree of vertex int index; // vertex serial number}; node V [maxn]; int edge [maxn] [maxn]; bool CMP (node A, Node B) {return. degree> B. degree;} int main () {int N; int t; scanf ("% d", & T); While (t --) {int flag = 1; scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% d", & V [I]. degree); V [I]. index = I;} memset (edge, 0, sizeof (edge); For (INT I = 0; I <n; I ++) {sort (V, V + N, CMP); // sort if (V [0]. degree = 0) break; For (Int J = 1; j <n; j ++) {v [J]. degree --; If (V [J]. degree <0) // case where the flag is invalid {flag = 0; break;} edge [V [0]. index] [V [J]. index] = edge [V [J]. index] [V [0]. index] = 1; V [0]. degree --; If (V [0]. degree = 0) break;} If (flag = 0) brea K;} If (flag = 0) printf ("NO \ n"); else {printf ("Yes \ n"); For (INT I = 0; I <n; I ++) {for (Int J = 0; j <n; j ++) {If (! J) printf ("% d", edge [I] [J]); else printf ("% d", edge [I] [J]);} printf ("\ n") ;}} if (t) printf ("\ n");} return 0 ;}





Poj 1659: frogs 'neighborhood (Havel-Hakimi theorem)

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.