题目地址:http://poj.org/problem?id=2553

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

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

Source

 
【题解】:
  找到入度为0的所有强连通分量,将其中的点排序后输出
  这题跟poj 2186 很类似
  
【code】:
  

 /**
Judge Status:Accepted Memory:1488K
Time:125MS Language:G++
Code Length:2443B Author:cj
*/
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<vector>
#include<algorithm> #define N 10010
using namespace std; vector<int> G[N];
stack<int> stk;
int pre[N],lowlink[N],sccno[N],scc_cnt,dfn_clock,out[N],counter[N]; void DFN(int u) //tarjan算法
{
lowlink[u] = pre[u] = ++dfn_clock;
stk.push(u);
int i;
for(i=;i<G[u].size();i++)
{
int v = G[u][i];
if(!pre[v])
{
DFN(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
{
lowlink[u] = min(lowlink[u],pre[v]);
}
}
if(lowlink[u]==pre[u])
{
scc_cnt++; //强连通图的个数标记
while()
{
int x = stk.top();
stk.pop();
sccno[x] = scc_cnt;
if(x==u) break;
}
}
} void findscc(int n)
{
int i;
scc_cnt = dfn_clock = ;
memset(pre,,sizeof(pre));
memset(lowlink,,sizeof(lowlink));
memset(sccno,,sizeof(sccno));
for(i=;i<=n;i++)
if(!pre[i])
DFN(i);
} int main()
{
int n,m;
while(~scanf("%d",&n)&&n)
{
scanf("%d",&m);
int i;
for(i=;i<=n;i++)
G[i].clear();
for(i=;i<m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
G[a].push_back(b); // 得到图
}
findscc(n); //查找强连通图
int j;
memset(out,,sizeof(out));
memset(counter,,sizeof(counter)); for(i=;i<=n;i++) //遍历一边图,查找统计个点缩点后的出度
{
for(j=;j<G[i].size();j++)
{
int v = G[i][j];
if(sccno[i]!=sccno[v])
{
out[sccno[i]]++; //出度
}
}
}
for(i=;i<=scc_cnt;i++)
{
if(!out[i]) //出度为0的强连通分量
{
counter[i] = ; //标记
}
} int pl = ;
for(i=;i<=n;i++)
if(counter[sccno[i]]) //是否被标记,从下到大
{
if(pl) printf(" %d",i);
else printf("%d",i);
pl = ;
}
putchar();
}
return ;
}

poj 2553 The Bottom of a Graph(强连通分量+缩点)的更多相关文章

  1. POJ 2553 The Bottom of a Graph (强连通分量)

    题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...

  2. 【poj2553】The Bottom of a Graph(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2553 [题意] 给n个点m条边构成一幅图,求出所有的sink点并按顺序输出.sink点是指该点能到达的点反过来又能回到该点. [思路] ...

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

  4. POJ 2553 The Bottom of a Graph(强连通分量)

    POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...

  5. poj 2553 The Bottom of a Graph

    求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点 ...

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

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

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

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

  8. POJ 2553 The Bottom of a Graph(强连通分量的出度)

    题意: 求出图中所有汇点 定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径:若v不可以到达u,则u到v的路径可有可无. 模板:http://www.cnblogs.co ...

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

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

随机推荐

  1. hdu 3743 树状数组

    思路:我们只需坚守一个原则,本来就在左边的坚决不把它换到右边.也就是相邻的两个数,左边小,右边大,那么就不调换.这样对每个数,只要统计左边比它大的数的个数.可以从后面开始用树状数组统计比它小的数的个数 ...

  2. poj 1201 差分约束

    http://www.cnblogs.com/wangfang20/p/3196858.html 题意: 求集合Z中至少要包含多少个元素才能是每个区间[ai,bi]中的元素与Z中的元素重合个数为ci. ...

  3. Swift 性能探索和优化分析

    本文首发在 CSDN<程序员>杂志,订阅地址 http://dingyue.programmer.com.cn/. Apple 在推出 Swift 时就将其冠以先进,安全和高效的新一代编程 ...

  4. 改变xmind显示中文界面

    最近打算迁移到ubuntu系统做为主系统,打算使用xmind这个工具,因为我的ubuntu并没有使用简体中文,xmind是根据系统的语种来自动选择的,所以也直接为英文界面,实在使用不习惯,查到了这篇文 ...

  5. Ehcache(2.9.x) - API Developer Guide, Blocking and Self Populating Caches

    About Blocking and Self-Populating Caches The net.sf.ehcache.constructs package contains some applie ...

  6. Linux 命令 - scp: 远程文件拷贝

    scp 与普通的文件复制命令 cp 类似,而它们之间最大的差别在于 scp 命令的源或目标文件是远程文件. 命令格式 scp [options] [[user@]host1:]file1 ... [[ ...

  7. Redis 命令 - Lists

    BLPOP key [key ...] timeout Remove and get the first element in a list, or block until one is availa ...

  8. sql中在查询时两列合并为一列

    select  a + ' '+b  as ab from tableName 如果是有整型的(例,b字段为int类型): select a +' '+Convert(VARCHAR(50),b) a ...

  9. ios Swift 算法

    // Playground - noun: a place where people can play import Cocoa var nums = Int[]() ... { nums.appen ...

  10. MyEclipse自动提示

    MyEclipse自动提示 Eclipse中默认是输入"."后出现自动提示,用于类成员的自动提示,可是有时候我们希望它能在我们输入类的. 首字母后就出现自动提示,可以节省大量的输入 ...