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 ...
随机推荐
- wine的使用
ubuntu-ubuntu10.04使用wine安装SourceInsight http://www.cnblogs.com/eddy-he/archive/2012/03/08/ubuntu_win ...
- 触发器三(行级DML触发器)(学习笔记)
行级DML触发器 每当一条记录出现更新操作时进行触发操作定义时要定义FOR EACH ROW 使用":old.字段"和":new.字段"标识符 No. 触发语句 ...
- Linux操作系统安装与VMTools的安装
Linux操作系统安装:虚拟机+RedHat 1.安装虚拟机VMware_WorkstationV7.1.3 2.安装RedHat 自动安装(会自动安装VMTools): 打开虚拟机-->新建虚 ...
- Windows平台Hadoop编译、安装、配置与运行(转)
http://www.srccodes.com/p/article/38/build-install-configure-run-apache-hadoop-2.2.0-microsoft-windo ...
- 解决Swap file ".ceshi.c.swp" already exists!问题
关于swp文件:使用vi,常常能够看到swp这个文件,那这个文件是怎么产生的呢.当你打开一个文件,vi就会生成这么一个.(filename)swp文件以备不測,假设你正常退出,那么这个.(filena ...
- 线性表 顺序存储 链式存储 ---java实现
首先抽象出一个线性表抽象类(包括主要的增删操作) public abstract class MyAbstractList<E> { public abstract void add(E ...
- 算法笔记_207:第五届蓝桥杯软件类决赛部分真题(Java语言C组)
目录 1 数字拆分 2 稍大的串 前言:以下代码仅供参考,若有错误欢迎指正哦~ 1 数字拆分 正整数可以表示为若干正整数的累加和. 如,对于正整数n=6,可以分划为: 6 5+1 4+2 4+1+ ...
- 在 Java SE 6 中监视和诊断性能问题
Java™ Platform, Standard Edition 6 (Java SE) 专注于提升性能,提供的增强工具可以管理和监视应用程序以及诊断常见的问题.本文将介绍 Java SE 平台中监视 ...
- 笔试题之javaweb
Java web部分 1.Tomcat的优化经验 答:去掉对web.xml的监视,把jsp提前编辑成Servlet. 有富余物理内存的情况,加大tomcat使用的jvm的内存 2. ...
- HTTP请求响应码
① 客户方错误 100 继续 101 交换协议 ② 成功 200 OK 201 已创建 202 接收 203 非认证信息 204 无内容 205 重置内容 206 部分内容 ③ 重定向 300 多路选 ...