#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 stacklow[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求强连通(缩点)
热度:98 发布时间:2023-09-23 07:51:08.0
相关解决方案
- 最短路+tarjan codeforces567E President and Roads
- POJ1330_Nearest Common Ancestors(tarjan)
- 超级模版系列----tarjan
- poj-3694-Network-并查集+tarjan
- HDU 4685 二分图匹配+tarjan
- POJ 1236 Network of Schools HDU 3836 Equivalent Sets 强连通分量+缩点 tarjan or kosaraju
- POJ 2553 The Bottom of a Graph 强连通分量+缩点 tarjan or kosaraju
- POJ 2762 Going from u to v or from v to u? 图的单连通性 tarjan or kosaraju
- POJ 1236 强连通分量 Tarjan
- POJ 2553 强连通分量 Tarjan
- 【tarjan】P1262 间谍网络
- [bzoj2707][SDOI2012]走迷宫(tarjan+拓扑排序+高斯消元)
- [P3469][POI2008]BLO-Blockade(tarjan+割点)
- HDU4689 Prince and Princess (Tarjan+匈牙利匹配)