tarjan模板(缩点,求有向图强连通分量)
2016-05-04 15:53
316 查看
具体思路见详解网址:https://www.byvoid.com/blog/scc-tarjan;
然后整理出了这个tarjan模板,具体数组的功能代码都有注释。
然后整理出了这个tarjan模板,具体数组的功能代码都有注释。
const int N=100010; struct data { int to,next; } tu[N*2]; int head ; int ip; int dfn , low ;///dfn[]表示深搜的步数,low[u]表示u或u的子树能够追溯到的最早的栈中节点的次序号 int sccno ;///缩点数组,表示某个点对应的缩点值 int step; int scc_cnt;///强连通分量个数 void init() { ip=0; memset(head,-1,sizeof(head)); } void add(int u,int v) { tu[ip].to=v,tu[ip].next=head[u],head[u]=ip++; } vector<int> scc ;///得出来的缩点,scc[i]里面存i这个缩点具体缩了哪些点 stack<int> S; void dfs(int u) { dfn[u] = low[u] = ++step; S.push(u); for (int i = head[u]; i !=-1; i=tu[i].next) { int v = tu[i].to; if (!dfn[v]) { dfs(v); low[u] = min(low[u], low[v]); } else if (!sccno[v]) low[u] = min(low[u], dfn[v]); } if (low[u] == dfn[u]) { scc_cnt += 1; scc[scc_cnt].clear(); while(1) { int x = S.top(); S.pop(); if (sccno[x] != scc_cnt) scc[scc_cnt].push_back(x); sccno[x] = scc_cnt; if (x == u) break; } } } void tarjan(int n) { memset(sccno, 0, sizeof(sccno)); memset(dfn, 0, sizeof(dfn)); step = scc_cnt = 0; for (int i = 1; i <=n; i++) if (!dfn[i]) dfs(i); }
相关文章推荐
- mysql导出和导入xml文件实例
- 大端与小端存储模式详解
- Logistic Regression 损失函数理解
- 第十周项目3 警察和厨师继承组合类
- Java基础知识IO流(其他类)
- 逻辑行计数
- ZOJ 3331 Process the Tasks(双塔DP)
- 高性能的关键:Spring MVC的异步模式
- javaweb学习总结(三十三)——使用JDBC对数据库进行CRUD
- MAC系统下命令获取IP地址
- 深入 理解 Statement 和 PreparedStatement
- json model 转换
- 常用DOM\HTML-DOM方法
- 详解Java中使用泛型实现快速排序算法的方法
- javaweb学习总结(三十二)——JDBC学习入门
- JMeter插件模拟发送UDP请求:UDP sampler
- hdu_2227_Find the nondecreasing subsequences_树状数组,离散化
- waiting for debugger
- TinyXml快速入门(三)
- JavaScript中的模块化之AMD和CMD