Zju1311 network-undirected graph cut point

Source: Internet
Author: User

Question:

Returns an undirected graph and calculates the number of cut points in the graph. (If vertex I and its adjacent edges are removed, the remaining graph is not connected, Then I is the cut point)

Analysis:

There is a mature DFS Algorithm for Finding cut points. One is written as a backup template.

 

  1. /*
  2. Zju1311 Network
  3. */
  4. # Include <stdio. h>
  5. # Include <memory. h>
  6. # Define CLR (a) memset (A, 0, sizeof ())
  7. # Define min (A, B) (a)> (B )? (B) ())
  8. # Define N 105
  9. Int N;
  10. Int a [n] [N];
  11. Int cut [N];
  12. Char * Next (char STR [], char * SP ){
  13. While (* sp & * SP! = '') SP ++;
  14. While (* sp & * sp = '') SP ++;
  15. Return sp;
  16. }
  17. /**************************************/
  18. Int ancestor [N];
  19. Int mark [N];
  20. Int deep [N];
  21. Int DFS (int I, int father, int dth, int B [] [N], int cut []) {
  22. Int j, k, sons = 0, count = 0;
  23. Mark [I] = 1;
  24. Deep [I] = ancestor [I] = dth;
  25. For (k = B [I] [0]; k> = 1; k --){
  26. J = B [I] [k];
  27. If (j! = Father & mark [j] = 1) ancestor [I] = MIN (ancestor [I], deep [j]);
  28. If (mark [j] = 0 ){
  29. Count + = DFS (j, I, dth + 1, B, cut );
  30. Sons ++;
  31. Ancestor [I] = min (ancestor [I], ancestor [J]);
  32. If (Father =-1 & Sons> 1) | (father! =-1 & ancestor [J]> = deep [I])
  33. Cut [I] = 1;
  34. // If (ancestor [J]> deep [I]) brige [I] [J] = 1;
  35. }
  36. }
  37. Mark [I] = 2;
  38. Return count + cut [I];
  39. }
  40. /*
  41. Cut Point
  42. Parameter: A [] [] adjacent matrix. Non-0 represents connectivity. N.
  43. Return: Number of cut points. Cut [I] = 1 indicates that I is a cut point.
  44. Note: The DFS algorithm first converts the adjacent matrix into an adjacent table to handle the situation where the graph itself is not connected. Complexity O (E)
  45. The bridge where the image can be modified.
  46. */
  47. Int cutpoints (int A [] [N], int N, int cut []) {
  48. Int I, j, B [N] [N], Count = 0;
  49. For (I = 0; I <n; I ++) Cut [I] = B [I] [0] = 0;
  50. Memset (mark, 0, sizeof (Mark ));
  51. For (I = 0; I <n; I ++) for (j = 0; j <n; j ++) if (a [I] [J]) B [I] [++ B [I] [0] = J;
  52. For (I = 0; I <n; I ++) if (MARK [I] = 0) Count + = DFS (0,-1, 0, B, cut );
  53. Return count;
  54. }
  55. /**************************************/
  56. Int main ()
  57. {
  58. Int I, J, K;
  59. Char STR [N * 10], * sp;
  60. While (scanf ("% d", & N )! = EOF & N ){
  61. // Init
  62. CLR ();
  63. // Input
  64. While (scanf ("% d", & I), I ){
  65. Gets (STR );
  66. For (sp = next (STR, STR); sscanf (SP, "% d", & J )! = EOF; SP = next (STR, SP )){
  67. A [I-1] [J-1] = A [J-1] [I-1] = 1;
  68. }
  69. }
  70. // Work
  71. Printf ("% d/N", cutpoints (A, N, cut ));
  72. }
  73. Return 0;
  74. }

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.