PKU 2553 the bottom of a graph-extremely large Strongly Connected Component

Source: Internet
Author: User

Pku2553 zju1979 the bottom of a graph

Description:

A directed graph is provided, which defines that if all vertices {wi} that can be reached by node v can both reach V, node v is called sink. All sink points are required.

Analysis:

Similar to such a question,Some members of the football team can contact other members and ask the coach how many members should be notified, so that all players can receive a training notification.

This classic topic is explained in Wu wenhu's graph theory book.Minimum vertex base of a Directed Graph. The method is as follows:

First, divide the directed graph into severalExtremely Large connected component. For each strongly connected component CI, you only need to notify one person. These strongly connected components are either independent of each other, or one-way edge is directed to CJ from the connected component CI to form a forest. For each tree, you only need to notify any one of the Root Node Croot. The entire tree can be notified. Then, you only need to count the number of connected components whose input degree is 0.

 

Now let's go back to this question. Similarly, what the question requires isAll nodes in the connected component with an outbound degree of 0. Output by serial number.

The key to this question isStrongly Connected Component of a Directed Graph. The subsequent work is very simple.

In addition, I'm glad that zju has won the first place in this question :)~

Submit time language run time (MS) Run memory (Kb) User Name
21:53:04 C ++ 30 628 Tiaotiao

  1. /*
  2. PKU2553 The Bottom of a Graph
  3. */
  4. # Include <stdio. h>
  5. # Include <memory. h>
  6. # Define clr (a) memset (a, 0, sizeof ())
  7. # Define n 5005
  8. # Define M 50000
  9. Typedef struct strnode {
  10. Int J;
  11. Struct strnode * next;
  12. } Node;
  13. Node mem [m];
  14. Int memp;
  15. Void addedge (node * E [], int I, Int J ){
  16. Node * P = & mem [memp ++];
  17. P-> J = J;
  18. P-> next = E [I];
  19. 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;
  25. Mark [I] = 1;
  26. If (! G_dfs_first) f [I] = * NF; // reverse search to obtain the connected component number
  27. For (P = E [I]; P! = NULL; P = p-> next ){
  28. J = p-> J;
  29. Dfs_conn (E, J, Mark, F, NF );
  30. }
  31. If (g_dfs_first) f [(* NF) ++] = I; // forward search, get the timestamp
  32. }
  33. /*
  34. Digraph extremely large Strongly Connected Component
  35. Parameters:
  36. Join Table E [], number of nodes N. Returns the number of strongly connected branches (ncon.
  37. Return CON [I], which indicates the ID of the strongly connected component to which node I belongs, 0 ~ Ncon-1.
  38. */
  39. Int connection (node * E [], int N, int CON []) {
  40. Int I, J, K, Mark [N], ncon;
  41. Int time [N], ntime; // time [I] indicates the node whose time stamp is I
  42. Node * P, * re [N]; // reverse edge
  43. // Construct the reverse edge joining table
  44. CLR (re );
  45. For (I = 0; I <n; I ++ ){
  46. For (P = E [I]; P! = NULL; P = p-> next)
  47. Addedge (Re, p-> J, I );
  48. }
  49. // Forward to DFS to obtain the timestamp
  50. G_dfs_first = 1;
  51. CLR (Mark); CLR (time); ntime = 0;
  52. For (I = 0; I <n; I ++ ){
  53. If (! Mark [I])
  54. Dfs_conn (E, I, Mark, time, & ntime );
  55. }
  56. // Reverse DFS to obtain strongly connected components
  57. G_dfs_first = 0;
  58. CLR (Mark); CLR (CON); ncon = 0;
  59. For (I = n-1; I> = 0; I --){
  60. If (! Mark [time [I]) {
  61. Dfs_conn (Re, time [I], Mark, Con, & ncon );
  62. Ncon ++;
  63. }
  64. }
  65. }
  66. // Vars
  67. Node * E [N];
  68. Int CON [N], ncon;
  69. Int n, m;
  70. Int d [N];
  71. Int main ()
  72. {
  73. Int I, J, K, X, Y;
  74. Node * P;
  75. While (scanf ("% d", & N )! = EOF & N ){
  76. // Init
  77. Memp = 0; CLR (E );
  78. // Input
  79. Scanf ("% d", & M );
  80. For (k = 0; k <m; k ++ ){
  81. Scanf ("% d", & I, & J); I --; j --;
  82. Addedge (E, I, j );
  83. }
  84. // Connection
  85. Ncon = Connection (E, N, con );
  86. // Work d []
  87. CLR (d );
  88. For (I = 0; I <n; I ++ ){
  89. X = CON [I];
  90. For (P = E [I]; P! = NULL; P = p-> next ){
  91. Y = CON [p-> J];
  92. If (X! = Y) d [x] ++;
  93. }
  94. }
  95. // Output
  96. J = 0;
  97. For (I = 0; I <n; I ++ ){
  98. K = CON [I];
  99. If (d [k] = 0 ){
  100. If (j) printf (""); j = 1;
  101. Printf ("% d", I + 1 );
  102. }
  103. }
  104. Puts ("");
  105. }
  106. Return 0;
  107. }

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.