POJ1236 Network of Schools (strongly connected component, condensing point)

Original title Address

Spent the night watching karjan, Take This question is do some template title hand and thought of it, everything written comments related to the inside.

#include <the iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include < String .h> 
#include <Stack>
 the using  namespace STD;
 int n-;
 int CNT = 0 , C [ 110 ];   // record the number cnt strongly connected component, c [] which strongly connected components each recording points belonging 
int NUM = 0 , DFN [ 110 ], Low [ 110 ], INS [ 110 ]; // maintenance information tarjan used 
int DIN [ 110 ], DOUT [ 110 ]; // into and out of the condensing point of view of each point in the record
stack<int> sta;

struct LIST{
    int head[110],tot,to[10010],nxt[10010];
    
    void init()
    {
        memset(head,0,sizeof(head));
        memset(to,0,sizeof(to));
        memset(nxt,0,sizeof(nxt));
        tot=0;
    }
    
    void add(int u,int v)
    {
        to[TOT ++] = V; NXT [TOT] = head [U]; head [U] = TOT; 
    } 
} in Li1, LI2;   // two lists, before and after recording in FIG point reduction 

void Tarjan ( int U) 
{ 
    DFN [u] = low [u] NUM = ++;   // initialize dfn with low value of the point u dfn point of arrival time stamps, low set point value and the minimum u strongly connected stamp 
    sta.push (u); INS [u] = . 1 ; // to point stack, and record the state stack, the stack points are ancestors of u and u 
    for ( int I = li1.head [u]; I; I = li1.nxt [I]) 
    { 
        int v = [I] li1.to;
         IF (! DFN [v]) 
        { 
            Tarjan (v);   // if v has not been searched, the first process v
            low [U] = min (low [U], low [V]);   // After treatment v, updating the current low value of the point, because the direct ancestor back to point "to the side" v may be led Low [v] smaller 
        }
         the else  IF (INS [v]) Low [U] = min (Low [U], DFN [v]);   // if v has been searched, the stack and v, v is the apparent u ancestors, which of course directly dfn [v] value updating Low [u] 
    }
     IF (DFN [u] == Low [u])    // if the current point is equal to the timestamp of strongly connected components in which the minimum time poke 
    {                     // instructions to the stack from all points of the points are in a strongly connected components 
        CNT ++;            // from the stack to store all points unstack and recording information 
        the while ( . 1 ) 
        { 
            int X = sta.top (); INS [X] = 0 ; sta.pop (); 
            C [X] = CNT;     //Record number of strongly connected components of the point belongs, in preparation for condensing point 
            IF (X == U) BREAK ; 
        } 
    } 
} 

int main () 
{ 
    li1.init (); 
    li2.init (); 
    Scanf ( " % D " , & n-);
     for ( int U = . 1 , V; U <= n-; U ++ )
         for (Scanf ( " % D " , & V);! V = 0 ; Scanf ( " % D " , & V)) li1.add (U, V);
     for ( int I = . 1 ; I <= n-; I ++) IF (!DFN [I]) Tarjan (I);
     // start point reduction, new li2 in FIG described 
    for ( int U = . 1 ; U <= n-; U ++ )
         for ( int I = li1.head [U]; I; = I li1.nxt [I]) 
        { 
            int V = li1.to [I];
             IF (C [U] == C [V]) Continue ; 
            li2.add (C [U], C [V]); // If u and v are not the same strongly connected component, the establishment of this edge, vertex C [u] and C [v] 
            DOUT [C [u]] ++ ; 
            DIN [C [v]] ++; // statistical degree and out of each vertex 
        }
     // the following conclusions are solving the problem point 
    IF (CNT == . 1 )  // If this Tuqiang communication, then obviously the answer is 1 and 0 
    { 
        COUT << " 1 \ N0 \ n- " ;
         return  0 ; 
    } 
    // the statistics of the condensing point and each point of the
     // least to provide of course, the degree of vertex number 0
     // relationship is added max (DIN0, DOUT0)
     // as to satisfy the condition, it is necessary Tuqiang entire communication, can not have the degree or the degree of presence of the point 0
     / / to eliminate these points, it is necessary at least max (din0, dout0) directed edge section. 
    int ANS1 = 0 , ANS2 = 0 ;
     for ( int I = . 1 ; I <= CNT; I ++ ) 
    { 
        IF (DIN [I] == 0 ) ANS1 ++ ;
        if(dout[i]==0) ans2++;
    }
    cout<<ans1<<endl;
    cout<<max(ans1,ans2)<<endl; 
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/BakaCirno/p/11504084.html