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
- /*
- PKU2553 The Bottom of a Graph
- */
- # Include <stdio. h>
- # Include <memory. h>
- # Define clr (a) memset (a, 0, sizeof ())
- # Define n 5005
- # Define M 50000
- Typedef struct strnode {
- Int J;
- Struct strnode * next;
- } Node;
- Node mem [m];
- Int memp;
- Void addedge (node * E [], int I, Int J ){
- Node * P = & mem [memp ++];
- P-> J = J;
- P-> next = E [I];
- 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;
- 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 ){
- J = p-> J;
- Dfs_conn (E, J, Mark, F, NF );
- }
- If (g_dfs_first) f [(* NF) ++] = I; // forward search, get the timestamp
- }
- /*
- Digraph extremely large Strongly Connected Component
- Parameters:
- Join Table E [], number of nodes N. Returns the number of strongly connected branches (ncon.
- Return CON [I], which indicates the ID of the strongly connected component to which node I belongs, 0 ~ Ncon-1.
- */
- Int connection (node * E [], int N, int CON []) {
- Int I, J, K, Mark [N], ncon;
- Int time [N], ntime; // time [I] indicates the node whose time stamp is I
- Node * P, * re [N]; // reverse edge
- // Construct the reverse edge joining table
- CLR (re );
- For (I = 0; I <n; I ++ ){
- For (P = E [I]; P! = NULL; P = p-> next)
- Addedge (Re, p-> J, I );
- }
- // Forward to DFS to obtain the timestamp
- G_dfs_first = 1;
- CLR (Mark); CLR (time); ntime = 0;
- For (I = 0; I <n; I ++ ){
- If (! Mark [I])
- Dfs_conn (E, I, Mark, time, & ntime );
- }
- // Reverse DFS to obtain strongly connected components
- G_dfs_first = 0;
- 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 ++;
- }
- }
- }
- // Vars
- Node * E [N];
- Int CON [N], ncon;
- Int n, m;
- Int d [N];
- Int main ()
- {
- Int I, J, K, X, Y;
- Node * P;
- While (scanf ("% d", & N )! = EOF & N ){
- // Init
- Memp = 0; CLR (E );
- // Input
- Scanf ("% d", & M );
- For (k = 0; k <m; k ++ ){
- Scanf ("% d", & I, & J); I --; j --;
- Addedge (E, I, j );
- }
- // Connection
- Ncon = Connection (E, N, con );
- // Work d []
- CLR (d );
- For (I = 0; I <n; I ++ ){
- X = CON [I];
- For (P = E [I]; P! = NULL; P = p-> next ){
- Y = CON [p-> J];
- If (X! = Y) d [x] ++;
- }
- }
- // Output
- J = 0;
- For (I = 0; I <n; I ++ ){
- K = CON [I];
- If (d [k] = 0 ){
- If (j) printf (""); j = 1;
- Printf ("% d", I + 1 );
- }
- }
- Puts ("");
- }
- Return 0;
- }