其他分享
首页 > 其他分享> > 强连通缩点

强连通缩点

作者:互联网

const int MAXN = 8001, MAXM = 8001;
int head[MAXN], next[MAXM], last[MAXM], lineNum = 0;
void add(int x,int y){
    lineNum++, next[lineNum] = head[x], last[lineNum] = y, head[x] = lineNum;
}
vector<int> own[MAXN];
int vis[MAXN], bel[MAXN], st[MAXN], dfn[MAXN], low[MAXN], deep = 0, cnt = 0;
void tarjan(int x){
    vis[x] = 1;
    low[x] = dfn[x] = ++deep;
    st[++st[0]] = x;
    for(int l=head[x]; l; l=next[l]){
        int y = last[l];
        if(vis[y] == 0)
            tarjan(y), low[x] = min(low[x], low[y]);
        else if(vis[y] == 1)
            low[x] = min(low[x], dfn[y]);
    }
    if(dfn[x] == low[x]){
        ++cnt;
        do{
            bel[st[st[0]]] = cnt;
            own[cnt].push_back(st[st[0]]);
            vis[st[st[0]]] = 2;
        }while(st[st[0]--] != x);
    }
}

 

标签:缩点,连通,lineNum,int,st,vis,MAXN,low
来源: https://www.cnblogs.com/PHDHD/p/12234075.html