POJ 2553——The Bottom of a Graph(强连通分量)
来源:互联网 发布:教师远程网络培训 编辑:程序博客网 时间:2024/05/29 17:53
The Bottom of a Graph
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 10222 Accepted: 4229
Description
We will use the following (standard) definitions from graph theory. Let V be a nonempty and finite set, its elements being called vertices (or nodes). Let E be a subset of the Cartesian product V×V, its elements being called edges. Then G=(V,E) is called a directed graph.
Let n be a positive integer, and let p=(e1,…,en) be a sequence of length n of edges ei∈E such that ei=(vi,vi+1) for a sequence of vertices (v1,…,vn+1). Then p is called a path from vertex v1 to vertex vn+1 in G and we say that vn+1 is reachable from v1, writing (v1→vn+1).
Here are some new definitions. A node v in a graph G=(V,E) is called a sink, if for every node w in G that is reachable from v, v is also reachable from w. The bottom of a graph is the subset of all nodes that are sinks, i.e., bottom(G)={v∈V|∀w∈V:(v→w)⇒(w→v)}. You have to calculate the bottom of certain graphs.
Input
The input contains several test cases, each of which corresponds to a directed graph G. Each test case starts with an integer number v, denoting the number of vertices of G=(V,E), where the vertices will be identified by the integer numbers in the set V={1,…,v}. You may assume that 1<=v<=5000. That is followed by a non-negative integer e and, thereafter, e pairs of vertex identifiers v1,w1,…,ve,we with the meaning that (vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.
Output
For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.
Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
Source
Ulm Local 2003
题目大意:给出一个有向图,求出图中哪些点满足——它能到达的点都能到达它——并输出各点。
题解:本题是一个求强连通分量的题目。显然在同一强连通分量中的点,如果满足性质则都满足,反之亦然。又因为如果某个强连通分量不满足性质,则必然存在一条由本连通分量指向其他分量的边,故所求的满足条件的强连通分量出度为0。求出所有出度为0的强连通分量即可。
求强连通分量可用Tarjan算法——详见白书。
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <stack>#include <vector>#include <cassert>using namespace std;const int maxn = 5000+50;const int maxm = 1e6+10;struct Edge{ int u; int v; int w;}edges[maxm];vector<int> G[maxn];int pre[maxn];int sccno[maxn]; //每个点所在的强连通分量的编号 int lowlink[maxn];int mark[maxn];int is_ans[maxn];int in0[maxn];int out0[maxn];int n,m,ans,a_from;int dfs_clock,scc_cnt;stack<int> S; void dfs(int u){ pre[u]=lowlink[u]=++dfs_clock; S.push(u); for(int i=0;i<G[u].size();i++){ int e = G[u][i]; int v = edges[e].v; if(!pre[v]){ dfs(v); lowlink[u] = min(lowlink[u],lowlink[v]); } else if(!sccno[v]){ lowlink[u] = min(lowlink[u],pre[v]); } } if(lowlink[u] == pre[u]){ scc_cnt++; for(;;){ int x = S.top();S.pop(); sccno[x] = scc_cnt; if(x == u) break; } }}void find_scc(int n){ dfs_clock = scc_cnt = 0; memset(sccno,0,sizeof(sccno)); memset(pre,0,sizeof(pre)); for(int i=1;i<=n;i++) if(!pre[i]) dfs(i);}int main(){ freopen("A.in","r",stdin); freopen("A.out","w",stdout); while(~scanf("%d%d",&n,&m) && n){ ans = 0;dfs_clock=0; memset(pre,0,sizeof(pre)); memset(sccno,0,sizeof(sccno)); memset(lowlink,0,sizeof(lowlink)); memset(is_ans,0,sizeof(is_ans)); for(int i=1;i<=n;i++) G[i].clear(); for(int i=1;i<=m;i++){ scanf("%d%d",&edges[i].u,&edges[i].v); G[edges[i].u].push_back(i); } find_scc(n); for(int i=1;i<=scc_cnt;i++) in0[i] = out0[i] = 1; for(int i=1;i<=n;i++) for(int j=0;j<G[i].size();j++){ int e = G[i][j]; int v = edges[e].v; if(sccno[i]!=sccno[v]) out0[sccno[i]] = in0[sccno[v]]= 0; } for(int i=1;i<=n;i++) if(out0[sccno[i]]) printf("%d ",i); printf("\n"); } return 0;}
- POJ 2553——The Bottom of a Graph(强连通分量)
- poj 2553 The Bottom of a Graph(强连通分量)
- POJ 2553 The Bottom of a Graph 强连通分量
- POJ 2553 The Bottom of a Graph(强连通分量)
- POJ 2553 The Bottom of a Graph (强连通分量)
- POJ 2553 The Bottom of a Graph(强连通分量)
- POJ 2553 The Bottom of a Graph 强连通分量
- poj 2553 The Bottom of a Graph【强连通分量】
- 强连通分量 ( Tarjan,邻接链表 )——The Bottom of a Graph ( POJ 2553 )
- 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 Algorithm强连通分量)
- poj2553-The Bottom of a Graph(强连通分量)
- poj2553——The Bottom of a Graph(强连通分量)
- POJ 2553 The Bottom of a Graph 强连通分量+缩点 tarjan or kosaraju
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
- POJ 2553The Bottom of a Graph 强连通分量分解
- 期末1.0比赛
- H5学习小结——div+css创建电子商务静态网页
- Retrofit 2中@Path 和@Query的区别
- 初识reactJS的组件化开发(一):简单封装
- U-BOOT-1.1.6-note-文件结构分析注释
- POJ 2553——The Bottom of a Graph(强连通分量)
- PAT-B 1044. 火星数字
- Java设计模式之单例模式
- 脉搏信号检测与处理系统
- 算法的时间复杂度
- PHP5.4及PHP5.5关于htmlspecialchars输出为空的问题
- 第5课:零基础实战Scala函数式编程及Spark源码解析
- java-基础-接口和抽象类
- app个人资料—头像测试