强连通分量:
SCC是指在有向图中,存在的“环”(一个节点本身也是一个环)
注:无向图中的“环”本算法不适用
算法思路:
通过对整个图进行一次深度优先遍历,DFN(x)表示第一次遍历到x的时间,LOW(x)表示遍历到x所在的SCC的最早时间,如果dfn(x)=low(x),则找到了一个强连通分量。具体实现时,需要维护一个栈来维护已经遍历到的节点。O(n)
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
using namespace std;
const int maxn=100000;
#define RUN
vector<int> to[maxn];
int lab[maxn],tt,low[maxn],dfn[maxn],time;
int s[maxn],top;
short used[maxn];
void DFS(int x){
used[x]=1;
dfn[x]=low[x]=++time;
s[++top]=x;
for(int i=0,y;i<to[x].size();i++){
y=to[x][i];
if(used[y]==0) DFS(y);
if(used[y]<2) low[x]=min(low[x