Poj 1659 frogs 'neighborhood (degree sequence)

Source: Internet
Author: User

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

Degree sequence: If the degrees of all vertices of graph G are arranged in a sequence S, S is called the degree sequence of graph G.

Train of Thought: sort the degree from large to small {D1, D2, D3 ....., DN}, delete the first D1 and the first D1 edge (set to zero), and reduce the D1 degree by one. If a certain degree is negative, the sequence cannot be shown.

# Include "stdio. H "# include" string. H "# include" algorithm "using namespace STD; # define n 15 struct node {int deg, ID;} G [N]; bool CMP (node A, Node B) {return. DEG> B. deg;} int main () {int I, J, K, U, V, N, T; int e [N] [N]; scanf ("% d ", & T); While (t --) {scanf ("% d", & N); memset (E, 0, sizeof (e); for (I = 0; I <n; I ++) {scanf ("% d", & G [I]. DEG); G [I]. id = I;} int flag = 1; for (k = 0; k <n; k ++) // n traversal {sort (G, G + N, CMP); // arrange the degrees in descending order // printf ("% d \ n", G [0]. DEG); If (G [0]. DEG> N-1) {flag = 0; break;} u = G [0]. ID; for (I = 1; I <= G [0]. deg; I ++) {G [I]. DEG --; If (G [I]. DEG <0) Flag = 0; V = G [I]. ID; E [u] [v] = E [v] [u] = 1;} G [0]. DEG = 0;} If (FLAG) {printf ("Yes \ n"); for (I = 0; I <n; I ++) {for (j = 0; j <n-1; j ++) {printf ("% d", E [I] [J]);} printf ("% d \ n ", E [I] [J]) ;}} else printf ("NO \ n"); If (t) puts ("") ;}return 0 ;}



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.