ZOJ_3795 Grouping (strongly connected component topology)

Source: Internet
Author: User

ZOJ_3795 Grouping (strongly connected component topology)


Question:
This is my first strongly connected component. Although I have referenced other people's code, it is still very rewarding. The search and processing of strongly connected components is the first step of many graph topics, so it is very important. This is only the processing of Strongly Connected digraphs.
Because the relationship between each group of questions is not less than, there is only one case if there is a ring, that is, everyone on the ring is of the same age, then, the comparison relationships of all people in this ring can be equivalent. This is why we need to first perform point reduction on the strongly connected component and take each strongly connected component as a whole, then, the topological order is constructed based on the hierarchical relationship, and the longest sequence is found using the topological order.
Algorithm explanation
Code reference
Test data:

4
1 2
2 3
3 4
4 1

5 3
1 2
2 3
3 4

9 9
5 4
4 1
1 3
3 2
2 1
1 6
6 7
7 8
8 9

5 5
1 2
2 3
3 4
4 1
5 1

Code implementation:

# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
        # Include
       
         # Define MAX_N 100010 # define MAX_M 300010 using namespace std; struct E {int from, to, next; bool sign ;}; int N, M; int result; int edgenum; // Number of edges int top, cnt, Time; // stack top, number of strongly connected components, timeline E edge [MAX_M]; // edge set int dis [MAX_N]; // The weight of the node in the topological order int head [MAX_N]; // The forward star storage head array int belong [MAX_N]; // records which strongly connected component a vertex belongs to. int indegree [MAX_N]; // records the bool instack [MAX_N] of the strongly connected component in the topological order. // whether the node is in the stack int DFN [MAX_N]; // The node u search order number (timestamp) int Low [MAX_N]; // The subtree of node u or u can trace the number of the node in the earliest Stack. int Stack [MAX_N]; // handwritten Stack vector
        
          G [MAX_N]; // stores the topological order vector.
         
           Bcnt [MAX_N]; // store each strongly connected component void add (int u, int v); // add the edge void tarjan (int u) to the star ); // search for the strongly connected component void tarjan_ini (int all); void suodian (); // contraction function, which constructs the topological order int bfs () according to each strongly connected component; int main () {// freopen ("in.txt", "r", stdin); while (scanf ("% d", & N, & M )! = EOF) {int a, B; edgenum = 0; memset (head,-1, sizeof (head); while (M --) {scanf ("% d", & a, & B); add (a, B) ;}tarjan_ini (N); suodian (); result = bfs (); printf ("% d \ n", result);} return 0;} void tarjan_ini (int all) {memset (DFN,-1, sizeof (DFN )); // initialize the top three pointers = Time = cnt = 0; for (int I = 1; I <= all; I ++) {// search for the strongly connected component if (DFN [I] =-1) {tarjan (I) ;}} of each vertex that has not been traversed );}}} // Recursively search for the strongly connected component void tarjan (int u) {// mark the timestamp DFN [U] = Low [u] = ++ Time; // Stack entry. Note that the front + Stack [++ top] = u; instack [u] = true; // search for all adjacent edges with u and find the strongly connected component for (int I = head [u]; I! =-1; I = edge [I]. next) {E tmp = edge [I]; // if (DFN [tmp. to] =-1) {tarjan (tmp. to); // The earliest stack node that the u or u subtree can trace to: Low [u] = min (Low [u], Low [tmp. to]);} // The accessed else if (instack [tmp. to]) {Low [u] = min (Low [u], DFN [tmp. to]);} // u is a strongly connected component node with an elastic stack. All elements are stored in a vector Array if (DFN [u] = Low [u]) {int now; cnt ++; bcnt [cnt]. clear (); do {// pop-up Stack. Pay attention to the Post -- now = Stack [top --]; // mark which strongly connected component belongs to belong [now] = cnt; instac K [now] = false; bcnt [cnt]. push_back (now);} while (now! = U); // pop to the root node deadline} void suodian () {for (int I = 1; I <= cnt; I ++) {G [I]. clear (); indegree [I] = 0;} // construct the topological order for (int I = 0; I <edgenum; I ++) {int u, v; u = belong [edge [I]. from]; v = belong [edge [I]. to]; if (u! = V) {indegree [v] ++; G [u]. push_back (v) ;}} void add (int u, int v) {E tmp = {u, v, head [u], false }; edge [edgenum] = tmp; head [u] = edgenum ++;} int bfs () {int tmp; queue
          
            Q; for (int I = 1; I <= cnt; I ++) {if (indegree [I] = 0) {Q. push (I); // The initial weight is the size dis [I] = bcnt [I] of the strongly connected component. size () ;}else {dis [I] = 0 ;}} while (! Q. empty () {int u = Q. front (); Q. pop (); int len = G [u]. size (); for (int I = 0; I <len; I ++) {int v = G [u] [I]; // overlay and find the maximum weight dis [v] = max (dis [v], (int) bcnt [v]. size () + dis [u]); // If the graph is a strongly connected graph, it will not be updated here // tmp = max (tmp, dis [v]); indegree [v] --; if (indegree [v] = 0) {Q. push (v) ;}}// the result tmp = 1 is displayed. for (int I = 1; I <= cnt; I ++) {tmp = max (tmp, dis [I]);} return tmp ;}
          
         
        
       
      
     
    
   
  

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.