The Bottom of a Graph
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 9139   Accepted: 3794

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+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 vv 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

参考代码这里: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 + 缩点)的更多相关文章

  1. POJ2533&&SP1799 The Bottom of a Graph(tarjan+缩点)

    POJ2553 SP1799 我们知道单独一个强连通分量中的所有点是满足题目要求的 但如果它连出去到了其他点那里,要么成为新的强连通分量,要么失去原有的符合题目要求的性质 所以只需tarjan缩点求出 ...

  2. POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)

    Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...

  3. POJ 2553 The Bottom of a Graph (Tarjan)

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11981   Accepted: ...

  4. 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 ...

  5. [poj 2553]The Bottom of a Graph[Tarjan强连通分量]

    题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...

  6. POJ 2553 The Bottom of a Graph TarJan算法题解

    本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...

  7. poj 2553 The Bottom of a Graph(强连通分量+缩点)

    题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K ...

  8. poj 2553 The Bottom of a Graph【强连通分量求汇点个数】

    The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 9641   Accepted:  ...

  9. 【图论】The Bottom of a Graph

    [POJ2553]The Bottom of a Graph Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 11182   ...

随机推荐

  1. CUDA编程学习(一)

    /****c code****/ #include<stdio.h> int main() { printf("Hello world!\n); ; } /****CUDA co ...

  2. 怎样写 OpenStack Neutron 的 Extension (二)

    接着之前一篇文章,再来谈谈 Extension 的具体实现问题.我使用的是本地数据库加远程API调用的方法,所以先要定义一下数据库中 myextension 如何存储.首先,我们可以在自己的 plug ...

  3. asp.net网站安全常见问题与防范

    1:SQL 注入 2:XSS 3:CSRF 4:文件上传 1:SQL 注入 引起原因: 其实现在很多网站中都存在这种问题.就是程序中直接进行SQL语句拼接.可能有些读者不太明白. 下面通过一个登录时对 ...

  4. 【BZOJ 1019】【SHOI2008】汉诺塔(待定系数法递推)

    1019: [SHOI2008]汉诺塔 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 559  Solved: 341[Submit][Status] ...

  5. DOM(四)事件流

    1.冒泡型事件 浏览器的事件模型分两种:捕获型事件和冒泡型事件.由于ie不支持捕获型事件,所以以下主要以冒泡型事件作为讲解.(dubbed bubbling)冒泡型指事件安装最特定的事件到最不特定的事 ...

  6. Java设计模式-装饰模式(Decorator)

    顾名思义,装饰模式就是给一个对象增加一些新的功能,而且是动态的,要求装饰对象和被装饰对象实现同一个接口,装饰对象持有被装饰对象的实例,关系图如下: Source类是被装饰类,Decorator类是一个 ...

  7. 遍历HashMap的四种方法

    public static void main(String[] args) { Map<String,String> map=new HashMap<String,String&g ...

  8. 【CodeForces 620D】Professor GukiZ and Two Arrays

    题 题意 两个数列,一个有n个数,另一个有m个数,让你最多交换两次两个数列的数,使得两个数列和的差的绝对值最小,求这个差的绝对值.最少交换次数.交换数对 分析 交换0次.1次可得到的最小的差可以枚举出 ...

  9. 3.Android之单选按钮RadioGroup和复选框Checkbox学习

    单选按钮和复选框在实际中经常看到,今天就简单梳理下. 首先,我们在工具中拖进单选按钮RadioGroup和复选框Checkbox,如图: xml对应的源码: <?xml version=&quo ...

  10. 微型 ORM-FluentData 温故知新系列

    http://www.cnblogs.com/_popc/archive/2012/12/26/2834726.html 引言:FluentData 是微型 ORM(micro-ORM)家族的一名新成 ...