算法 {SCC缩点变为DAG}
SCC缩点变为DAG
定义
有向图G, 对任意有向边a->b
, 假设Sa,Sb: 表示他俩的SCC编号
, 如果Sa != Sb
, 则在新图GG中连接Sa -> Sb
边, 最终GG图是DAG;
算法
模板
//{ ___ShrinkSCC_ToDAG {SCC,DAG}
template< class _RawGraph_, class _ResultGraph_> void ___ShrinkSCC_ToDAG( _RawGraph_ const& _rawGraph, int const* _rawGraph_sccID, _ResultGraph_ & _resGraph){
_resGraph.Initialize( 1 + *std::max_element( _rawGraph_sccID, _rawGraph_sccID + _rawGraph.PointsCount), 0, 0);
for( int cur = 0; cur < _rawGraph.PointsCount; ++cur){
for( auto const& [nex,wth] : _rawGraph[cur]){
if( _rawGraph_sccID[ cur] == _rawGraph_sccID[ nex]){ continue;}
_resGraph.AddEdge_Directed( _rawGraph_sccID[ cur], _rawGraph_sccID[ nex], wth);
}
}
}
//} ___ShrinkSCC_ToDAG
应用
缩点后, 在DAG中 求一个点的可达点的个数 (通过记忆化DFS);
@LINK: https://editor.csdn.net/md/?not_checkout=1&articleId=139597956
;