22.5-5求有向图的分量图
1.求出强连通分量
(1)第1次dfs,求出(逆)拓扑序
(2)第2次对以逆图的逆拓扑序dfs,得到的DFS森林,便求出强连通分量。期间要记录结点v所属的强连通分量为scc_count。
2.对于原图的每条边,若两个端点不属同一强连通分量,即!stronglyreachable(v,t),便加进DAG中
参考资料:C++算法--图算法(第3版)
P14-15 IO
P23-24 SparseMutiGRAPH
P28 InDegree
P173 StronglyConnected
1.求出强连通分量
(1)第1次dfs,求出(逆)拓扑序
(2)第2次对以逆图的逆拓扑序dfs,得到的DFS森林,便求出强连通分量。期间要记录结点v所属的强连通分量为scc_count。
2.对于原图的每条边,若两个端点不属同一强连通分量,即!stronglyreachable(v,t),便加进DAG中
参考资料:C++算法--图算法(第3版)
P14-15 IO
P23-24 SparseMutiGRAPH
P28 InDegree
P173 StronglyConnected
P180 SccDAG
#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
struct Edge{
int v,w;
Edge(int v=-1,int w=-1):v(v),w(w){}
};
class SparseMutiGRAPH{//邻接链表表示的稀疏图
int Vcount,Ecount;//点数,边数
bool digraph;//true为有向图,false为无向图
struct node{
int v;node*next;
node(int v,node*next):v(v),next(next){}
};
typedef node* link;
vector<link> adjacency;//邻接链表
public:
SparseMutiGRAPH(int Vcount,bool digraph=false):adjacency(Vcount),Vcount(Vcount),Ecount(0),digraph(digraph){
adjacency.assign(Vcount,(node*)NULL);//adjacency大小置为Vcount,每个元素为NULL
}
int V() const{return Vcount;}
int E() const{return Ecount;}
bool directed() const{return digraph;}
void insert(Edge e)
{
int v=e.v,w=e.w;
//将w插入到v的邻接链表表头
adjacency[v]=new node(w,adjacency[v]);
if(!digraph) adjacency[w]=new node(v,adjacency[w]);
Ecount&#