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.
- /*
- Pku1236 network of schools
- */
- # Include <stdio. h>
- # Include <stdlib. h>
- # Include <memory. h>
- # Define CLR (a) memset (A, 0, sizeof ())
- # Define max (A, B) (a)> (B )? (A) (B ))
- # Define N 105
- # Define M 10005
- Typedef struct nodestr {
- Int J;
- Struct nodestr * next;
- } Node;
- Node mem [M * 2];
- Int memp;
- Void addedge (node * E [], int I, Int J ){
- Node * P = & mem [memp ++];
- P-> next = E [I]; P-> J = J; E [I] = P;
- }
- Int g_dfs_first;
- Void dfs_conn (node * E [], int I, int mark [], int f [], int * NF ){
- Int J; node * P;
- If (MARK [I]) return; else mark [I] = 1;
- If (! G_dfs_first) f [I] = * NF; // reverse search to obtain the connected component number
- For (P = E [I]; P! = NULL; P = p-> next) dfs_conn (E, P-> J, Mark, F, NF );
- If (g_dfs_first) f [(* NF) ++] = I; // forward search, get the timestamp
- }
- Int connection (node * E [], int N, int CON []) {
- Int I, J, K, Mark [N], ncon, time [N], ntime; // time [I] indicates the node whose time stamp is I
- Node * P, * re [N]; // reverse edge
- CLR (re); // construct the reverse edge adjacent table
- For (I = 0; I <n; I ++) for (P = E [I]; P! = NULL; P = p-> next) addedge (Re, p-> J, I );
- G_dfs_first = 1; // returns the timestamp to the forward DFS.
- CLR (Mark); CLR (time); ntime = 0;
- For (I = 0; I <n; I ++) if (! Mark [I]) dfs_conn (E, I, Mark, time, & ntime );
- G_dfs_first = 0; // reverse DFS to obtain strongly connected components
- CLR (Mark); CLR (CON); ncon = 0;
- For (I = n-1; I> = 0; I --) if (! Mark [time [I])
- {Dfs_conn (Re, time [I], Mark, Con, & ncon); ncon ++ ;}
- Return ncon;
- }
- /*
- Shrink strongly connected components
- Parameters:
- E [] directed graph neighbor table. returns the number of strongly connected components M.
- CE [] returns the directed graph neighbor table [0 M-1] After the strongly connected component is reduced.
- CON [] returns the ID of the strongly connected component to which vertex I belongs. CON [I]
- */
- Int shrinkconnection (node * E [], int N, node * CE [], int CON []) {
- Int I, J, K, M; node * P, * q;
- M = Connection (E, N, con );
- For (I = 0; I <m; I ++) Ce [I] = NULL;
- For (k = 0; k <n; k ++ ){
- For (I = CON [K], P = E [k]; P! = NULL; P = p-> next ){
- For (j = CON [p-> J], q = Ce [I]; Q! = NULL; q = Q-> next)
- If (Q-> J = J) break;
- If (q = NULL & I! = J) addedge (CE, I, j );
- }
- }
- Return m;
- }
- /*************************************** **/
- Int main ()
- {
- Int I, J, K;
- Int n, m, CON [N], dout [N], DIN [N];
- Node * E [N], * CE [N], * P;
- Int ANSA, ansb;
- While (scanf ("% d", & N )! = EOF ){
- // Init
- Memp = 0;
- CLR (E );
- // Input
- For (I = 0; I <n; I ++ ){
- While (scanf ("% d", & J), J)
- Addedge (E, I, J-1 );
- }
- // Shrinkconnection
- M = shrinkconnection (E, N, Ce, con );
- // Work d []-ce
- CLR (DIN); CLR (dout );
- For (I = 0; I <m; I ++ ){
- For (P = Ce [I]; P! = NULL; P = p-> next)
- Din [p-> J] ++, dout [I] ++;
- }
- // Get ans
- ANSA = ansb = 0;
- For (I = 0; I <m; I ++ ){
- If (DIN [I] = 0) ANSA ++;
- If (dout [I] = 0) ansb ++;
- }
- Ansb = max (ANSA, ansb );
- If (M = 1) ansb = 0;
- Printf ("% d/n % d/N", ANSA, ansb );
- }
- Return 0;
- }