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 ≤I≤N). 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 ≤XI≤N).
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"); } } }}