POJ 2553 - The Bottom of a Graph(Tarjan + 缩点 + 计算出度)
2016-08-01 21:31
519 查看
题目链接:
http://poj.org/problem?id=2553题解:
Tarjan搞一下,然后缩个点,最后计算一下出度,出度为0的按顺序输出就好了。贴个大神博客,看了他的讲解我对Tarjan了解加深不少:
http://blog.csdn.net/mengxiang000000/article/details/51672725
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; vector <int> mapp[100000]; int vis[100000],dfn[100000],low[100000],color[100000],stk[100000],ans[100000],degree[100000]; int cnt,n,m,x,y,t,sig; void init() { memset(vis,0,sizeof(vis)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(color,0,sizeof(color)); memset(ans,0,sizeof(ans)); memset(degree,0,sizeof(degree)); for(int i=1;i<=n;i++)mapp[i].clear(); } void taijan(int u) { vis[u] = 1; low[u] = dfn[u] = cnt++; stk[++t] = u; for(int j = 0; j < mapp[u].size(); j++) { int v = mapp[u][j]; if(!vis[v]) taijan(v); if(vis[v] == 1)low[u]=min(low[u],low[v]);//这里一定要用if,回溯的关键条件 } if(dfn[u] == low[u]) { sig++; do { color[stk[t]]=sig;//缩点之后同一强连通分量的点染成同一种颜色。 vis[stk[t]]=-1;//这个点相当于没了,染成同一种颜色意思就是缩成一个点了 } while(stk[t--]!=u); } } int main() { while(~scanf("%d", &n)) { if(n == 0)break; scanf("%d", &m); init(); cnt = 1,t = -1,sig = 0; for(int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); mapp[x].push_back(y); } for(int i = 1; i <= n; i++) { if(!vis[i]) taijan(i); } for(int i = 1; i <= n; i++) { for(int j = 0; j < mapp[i].size(); j++) { int v = mapp[i][j]; if(color[i] != color[v])//i所能到达的点如果有颜色不一样的,说明不是同一强连通分量 { degree[color[i]]++;//计算出度 } } } int ct = 0; for(int i = 1; i <= sig; i++)//已经染完色的区域 { if(degree[i] > 0)continue; for(int j = 1; j <= n; j++) { if(color[j] == i)//出度为0切缩点后为同一染色区域的点,进入ans数组 { ans[ct++] = j; } } } sort(ans, ans + ct); for(int i = 0 ; i < ct; i++) { if(i == ct-1) printf("%d\n", ans[i]); else printf("%d ", ans[i]); } } return 0; }
相关文章推荐
- The Bottom of a Graph poj 2553 缩点+Tarjan
- POJ 2553 The Bottom of a Graph【Tarjan+缩点染色】
- POJ 2553 The Bottom of a Graph(Tarjan,强连通分量)
- poj 2553 The Bottom of a Graph(强连通 Tarjan)
- poj2553 The Bottom of a Graph--Kosaraju算法 & 缩点 & 强连通分量
- POJ 2553 - The Bottom of a Graph(强连通分量, 缩点)
- [tarjan] poj 2553 The Bottom of a Graph
- POJ 2553 The Bottom of a Graph 【scc tarjan】
- poj 2553 The Bottom of a Graph 【有向图tarjan 求SCC 处理出度为0的SCC】
- poj 2553 zoj 1979 The Bottom of a Graph(强联通分量 Tarjan)
- pku 2553 The Bottom of a Graph(tarjan缩点)
- POJ2553 The Bottom of a Graph 强连通 tarjan
- poj 2553 The Bottom of a Graph(Tarjan~)
- poj 2553 The Bottom of a Graph(强连通、缩点、出入度)
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
- POJ 2553 The Bottom of a Graph TarJan算法题解
- 强连通分量 ( Tarjan,邻接链表 )——The Bottom of a Graph ( POJ 2553 )
- poj 2553 The Bottom of a Graph 【有向图tarjan 求SCC 处理出度为0的SCC】
- POJ2553 The Bottom of a Graph 强连通 tarjan