Topic Link:
http://poj.org/problem?id=1659
Type: Greed, Havel-hakimi theorem
Topic:
Description
There are N large and small lakes L1, L2, ..., Ln (including lively) in the vicinity of Lively, and a Frog fi (1≤i≤n) is living in each lake Li. If there is a waterway between the lake Li and LJ, Frog fi and FJ each other as neighbors. The number of neighbors known to each frog is now x1, x2, ..., xn, please give the connection between each of the two lakes.
Input
The first line is the number of groups T (0≤T≤20) that test the data. Each group of data consists of two rows, the first row is an integer n (2 < N < 10), the second row is n integers, x1, x2,..., xn (0≤xi≤n).
More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Output
For each set of test data entered, if there is no possible connection, the output "no". Otherwise the output is "YES" and the NXN matrix is used to represent the adjacent relationship between lakes, that is, if there is a waterway connection between Lake I and Lake J, the number J of line I is 1, otherwise 0. A space is output between each two digits. If there are multiple possibilities, just give a condition that meets the criteria. Output a blank line between two adjacent sets of test data.
Sample Input
3
7
4 3 1 5 4 2 1 6 4 3 1 4 2 0 6 2 3 1 1 2-
1
Sample Output
YES
0 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
No. 0 0 0 0
NO
YES
0
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
Analysis and Summary:
Havel-hakimi theorem is applied to the problem, according to the method of this theorem can be directly done.
Code:
* * POJ 1659 frogs ' neighborhood * greed, Havel-hakimi theorem * time:0ms * author:d_double * * * * * #include <iostr eam> #include <algorithm> #include <cstdio> #include <cstring> #define MAXN using Namespac
e std;
struct node{int no;
int degree;
friend BOOL operator < (const node &a, const node &b) {return a.degree>b.degree;
}}ARR[MAXN];
int g[maxn][maxn],n;
BOOL Havel_hakimi () {for (int i=0; i<n-1; ++i) {sort (arr+i,arr+n);
if (I+arr[i].degree >= N) return false;
for (int j=i+1; j<=i+arr[i].degree; ++j) {--arr[j].degree;
G[arr[i].no][arr[j].no]=g[arr[j].no][arr[i].no]=1;
if (Arr[j].degree < 0) return false;
} if (arr[n-1].degree!=0) return false;
return true;
int main () {int T;
scanf ("%d", &t); while (t--) {scanf ("%d",&n);
int sum=0;
memset (g, 0, sizeof (g));
BOOL Flag=true;
for (int i=0; i<n; ++i) {scanf ("%d", &arr[i].degree);
arr[i].no = i;
if (arr[i].degree>n-1) Flag=false;
sum + + Arr[i].degree;
} if (sum%2!=0) Flag=false;
if (sum==0 | | Flag&&havel_hakimi ()) {printf ("yes\n");
for (int i=0; i<n; ++i) {printf ("%d", g[i][0]);
for (int j=1; j<n; ++j) printf ("%d", g[i][j]);
printf ("\ n");
} else printf ("no\n");
if (T) printf ("\ n");
return 0; }
Author: csdn Blog shuangde800