PKU 1659 frogs 'neighborhood-Greedy Havel Theorem

Source: Internet
Author: User

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

  1. /*
  2. Pku1659 frogs 'neighborhood
  3. */
  4. # Include <stdio. h>
  5. # Include <stdlib. h>
  6. # Include <memory. h>
  7. # Define CLR (a) memset (A, 0, sizeof ())
  8. # Define n 15
  9. Int d [N], idx [N];
  10. Int CMP (const void * a, const void * B ){
  11. Return d [* (int *) B]-d [* (int *) A];
  12. }
  13. Int main ()
  14. {
  15. Int I, J, K, R, N, T;
  16. Int a [n] [N], flag;
  17. Scanf ("% d", & T );
  18. While (t --){
  19. Scanf ("% d", & N );
  20. For (I = 0; I <n; I ++) {scanf ("% d", & D [I]); idx [I] = I ;}
  21. CLR ();
  22. Flag = 1;
  23. // Work
  24. For (k = 0; k <n & flag; k ++ ){
  25. Qsort (idx + k, n-k, sizeof (INT), CMP );
  26. I = idx [k];
  27. If (d [I]> n-k-1) Flag = 0;
  28. For (r = 1; r <= d [I] & flag; r ++ ){
  29. J = idx [K + R];
  30. If (d [J] <= 0) Flag = 0;
  31. D [J] --;
  32. A [I] [J] = A [J] [I] = 1;
  33. }
  34. }
  35. // Output
  36. If (FLAG ){
  37. Puts ("yes ");
  38. For (I = 0; I <n; I ++ ){
  39. For (j = 0; j <n; j ++ ){
  40. If (j) printf ("");
  41. Printf ("% d", a [I] [J]);
  42. }
  43. Puts ("");
  44. }
  45. }
  46. Else puts ("no ");
  47. If (t) puts ("");
  48. }
  49. Return 0;
  50. }

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.