POJ2553 The Bottom of a Graph 强连通 tarjan
2011-10-30 19:28
483 查看
题意:此题最难的部分即是理解题意。
注意要求得点的定义为:所有这个点能到达的点都能到达这个点。
思路:
强连通,缩点,找出出度为0的强连通分量集合,就是要求得点集合。
注意要求得点的定义为:所有这个点能到达的点都能到达这个点。
思路:
强连通,缩点,找出出度为0的强连通分量集合,就是要求得点集合。
#include<iostream> #define min(a,b) (a<b?a:b) using namespace std; const int N=5005; bool mat ; int dfn ,low ; bool instack ; int indgr ; int outdgr ; int stack ; int sp; int index; int n,m; int belong ; void tarjan(int i) { dfn[i]=low[i]=index++; stack[sp++]=i; instack[i]=true; for(int j=1;j<=n;j++) { if(mat[i][j]) { if(!dfn[j]) { tarjan(j); low[i]=min(low[i],low[j]); } else if(instack[j]) { low[i]=min(dfn[j],low[i]); } } } if(low[i]==dfn[i]) { int j; do { j=stack[--sp]; instack[j]=false; belong[j]=i; }while(j!=i); } } void solve() { for(int i=1;i<=n;i++) { if(!dfn[i]) { tarjan(i); } } for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { if(mat[i][j]) { if(belong[i]!=belong[j]) { indgr[belong[j]]++; outdgr[belong[i]]++; } } } for(int i=1;i<=n;i++) { if(outdgr[belong[i]]==0) { printf("%d ",i); } } printf("\n"); } int main() { while(scanf("%d%d",&n,&m),n!=0) { index=1; sp=1; memset(indgr,0,sizeof(indgr)); memset(outdgr,0,sizeof(outdgr)); memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(mat,0,sizeof(mat)); memset(instack,0,sizeof(instack)); memset(stack,0,sizeof(stack)); memset(belong,0,sizeof(belong)); int from,to; for(int i=1;i<=m;i++) { scanf("%d%d",&from,&to); mat[from][to]=true; } solve(); } return 0; }
相关文章推荐
- [poj 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(强连通 Tarjan)
- 强连通分量 ( Tarjan,邻接链表 )——The Bottom of a Graph ( POJ 2553 )
- POJ 2553 - The Bottom of a Graph(Tarjan + 缩点 + 计算出度)
- POJ 2553 The Bottom of a Graph TarJan算法题解
- poj 2553 The Bottom of a Graph 【有向图tarjan 求SCC 处理出度为0的SCC】
- POJ 2553 The Bottom of a Graph(Tarjan,强连通分量)
- POJ2553 The Bottom of a Graph Tarjan 矩阵 pascal 解题报告
- poj 2553 The Bottom of a Graph 强连通Kosaraju
- poj 2553 The Bottom of a Graph 【有向图tarjan 求SCC 处理出度为0的SCC】
- poj 2553 The Bottom of a Graph 强连通Kosaraju
- poj 2553 The Bottom of a Graph(Tarjan~)
- POJ-2553 The Bottom of a Graph (强连通分量[Tarjan])
- The Bottom of a Graph poj 2553 缩点+Tarjan
- poj 2553 The Bottom of a Graph(强连通、缩点、出入度)
- POJ 2553 The Bottom of Graph 强连通图题解
- poj 2553 The Bottom of a Graph 【强连通图中出度为0点】
- [tarjan] poj 2553 The Bottom of a Graph