PKU 1236 network of schools-minimum vertex Base

Source: Internet
Author: User

Question:

There are some one-way network connections between n colleges (n <100). When a software is released, when I receive the software, it can send the software to all the schools it is linked. Now I want to release a software, which should be sent to at least a number of schools so that all schools can receive the software (question ). How many one-way network connections need to be added at least so that the software can be sent to any school, so that all schools can receive (Problem B ).

Analysis:

Let's first discuss question. This problem was introduced in the OI graph theory book of Wu wenhu, called directed graph.Minimum vertex Base.

First, find the Directed GraphExtremely Large connected componentAny school in the same strongly connected component receives software, and all schools in the whole strongly connected component can receive the software. Set each Strongly Connected ComponentScale to a pointTo form a new directed acyclic graph. When the Strongly Connected Component I receives the software, all the strongly connected components that I can reach can receive the software.

We call the strongly connected component with an inbound value of 0Maximum Connectivity component. Obviously, each maximum-strength connected component must be independently sent by the software, and other strongly connected components can be reached by the maximum-strength connected component. So,The maximum number of connected components is the solution of problem..

As for question B, I guess it isMax (number of points with an inbound value of 0 and number of points with an outbound value of 0 ),I did not expect it to be correct, and I did not prove it carefully. Note that when the source image has only one strongly connected component, the answer to Question B is 0.

 

  1. /*
  2. Pku1236 network of schools
  3. */
  4. # Include <stdio. h>
  5. # Include <stdlib. h>
  6. # Include <memory. h>
  7. # Define CLR (a) memset (A, 0, sizeof ())
  8. # Define max (A, B) (a)> (B )? (A) (B ))
  9. # Define N 105
  10. # Define M 10005
  11. Typedef struct nodestr {
  12. Int J;
  13. Struct nodestr * next;
  14. } Node;
  15. Node mem [M * 2];
  16. Int memp;
  17. Void addedge (node * E [], int I, Int J ){
  18. Node * P = & mem [memp ++];
  19. P-> next = E [I]; P-> J = J; E [I] = P;
  20. }
  21. Int g_dfs_first;
  22. Void dfs_conn (node * E [], int I, int mark [], int f [], int * NF ){
  23. Int J; node * P;
  24. If (MARK [I]) return; else mark [I] = 1;
  25. If (! G_dfs_first) f [I] = * NF; // reverse search to obtain the connected component number
  26. For (P = E [I]; P! = NULL; P = p-> next) dfs_conn (E, P-> J, Mark, F, NF );
  27. If (g_dfs_first) f [(* NF) ++] = I; // forward search, get the timestamp
  28. }
  29. Int connection (node * E [], int N, int CON []) {
  30. Int I, J, K, Mark [N], ncon, time [N], ntime; // time [I] indicates the node whose time stamp is I
  31. Node * P, * re [N]; // reverse edge
  32. CLR (re); // construct the reverse edge adjacent table
  33. For (I = 0; I <n; I ++) for (P = E [I]; P! = NULL; P = p-> next) addedge (Re, p-> J, I );
  34. G_dfs_first = 1; // returns the timestamp to the forward DFS.
  35. CLR (Mark); CLR (time); ntime = 0;
  36. For (I = 0; I <n; I ++) if (! Mark [I]) dfs_conn (E, I, Mark, time, & ntime );
  37. G_dfs_first = 0; // reverse DFS to obtain strongly connected components
  38. CLR (Mark); CLR (CON); ncon = 0;
  39. For (I = n-1; I> = 0; I --) if (! Mark [time [I])
  40. {Dfs_conn (Re, time [I], Mark, Con, & ncon); ncon ++ ;}
  41. Return ncon;
  42. }
  43. /*
  44. Shrink strongly connected components
  45. Parameters:
  46. E [] directed graph neighbor table. returns the number of strongly connected components M.
  47. CE [] returns the directed graph neighbor table [0 M-1] After the strongly connected component is reduced.
  48. CON [] returns the ID of the strongly connected component to which vertex I belongs. CON [I]
  49. */
  50. Int shrinkconnection (node * E [], int N, node * CE [], int CON []) {
  51. Int I, J, K, M; node * P, * q;
  52. M = Connection (E, N, con );
  53. For (I = 0; I <m; I ++) Ce [I] = NULL;
  54. For (k = 0; k <n; k ++ ){
  55. For (I = CON [K], P = E [k]; P! = NULL; P = p-> next ){
  56. For (j = CON [p-> J], q = Ce [I]; Q! = NULL; q = Q-> next)
  57. If (Q-> J = J) break;
  58. If (q = NULL & I! = J) addedge (CE, I, j );
  59. }
  60. }
  61. Return m;
  62. }
  63. /*************************************** **/
  64. Int main ()
  65. {
  66. Int I, J, K;
  67. Int n, m, CON [N], dout [N], DIN [N];
  68. Node * E [N], * CE [N], * P;
  69. Int ANSA, ansb;
  70. While (scanf ("% d", & N )! = EOF ){
  71. // Init
  72. Memp = 0;
  73. CLR (E );
  74. // Input
  75. For (I = 0; I <n; I ++ ){
  76. While (scanf ("% d", & J), J)
  77. Addedge (E, I, J-1 );
  78. }
  79. // Shrinkconnection
  80. M = shrinkconnection (E, N, Ce, con );
  81. // Work d []-ce
  82. CLR (DIN); CLR (dout );
  83. For (I = 0; I <m; I ++ ){
  84. For (P = Ce [I]; P! = NULL; P = p-> next)
  85. Din [p-> J] ++, dout [I] ++;
  86. }
  87. // Get ans
  88. ANSA = ansb = 0;
  89. For (I = 0; I <m; I ++ ){
  90. If (DIN [I] = 0) ANSA ++;
  91. If (dout [I] = 0) ansb ++;
  92. }
  93. Ansb = max (ANSA, ansb );
  94. If (M = 1) ansb = 0;
  95. Printf ("% d/n % d/N", ANSA, ansb );
  96. }
  97. Return 0;
  98. }

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.