Tarjan求强连通(缩点)
2016-08-21 16:48
141 查看
#include<iostream> #include<cstdio> #include<vector> #include<stack> #include<cstring> #include<algorithm> using namespace std; const int maxn=10000+10; bool vis[maxn]; int ID[maxn]; //点的颜色编号 int index,ncolor; vector<int> dfn(maxn),low(maxn),st; vector<vector<int> > color(maxn); //同一种颜色的点 vector<vector<int> > G(maxn); void Tarjan(int u) { dfn[u]=low[u]=++index; vis[u]=true; st.push_back(u); for(int i=0;i<G[u].size();i++) { int v=G[u][i]; if(!vis[v]){ Tarjan(v); low[u]=min(low[u],low[v]); } else if(find(st.begin(),st.end(),v)!=st.end()) //in stack low[u]=min(low[u],dfn[v]); } if(dfn[u]==low[u]) { int v; ncolor++; do{ v=st.back(); st.pop_back(); color[ncolor].push_back(v); ID[v]=ncolor; }while(v!=u); } } int getGraph(int n) { index=ncolor=0; for(int i=1;i<=n;i++) if(!vis[i]) Tarjan(i); } int main() { int n,m; cin>>n>>m; while(m--){ int u,v; cin>>u>>v; G[u].push_back(v); } getGraph(n); return 0; }
相关文章推荐
- 强连通分量,Tarjan,缩点(Network of Schools,POJ 1236)
- HDU4612Warm up 边双连通 Tarjan缩点
- 连通分量模板:tarjan: 求割点 && 桥 && 缩点 && 强连通分量 && 双连通分量 && LCA(最近公共祖先)
- 【日常学习】【强连通分量tarjan缩点】codevs1611 抢掠计划题解
- 强连通分量——tarjan ->缩点
- POJ 3177--Redundant Paths【无向图添加最少的边成为边双连通图 && tarjan求ebc && 缩点构造缩点树】
- POJ 2762 强连通分量中存在单相连通边 【tarjan+toposort+缩点】.cpp
- BZOJ 1179: [Apio2009]Atm Tarjan强连通分量缩点,SPFA,DP
- Tarjan求强连通分量 缩点
- 强连通分量及缩点tarjan算法解析
- POJ 3114 Countries in War 强连通tarjan缩点后 跑最短路spfa
- Going from u to v or from v to u? 【判定弱连通】=【tarjan求scc+ 缩点+topo】
- BZOJ 1093 最大半连通子图(tarjan缩点 拓扑排序)
- BZOJ 2208 [Jsoi2010]连通数 tarjan缩点+bitset优化DP
- POJ 2186 -- Popular Cows【强连通分支 && Tarjan缩点】
- 51nod 1456 小K的技术(Tarjan强连通分量缩点,并查集)
- BZOJ2208: [Jsoi2010]连通数 Tarjan缩点+Dfs
- Network of Schools POJ - 1236 tarjan强连通分量缩点
- poj 2186 Popular Cows(Tarjan,强连通分量缩点)
- [HDU 2767]Proving Equivalences[Tarjan强连通缩点缩点]