Strong connected Component Tarjan algorithm

Source: Internet
Author: User
Tags min

O (V+e)

The usual Tarjan notation is a dfn[] array followed by a instack[] array, and I've streamlined the code to delete both of these arrays, replacing them with simpler notation, and saving space.


int LOW[MAXN];           Record the earliest ancestors the tree can reach (not necessarily the earliest, but not the use) int time;            timestamp int num;   Number of connected components int BELONG[MAXN];      The record belongs to which connected component int STA[MAXN];            handwritten stack int top;
    Stack top void init () {num = time = top = 0;
    memset (low,-1,sizeof (Low));
Memset (Belong,-1,sizeof (belong));      } void Tarjan (int p) {int t=time;
    Record the time stamp when accessing this point, do not DFN array low[p]=time++;
    Sta[top++]=p;
        for (int i=v[p].size () -1;i>=0;i--) {int next=v[p][i];
            if (low[next]==-1) {//has not been visited Tarjan (next);
        Low[p]=min (Low[p],low[next]); } else if (belong[next]==-1)//is still in the stack (as long as the stack is out, the value of belong is definitely determined, this way of judging the instack array is saved) Low[p]=min (Low[p],low
    [Next]);
            } if (low[p]==t) {while (1) {int i=sta[--top]; Belong[i]=num;
        The connected component number starts with 0 if (i==p) break;
    } num++;
    }} void Solve () {init ();
           for (int i=1;i<=n;i++) if (low[i]==-1) Tarjan (i); }


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.