Theme:
The vertex degree sequence {DN} of an undirected graph is given, and it is required to determine whether a simple undirected graph can be constructed. If any output adjacent matrix can be constructed.
Analysis:
This yearHarbin DivisionThere is also a same question, and only the judgment is not required to be constructed. At that time, we were doing it with greed, but we didn't know why it was correct at the end ......
GreedyThe method is to increase the vertex size from large to small each time.SortTo retrieve the vertices vi with the highest degree, connect them to those vertices with the highest degree, and subtract the degree of VJ. After the connection is complete, the VI will not be considered, and the remaining points will be sorted again and then connected with the highest degree ...... In this way, a feasible solution can be constructed.
There are two points for determining that there is no solution. If a selected VI has more than the remaining vertices, there is no solution. If the degree of a VJ is reduced to a negative number, there is no solution.
What isHavel TheoremThe above construction process is ^
For more information, see here http://roba.yo2.cn/archives/439802
- /*
- Pku1659 frogs 'neighborhood
- */
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <memory. h>
- # Define CLR (a) memset (A, 0, sizeof ())
- # Define n 15
- Int d [N], idx [N];
- Int CMP (const void * a, const void * B ){
- Return d [* (int *) B]-d [* (int *) A];
- }
- Int main ()
- {
- Int I, J, K, R, N, T;
- Int a [n] [N], flag;
- Scanf ("% d", & T );
- While (t --){
- Scanf ("% d", & N );
- For (I = 0; I <n; I ++) {scanf ("% d", & D [I]); idx [I] = I ;}
- CLR ();
- Flag = 1;
- // Work
- For (k = 0; k <n & flag; k ++ ){
- Qsort (idx + k, n-k, sizeof (INT), CMP );
- I = idx [k];
- If (d [I]> n-k-1) Flag = 0;
- For (r = 1; r <= d [I] & flag; r ++ ){
- J = idx [K + R];
- If (d [J] <= 0) Flag = 0;
- D [J] --;
- A [I] [J] = A [J] [I] = 1;
- }
- }
- // Output
- If (FLAG ){
- Puts ("yes ");
- For (I = 0; I <n; I ++ ){
- For (j = 0; j <n; j ++ ){
- If (j) printf ("");
- Printf ("% d", a [I] [J]);
- }
- Puts ("");
- }
- }
- Else puts ("no ");
- If (t) puts ("");
- }
- Return 0;
- }