码迷,mamicode.com
首页 > 其他好文 > 详细

Tarjan 模板

时间:2018-07-20 21:42:22      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:code   16px   targe   get   lan   stack   min   target   ret   

感谢 键盘里的青春 前辈详细讲解

先开坑

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 const int MAXN=1000+5;
 7 struct Edge
 8 {
 9     int to,next;
10 }E[MAXN];
11 int node,head[MAXN];
12 int DFN[MAXN],LOW[MAXN],stack[MAXN];
13 int vis[MAXN],index,tot;
14 
15 void insert(int u,int v)
16 {
17     E[++node]=(Edge){v,head[u]};head[u]=node;
18 }
19 
20 void tarjan(int x)
21 {
22     DFN[x]=LOW[x]=++index;
23     stack[++tot]=x;
24     vis[x]=1;
25     for(int i=head[x];i;i=E[i].next)
26     {
27         if(!DFN[E[i].to])
28         {
29             tarjan(E[i].to);
30             LOW[x]=min(LOW[x],LOW[E[i].to]);
31         }
32         else if(vis[E[i].to])
33             LOW[x]=min(LOW[x],DFN[E[i].to]);
34     }
35     if(LOW[x]==DFN[x])
36     {
37         do
38         {
39             printf("%d ",stack[tot]);
40             vis[stack[tot]]=0;
41             tot--;
42         }while(x!=stack[tot+1]);
43         printf("\n");
44     }
45 }
46 
47 int main()
48 {
49     int n,m;
50     scanf("%d %d",&n,&m);
51     for(int i=1;i<=m;i++)
52     {
53         int x,y;
54         scanf("%d %d",&x,&y);
55         insert(x,y);
56     }
57     for(int i=1;i<=n;i++)
58         if(!DFN[i]) tarjan(i);
59     return 0;
60 }

 

Tarjan 模板

标签:code   16px   targe   get   lan   stack   min   target   ret   

原文地址:https://www.cnblogs.com/InWILL/p/9343655.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!