POJ 2553 The Bottom of a Graph - from lanshui_Yang

原创 2013年08月17日 11:31:57

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 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
    题目大意:给你一个有向图,让你找出满足下面条件的点,并按序号从小到大输出。条件:对图中一个顶点V,对于图中每个能到达它的顶点w,w亦可到达v,当然,如果v不能到达其他顶点,这样的v也是满足条件的。
    解题思路:先用tarjan缩点,然后统计出出度为 0 的顶点即可。
    请看代码:
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<set>
#include<vector>
#define mem(a , b) memset(a , b , sizeof(a))
using namespace std ;
const int MAXN = 5005 ;
vector<int> vert[MAXN] ;
vector<int> f[MAXN] ; // 统计每个强连通分量所包含的顶点
set<int> ans ;  // 记录满足条件的点,set 中的元素默认是从小到大排序的
int n , m ;
bool vis[MAXN] ;
int dfn[MAXN] ;
int low[MAXN] ;
int id[MAXN] ;  
int d[MAXN] ; // 统计每个强连通分量的出度
int stap[MAXN] ;
int top ;
bool inq[MAXN] ;
int tmpdfn ;
int fz ;
void clr()
{
    ans.clear() ;
    mem(vis , 0) ;
    mem(dfn , 0) ;
    mem(low , 0) ;
    mem(id , -1) ;
    mem(d , 0) ;
    mem(inq , 0) ;
    mem(stap , -1) ;
    tmpdfn = 0 ;
    top = -1 ;
    fz = 0 ;
    int i ;
    for(i = 1 ; i <= n ; i ++)
    {
        vert[i].clear() ;
        f[i].clear() ;
    }
}
void tarjan(int u)
{
    vis[u] = 1 ;
    stap[++ top] = u ;
    inq[u] = 1 ;
    dfn[u] = low[u] = ++ tmpdfn ;
    int i ;
    for(i = 0 ; i < vert[u].size() ; i ++)
    {
        int v = vert[u][i] ;
        if(!vis[v])
        {
            tarjan(v) ;
            low[u] = min(low[u] , low[v]) ;
        }
        else if(inq[v])
        {
            low[u] = min(low[u] , dfn[v]) ;
        }
    }
    if(low[u] == dfn[u])
    {
        fz ++ ;
        int tmp ;
        do
        {
            tmp = stap[top --] ;
            id[tmp] = fz ;
            f[fz].push_back(tmp) ;
            inq[tmp] = false ;
        }while (tmp != u) ;
    }
}
void init()
{
    clr() ;
    int i ;
    for(i = 0 ; i < m ; i ++)
    {
        int a , b ;
        scanf("%d%d" , &a , &b) ;
        vert[a].push_back(b) ;
    }
}
void solve()
{
    int i ;
    for(i = 1 ; i <= n ; i ++)
    {
        if(!vis[i])
        tarjan(i) ;
    }
    int j ;
    for(i = 1 ; i <= n ; i ++)
    {
        for(j = 0 ; j < vert[i].size() ; j ++)
        {
            int x , y ;
            x = id[i] ;
            y = id[ vert[i][j] ] ;
            if(x != y)
            {
                d[x] ++ ;
            }
        }
    }
    for(i = 1 ; i <= fz ; i ++)
    {
        if(d[i] == 0)
        {
            for(j = 0 ; j < f[i].size() ; j ++)
            {
                ans.insert(f[i][j]) ;
            }
        }
    }
    set<int> :: iterator it ;
    int sum = 0 ;
    for(it = ans.begin() ; it != ans.end() ; ++ it)
    {
        printf("%d" , *it) ;
        if(sum < ans.size() - 1)
        putchar(' ') ;
        sum ++ ;
    }
    puts("") ;
}
int main()
{
    while (scanf("%d" , &n) != EOF)
    {
        if(n == 0)
        break ;
        scanf("%d" , &m) ;
        init() ;
        solve() ;
    }
    return 0 ;
}


From Microservices to Data Microservices

-
  • 1970年01月01日 08:00

poj2553 - The Bottom of a Graph

想看更多的解题报告:http://blog.csdn.net/wangjian8006/article/details/7870410                                ...
  • wangjian8006
  • wangjian8006
  • 2012-08-22 10:50:46
  • 1679

The Bottom of a Graph-POJ2553强连通

The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9759 Accep...
  • huayunhualuo
  • huayunhualuo
  • 2016-01-22 20:10:50
  • 437

POJ 2553 The Bottom of a Graph

http://poj.org/problem?id=2553The Bottom of a GraphTime Limit: 3000MSMemory Limit: 65536KTotal Submi...
  • u013077144
  • u013077144
  • 2016-04-21 18:59:21
  • 286

POJ 2553 The Bottom of a Graph

题意: 定义一个点所能到达的所有点都能到达这个点,那么该点就是一个sink点,现给你n个点m条边的有向图,问你图中有多少个sink点,并指出对应的点。 思路: 首先明白一点,强连通分量里的所有点...
  • w446506278
  • w446506278
  • 2016-07-23 20:32:43
  • 78

POJ 2553 —— The Bottom of a Graph

原题:http://poj.org/problem?id=2553 题意:给出n个点,m条边的有向图;问有多少个点 v 满足—— v能到u,u也能到v(v能到达的所有点都要能回到v); 思...
  • L_avender
  • L_avender
  • 2016-04-21 20:43:44
  • 157

POJ 2553 The Bottom of a Graph .

题目地址:http://poj.org/problem?id=2553 一个点集中任意两个点若u能到v,那么v一定要能到u,也即是求强连通中出度为0的点 代码如下: #include #incl...
  • qq_34446253
  • qq_34446253
  • 2016-08-24 14:01:23
  • 107

POJ 2553 The Bottom of A Graph

强连通分量的应用,要看清题意
  • scorpiocj
  • scorpiocj
  • 2011-01-17 10:09:00
  • 326

poj 2553 The Bottom of a Graph

这个题的题意真是醉了 一个点是sink不是说他和任一点互相可达,而是如果他能到达一个点的情况下,那个点必须能够到达他 = =。这样的话就是强连通缩点之后,出度为零的点及其强连通分量中的所有点都是sin...
  • Unin88
  • Unin88
  • 2015-03-12 21:46:41
  • 237

POJ 2553 The Bottom of a Graph

Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 10687   Accepted: 4403 ...
  • qq_35776409
  • qq_35776409
  • 2017-03-17 16:17:05
  • 56
收藏助手
不良信息举报
您举报文章:POJ 2553 The Bottom of a Graph - from lanshui_Yang
举报原因:
原因补充:

(最多只允许输入30个字)