Created
August 13, 2016 01:24
-
-
Save Lyken17/1d46c81634ad51d7c8fc8db6b777a394 to your computer and use it in GitHub Desktop.
tarjan algorithm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void tarjan(int i) | |
{ | |
int j; | |
DFN[i]=LOW[i]=++Dindex; | |
instack[i]=true; | |
Stap[++Stop]=i; | |
for (edge *e=V[i];e;e=e->next) | |
{ | |
j=e->t; | |
if (!DFN[j]) | |
{ | |
tarjan(j); | |
if (LOW[j]<LOW[i]) | |
LOW[i]=LOW[j]; | |
} | |
else if (instack[j] && DFN[j]<LOW[i]) | |
LOW[i]=DFN[j]; | |
} | |
if (DFN[i]==LOW[i]) | |
{ | |
Bcnt++; | |
do | |
{ | |
j=Stap[Stop--]; | |
instack[j]=false; | |
Belong[j]=Bcnt; | |
} | |
while (j!=i); | |
} | |
} | |
void solve() | |
{ | |
int i; | |
Stop=Bcnt=Dindex=0; | |
memset(DFN,0,sizeof(DFN)); | |
for (i=1;i<=N;i++) | |
if (!DFN[i]) | |
tarjan(i); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment