tarjan算法——边双连通分量

简介:还是采用tarjan缩点,如果边(u,v)是桥,那么显然dfn[v]=low[v],则v此时栈中的集合为一个边连通分量。

代码:

 1 void tarjan(int x,int &sccnum,int from) {
 2     sta[++cnt]=x;
 3     vis[x]=1;
 4     dfn[x]=++lay;
 5     low[x]=lay;
 6     bool flag=false;
 7     for(int i=head[x];~i;i=e[i].net) {
 8         int v=e[i].v;
 9         if(v==from&&!flag) {    ///如果是重边的情况
10             flag=true;
11             continue;
12         }
13         if(!dfn[v]) {
14             tarjan(v,sccnum,x);
15             low[x]=min(low[x],low[v]);
16         }
17         else if(vis[v]==1)
18             low[x]=min(low[x],low[v]);
19     }
20     if(dfn[x]==low[x]) { ///有割边
21         ++sccnum;
22         do {
23             f[sta[cnt]]=sccnum; ///标记为同一集合
24             vis[sta[cnt]]=2;
25         }while(sta[cnt--]!=x);
26     }
27 }
View Code

猜你喜欢

转载自www.cnblogs.com/wuliking/p/11511602.html
今日推荐