tarjian強聯通分量(模板)

來源:互聯網
上載者:User

標籤:強聯通   拓展   names   log   模板   color   cst   入棧   else   

來,水水模板吧。。。。。

#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#define N 10000using namespace std;int x,y,n,m,t,tot,sum,top,time;int head[N],col[N],stack[N],dfn[N],low[N],a[N][N];bool vis[N];struct Edge{    int from,next,to; }edge[N];int add(int x,int y){    tot++;    edge[tot].to=y;    edge[tot].next=head[x];    head[x]=tot;}int read(){    int x=0,f=1; char ch=getchar();    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}    return f*x;}int tarjian(int now){    t=0;    dfn[now]=low[now]=++time;//初始每一個點的low值dfn等於它的時間戳記     stack[++top]=now; vis[now]=true;//將該點入棧,標記為在棧中     for(int i=head[now];i;i=edge[i].next)//更新於他相連的點的low值     {        x=edge[i].to;        if(vis[x]) low[i]=min(dfn[x],low[i]);//如果該點已經在棧中         else if(!dfn[x])        {            tarjian(x);            low[i]=min(low[x],low[i]);//從該點繼續拓展         }    }    if(low[now]==dfn[now])//說明以這個點結束強連通分量     {        sum++;// 強連通分量的個數加一         col[now]=sum;//將該點放在她所屬的強連通分量了         for(;stack[top]!=now;top--)        {            col[stack[top]]=sum;            vis[stack[top]]=false;        }        vis[now]=false;        top--;     } }int main(){    n=read(),m=read();    for(int i=1;i<=m;i++)     {         x=read();y=read();         add(x,y);     }    for(int i=1;i<=n;i++)      if(!dfn[i]) tarjian(i);    printf("%d",sum);    return 0;}

 

tarjian強聯通分量(模板)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.