Analysis: the first question to cut graph theory is also an example of graph theory. The Havel-Hakimi theorem is mainly used in the following two unreasonable situations: (1) after sorting the remaining sequence, the maximum degree (set to d1) exceeds the number of remaining vertices. (2) after each of the d1 degrees after the maximum degree is reduced by 1, A negative number is displayed.
# Include <stdio. h> # include <stdlib. h> # include <string. h> # define N 15 struct vertex {int degree; // vertex degree int index; // vertex number} v [N]; int cmp (const void *, const void * B) {return (vertex *) B)-> degree-(vertex *) a)-> degree;} int main () {int t, n, i, j, k, r, p, q, d1; int Edge [N] [N], flag; scanf ("% d", & t ); while (t --) {scanf ("% d", & n); for (I = 0; I <n; I ++) {scanf ("% d ", & v [I]. degree); v [I]. index = I;} memset (Edge, 0, sizeof (Edge); flag = 1; for (k = 0; k <n & flag; k ++) {qsort (v + k, n-k, sizeof (vertex), cmp); I = v [k]. index; d1 = v [k]. degree; if (d1> n-k-1) flag = 0; for (r = 1; r <= d1 & flag; r ++) {j = v [k + r]. index; if (v [k + r]. degree <= 0) flag = 0; v [k + r]. degree --; Edge [I] [j] = Edge [j] [I] = 1 ;}} if (flag) {puts ("YES "); for (p = 0; p <n; p ++) {for (q = 0; q <n; q ++) {if (q) printf (""); printf ("% d", Edge [p] [q]);} puts ("") ;}} else puts ("NO"); if (t) puts ("");} return 0 ;}