Tarjan算法 void Tarjan(int u) { DFN[u]=LOW[u]=++index; instack[u]=true; stack[top++]=u; for(node*temp=edge[u];temp;temp=temp->next) { if(!DFN[temp->v]) { Tarjan(temp->v); LOW[u]=LOW[u]<LOW[temp->v]?LOW[u]:LOW[temp->v]; } else { if(instack[temp->v]&&DFN[temp->v]<LOW[u]) LOW[u]=DNF[temp->v]; } } if(LOW[u]==DNF[u]) { Scnt++; do { rec=stack[top--]; instack[rec]=false; belong[rec]=Scnt; } while(rec!=u) } } void solve() { int i; top=Scnt=index=0; memset(DFN,0,sizeof(DFN)); for(i=1;i<=N;i++) { if(!DFN[i]) tarjan(i); } }