[模板]tarjan求强连通分量

大约是今年4月学的算法了,后来5月的时候做题还写了一个退化的tarjanQAQ

时间复杂度:O(n+m)

用途:有向图缩环

 1 #include<set> 
 2 #include<cmath>
 3 #include<ctime>
 4 #include<queue>
 5 #include<stack>
 6 #include<cstdio>
 7 #include<vector>
 8 #include<cstring>
 9 #include<cstdlib>
10 #include<iostream>
11 #include<algorithm>
12 #define N 10001
13 #define M 100001
14 using namespace std;
15 struct graph{
16     int nxt,to;
17 }e[M];
18 int g[N],f[N]/*father*/,dfn[N],low[N],sta[N]/*stack;stack[0]:the top of the stack*/,m,n,cnt;
19 /*dfn[u]:遍历到u点的时间; low[u]:u点可到达的各点中最小的dfn[v],即最高层的点*/
20 bool ins[N];//in stack
21 inline void addedge(int x,int y){
22     e[++cnt].nxt=g[x];g[x]=cnt;e[cnt].to=y;
23 }
24 inline void tarjan(int u){
25     dfn[u]=low[u]=++cnt;
26     sta[++sta[0]]=u;ins[u]=true;
27     for(int i=g[u];i;i=e[i].nxt)
28         if(!dfn[e[i].to]){
29             tarjan(e[i].to);
30             low[u]=min(low[u],low[e[i].to]);
31         }
32         else if(ins[e[i].to])
33             low[u]=min(low[u],low[e[i].to]);
34     if(dfn[u]==low[u]){
35         while(sta[sta[0]+1]!=u){
36             f[sta[sta[0]]]=u;
37             ins[sta[sta[0]--]]=false;
38         }
39     }
40 }
41 inline void init(){
42     scanf("%d%d",&n,&m);
43     for(int i=1,j,k;i<=m;i++){
44         scanf("%d%d",&j,&k);
45         addedge(j,k);
46     }
47     cnt=0;
48     for(int i=1;i<=n;i++)
49         if(!dfn[i]) tarjan(i);
50     for(int i=1;i<=n;i++)
51         printf("%d's father is %d\n",i,f[i]);
52 }
53 int main(){
54     freopen("tarjan.in","r",stdin);
55     freopen("tarjan.out","w",stdout);
56     init();
57     fclose(stdin);
58     fclose(stdout);
59     return 0;
60 }

 

转载于:https://www.cnblogs.com/AireenYe/p/5653717.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值