The Bottom of a Graph(tarjan + 缩点)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 9139 | Accepted: 3794 |
Description
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+1in 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
Output

Sample Input
3 3
1 3 2 3 3 1
2 1
1 2
0
Sample Output
1 3
2
Source
参考代码这里:http://blog.csdn.net/ehi11/article/details/7884851
缩点:(这个概念也是看了别人的理解)先求有向图的强连通分量 , 如果几个点同属于一个强连通 , 那就给它们标上相同的记号 , 这样这几个点的集合就形成了一个缩点。
题目大意:求出度为0的强连通分量
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int M = ;
int n , m ;
int stack [M] , top = , index = ;
bool instack [M] ;
int dfn[M] , low[M] ;
int cnt = ;
vector <int> e[M] ;
int belong[M] ;
int out[M] ; void init (int n)
{
top = ;
cnt = ;
index = ;
memset (stack , - , sizeof(stack)) ;
memset (instack , , sizeof(instack)) ;
memset (dfn , - , sizeof(dfn)) ;
memset (low , - , sizeof(low)) ;
for (int i = ; i <= n ; i++)
e[i].clear () ;
memset (belong , - , sizeof(belong)) ;
memset (out , , sizeof(out)) ;
} void tarjan (int u)
{
int v ;
dfn[u] = low[u] = index++ ;
instack[u] = true ;
stack[++top] = u ;
for (int i = ; i < e[u].size () ; i++) {
v = e[u][i] ;
if (dfn[v] == -) {
tarjan (v) ;
low[u] = min (low[u] , low[v]) ;
}
else if (instack[v])
low[u] = min (low[u] , dfn[v]) ;
}
if (low[u] == dfn[u]) {
cnt++ ;
do {
v = stack[top--] ;
instack[v] = false ;
belong[v] = cnt ;
} while (u != v) ;
}
} int main ()
{
//freopen ("a.txt" , "r" , stdin) ;
int u , v ;
while (~ scanf ("%d" ,&n)) {
if (n == )
break ;
init (n) ;
scanf ("%d" , &m) ;
while (m--) {
scanf ("%d%d" , &u , &v) ;
e[u].push_back (v) ;
}
for (int i = ; i <= n ; i++) {
if (dfn[i] == -)
tarjan (i) ;
}
for (int i = ; i <= n ; i++) {
for (int j = ; j < e[i].size () ; j++) {
if (belong [i] != belong[e[i][j]])
out[belong[i]] ++;
}
}
int k = ;
for (int i = ; i <= n ; i++) {
if (out[belong[i]] == ) {
if (k++)
printf (" ") ;
printf ("%d" , i) ;
}
}
puts ("") ;
}
return ;
}
The Bottom of a Graph(tarjan + 缩点)的更多相关文章
- POJ2533&&SP1799 The Bottom of a Graph(tarjan+缩点)
POJ2553 SP1799 我们知道单独一个强连通分量中的所有点是满足题目要求的 但如果它连出去到了其他点那里,要么成为新的强连通分量,要么失去原有的符合题目要求的性质 所以只需tarjan缩点求出 ...
- 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)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11981 Accepted: ...
- poj--2553--The Bottom of a Graph (scc+缩点)
The Bottom of a Graph Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Oth ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- POJ 2553 The Bottom of a Graph TarJan算法题解
本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...
- 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【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- 【图论】The Bottom of a Graph
[POJ2553]The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11182 ...
随机推荐
- 使用git推送代码到开源中国以及IDEA环境下使用git
使用git推送代码到开源中国以及IDEA环境下使用git 在学习Java的过程中我们会使用到git这个工具来将我们本周所编写的代码上传到开源中国进行代码托管,而在使用git的时候有很多的同学由于不会操 ...
- 浅谈JS面向对象之创建对象
hello,everybody,今天要探讨的问题是JS面向对象,其实面向对象呢呢,一般是在大型项目上会采用,不过了解它对我们理解JS语言有很大的意义. 首先什么是面向对象编程(oop),就是用对象的思 ...
- 推荐一个 angular 图像加载插件
推荐一个简单的 Angular 图片加载插件:vgSrc,插件根据图片资源的不同加载状态,显示不同图片,亲测兼容IE-8. 使用 推荐使用 bower 加载: bash bower install v ...
- 5.9-4用字符串生成器给字符串str追加1~10这10个数字
package zfc; public class ZfcShcq { public static void main(String[] args) { // TODO Auto-generated ...
- Html-Css-设置DIV边框圆滑
border-radius: 10px; -moz-border-radius: 10px; -webkit-border-radius: 10px; -o-border-radius: 10px; ...
- Java 集合类详解(含类图)
0.参考文献 此图中蓝色为抽象类.深红色表示接口(Arrays除外).绿色表示具体容器类 1.java集合类图 1.1 1.2 上述类图中,实线边框的是实现类,比如ArrayList,LinkedLi ...
- php复习
最近要用php,好久不用感觉手生.抓起<零基础学PHP>一书复习了下,顺带学了smarty模板语言,然后到慕课网看了些php中级视频教程,这里记录下. php最基本的文件上传 不用任何第三 ...
- 轻量级应用开发之(08)UITableView
一 UITableView基本介绍 在众多移动应⽤用中,能看到各式各样的表格数据 . 在iOS中,要实现表格数据展示,最常用的做法就是使用UITableView,UITableView继承自UISc ...
- 如何把项目托管到GitHub
第一步:先注册一个Github的账号,这是必须的 注册地址:Github官网注册入口 第二步:准备工作 gitHub网站使用Git版本管理工具来对仓库进行管理,注意它们并不等同. gitHub是全球最 ...
- BC68(HD5606) 并查集+求集合元素
tree Accepts: 143 Submissions: 807 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65 ...