POJ 2553 The Bottom of Graph 强连通图题解
Description
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
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

Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
题意的本质是查找没有出度的强连通子图,没有出度就是sink。the bottom of graph了。
就是利用Tarjan算法求强连通子图,并要用标识号标识各个强连通子图,然后记录好各个顶点属于哪强连通子图。
程序带具体的注解:
#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std; const int MAX_V = 5001;
vector<int> graAdj[MAX_V];//vector表示的邻接图
int conNo, vToCon[MAX_V];//强连通子图标号及顶点相应强连通子图号的数组
int low[MAX_V];//标识最低标识号。假设都属于这个标识号的顶点都属于同一连通子图
int stk[MAX_V], top;//数组表示栈
bool vis[MAX_V];//记录是否訪问过的顶点
int out[MAX_V];//强连通子图的出度。假设出度为零。那么改强连通子图为sink template<typename T>
inline bool equ(T t1, T t2) { return t1 == t2; } void dfsTar(int u, int no = 1)
{
low[u] = no;//每递归进一个顶点。初始表示low[]
stk[++top] = u;//每一个顶点记录入栈
vis[u] = true;//标志好是否訪问过了 int n = (int)graAdj[u].size();
for (int i = 0; i < n; i++)
{
int v = graAdj[u][i];
if (!vis[v])
{
dfsTar(v, no+1);//这里递归
if (low[u] > low[v]) low[u] = low[v];//更新最低标识号
}
else if (!vToCon[v] && low[u] > low[v]) low[u] = low[v];//更新
}
if (equ(low[u], no))//最低标识号和递归进的初始号同样就找到一个子图了
{
++conNo;
int v;
do
{
v = stk[top--];//出栈
vToCon[v] = conNo;//顶点相应到子图号
} while (v != u);//出栈到本顶点,那么改子图全部顶点出栈完成
}
} void Tarjan(int n)
{
conNo = 0;//记得前期的清零工作
fill(vToCon, vToCon+n+1, 0);
fill(low, low+n+1, 0);
fill(vis, vis+n+1, false);
top = -1; for (int u = 1; u <= n; u++) if (!vis[u]) dfsTar(u);
} int main()
{
int V, E, u, v;
while(~scanf("%d %d", &V, &E) && V)
{
for (int i = 1; i <= V; i++)
{
graAdj[i].clear();//清零
}
for (int i = 0; i < E; i++)
{
scanf("%d %d", &u, &v);
graAdj[u].push_back(v);//建立vector表示的邻接表
}
Tarjan(V);
fill(out, out+conNo+1, 0);
for (int u = 1; u <= V; u++)
{
int n = graAdj[u].size();
for (int i = 0; i < n; i++)
{
int v = graAdj[u][i];
if (vToCon[u] != vToCon[v])
{
out[vToCon[u]]++;//记录强连通子图号的出度数
}
}
}
for (int u = 1; u <= V; u++)//出度为零,即为答案:Graph Bottom
{
if (!out[vToCon[u]]) printf("%d ", u);
}
putchar('\n');
}
return 0;
}
POJ 2553 The Bottom of Graph 强连通图题解的更多相关文章
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- poj 2553 The Bottom of a Graph(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- POJ 2553 The Bottom of a Graph (Tarjan)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11981 Accepted: ...
- poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点
/** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...
- poj 2553 The Bottom of a Graph【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- poj - 2186 Popular Cows && poj - 2553 The Bottom of a Graph (强连通)
http://poj.org/problem?id=2186 给定n头牛,m个关系,每个关系a,b表示a认为b是受欢迎的,但是不代表b认为a是受欢迎的,关系之间还有传递性,假如a->b,b-&g ...
随机推荐
- 单点登录(SSO)(原创)
单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. 下面的sso ...
- 【转载】Remote System Explorer Operation总是运行后台服务,卡死eclipse解决办法
原来是eclipse后台进程在远程操作,就是右下角显示的“Remote System Explorer Operation”.折腾了半天,在Stack Overflow找到答案(源地址).把解决方案翻 ...
- 优秀web资源
http://www.filewatcher.com 一步一步asp.net_页面静态化管理 http://www.cnblogs.com/ylwn817/articles/2006923.html ...
- 上传的文件放在SVN服务器的哪个目录下
SVN服务器版本库有两种格式,一种为FSFS,一种为BDB 把文件上传到SVN版本库后,上传的文件不再以文件原来的格式存储,而是被svn以它自定义的格式压缩成版本库数据,存放在版本库中. 如果是FSF ...
- wepy - 与原生有什么不同(x.wpy)使用实例
源码 <template> <view class='mark' wx:if="{{showMark}}"> <view animation=&quo ...
- 【二】Drupal 入门之新建主题
Drupal 的模板是以 *.tpl.php 命名的 php 文件 1.在Drupal中,默认模板路径为 moudles/system 这就是我们为什么还没有制作模板 Drupal 就能够正常显示 ...
- photoshop 切片工具进行切图
1.使用切片工具切图 2.存储为web所用格式 3.选择图片格式 4.只保存切片(选择所有用户切片) 5.查看:
- 【FinacialKnowledge】财务报表及名词解释
1.财务报表 以下三张表为:资产负债表.利润表.现金流量表 ...
- Tomcat 配置加密的服务器连接器
先查询API,找到Configuration里面的Connector的HTTP中的SSL(加密连接器) SSL abbr. Security Socket Layer 加密套接字协议层 利用已生成 ...
- xml DTD中的ELEMENT和ATTLIST
是W3C的一个文档类型定义规则文件,是用来让浏览器根据你定义的DTD(文档类型定义)来解释页面代码的. doctype声明指出阅读程序应该用什么规则集来解释文档中的标记.在Web文档的情况下,“阅读程 ...