【省选】算法总结——强连通

本文介绍使用Tarjan算法寻找图中的强连通分量,并通过几个实例问题展示了该算法的应用。同时分享了作者在实现过程中的经验和教训。

强连通

强连通这东西我也只写了几回,主要是在我现在看来他除了找环缩点而外好像也没什么用处了

 

具体不想证明了,放上tarjan代码

void dfs(int x)

{

         now[x]=low[x]=++dfstime;

         hash[x]=true;st.push(x);inst[x]=true;

         for(int i=1;i<=n;i++) if(map[x][i])

         {

                   if(!hash[i])

                   {

                            dfs(i);

                            low[x]=min(low[x],low[i]);

                   }

                   else if(inst[i]) low[x]=min(low[x],now[i]);

         }

         if(low[x]==now[x])

         {

                   while(!st.empty())

                   {

                            int u=st.top();st.pop();inst[u]=false;

                            belong[u]=number;

                            if(u==x) break;

                   }

                   number++;

         }

}

void tarjan()

{

         for(int i=1;i<=n;i++) if(!hash[i]) dfs(i);

         if(!st.empty())//栈中剩下的又是一个强连通分量

         {

                   while(!st.empty())

                   {

                            int u=st.top();st.pop();

                            belong[u]=number;

                   }

                   number++;

         }

}

 

说实话,基本上我写了几次强连通,几次都写错了,后来查错才改对的

 

 

 

我做的强连通题目:

Popular Cows POJ2186 http://blog.csdn.net/jiangzh7/article/details/8630500

Network of Schools POJ1236 http://blog.csdn.net/jiangzh7/article/details/8639709

迷宫城堡 HDU 1269http://blog.csdn.net/jiangzh7/article/details/8639858

Victoria的舞会2 -- Vijos 1022,1023 http://blog.csdn.net/jiangzh7/article/details/8644023

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值