Description
Peiling a total of N size Lakes L1, l2, ..., Ln(which includes peiling), each lake Li has a frog Fi(1≤ i ≤ N). If there is a waterway connection between the lake Li and Lj , the Frog Fi and Fj each other as neighbors. It is now known that the number of neighbors per frog x1,x2, ..., xn, please give a link to each of the two lakes.
Input
The first line is the number of groups of test data T(0≤ t ≤20). Each group of data consists of two rows, the first line is an integer n (2 < N < 10), the second row is n integers,x1,x2,..., xN (0≤ XI ≤ N).
Output
For each set of test data entered, if there is no possible connection, output "no". Otherwise, the output is " YES" and an n-n Matrix represents the neighboring relationship between lakes, that is, if Lake i is connected to Lake J by Waterway, the first row The J number is 1, otherwise 0. Output a space between each of the two numbers. If there is more than one possibility, just give a condition that matches the situation. Output a blank line between two adjacent sets of test data.
Sample Input
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 0The requirements of the topic are based on whether a given set of degrees can form a complete picture if it is possible to output the adjacency matrix of the graph; According to the Havel theorem, if the maximum number of degrees vertices is greater than the sum of all other vertices, it does not constitute a graph. So we can do it every time
1 sorting an array of degrees
2 According to the theorem to determine whether the composition
3 Connect each time a maximum number of points is completed (remove the point and let the number of dmax vertices in the back minus 1)
Repeat the above steps to complete the composition
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace std;struct node{int num; number int degree; } q[20];int map[20][20]; adjacency Matrix int S;int cmp (node A,node b) {return a.degree>b.degree; Based on degrees from large to small sort}void connect ()//Connect to the maximum point {int a A, B; int j=2; A=q[1].num; while (q[1].degree--)//dmax vertex degree-1 {b=q[j].num; q[j].degree--; if (q[j].degree==0)//A point with a degree of 0 then the number of vertices remaining-1 s--; j + +; Map[a][b]=map[b][a]=1; Connect} return;} int main () {int m,n,maxd; int I,j,flag; int t=0; scanf ("%d", &n); while (n--) {if (t!=0) cout<<endl; t++; flag=0; scanf ("%d", &m); S=m; Maxd=0; memset (map,0,sizeof (map)); for (I=1; i<=m; i++) {scanf ("%d", &q[i].degree); Q[i].num=i; if (q[i].degree<=0) s--; if (Q[i].degree>maxd) Maxd=q[i].degree; } sort (q+1,q+1+m,cmp); while (maxd!=0) {if (maxd>s-1)//theorem {flag=1; cout<< "NO" <<endl; Break } sort (q+1,q+1+m,cmp); Connect (); s--; Remove the maximum vertex sort (q+1,q+1+m,cmp) in degrees; Maxd=q[1].degree; } if (!flag) {cout<< "YES" <<endl; for (i=1;i<=m;i++) for (j=1;j<=m;j++) {if (j!=m) c out<<map[i][j]<< "; else cout<<map[i][j]<<endl; }}} return 0;}
POJ 1659 Frogs ' neighborhood--a-havel theorem for the determination of graphs