POJ 2553 - The Bottom of a Graph(Tarjan + 缩点 + 计算出度)
来源:互联网 发布:知乎周刊plus重拾英语 编辑:程序博客网 时间:2024/04/27 21:58
题目链接:
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;}
0 0
- POJ 2553 - The Bottom of a Graph(Tarjan + 缩点 + 计算出度)
- 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 or kosaraju
- POJ2553:The Bottom of a Graph(tarjan缩点)
- POJ 2553The Bottom of a Graph(Tarjan)
- POJ 2553 The Bottom of a Graph TarJan算法题解
- [tarjan] poj 2553 The Bottom of a Graph
- 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 zoj 1979 The Bottom of a Graph(强联通分量 Tarjan)
- POJ-2553 The Bottom of a Graph (强连通分量[Tarjan])
- POJ 2553 The Bottom of a Graph(Tarjan Algorithm强连通分量)
- poj--2553--The Bottom of a Graph (scc+缩点)
- Poj 2533 The Bottom of a Graph【强连通Tarjan】
- POJ 2553 The Bottom of A Graph
- Android
- figure新标签的应用
- More is better
- POJ 3026 Borg Maze (bfs+prim)
- POST方法 和 GET方法
- POJ 2553 - The Bottom of a Graph(Tarjan + 缩点 + 计算出度)
- CopyOnWriteArrayList与Collections.synchronizedList的性能对比
- Java Web学习(30): Servlet学习(三)
- 【NAS】NFS客户端挂载目录后无写入权限的解放方案
- led与普通灯的区别
- 关于困扰我很多次的matlab编程小问题
- C++简单介绍
- Unity5.3加载场景
- CSU 1756: Prime