http://poj.org/problem?id=2553

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 10987   Accepted: 4516

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. ThenG=(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+1is 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

 
模板题,调了三天(崩溃)很无奈

 
 #include <algorithm>
#include <cstring>
#include <cstdio> using namespace std; const int MAXN();
const int N();
int n,m,u,v;
int sumedge,head[N];
struct Edge
{
int to,next;
Edge(int to=,int next=) :
to(to),next(next) {}
}edge[MAXN]; void ins(int from,int to)
{
edge[++sumedge]=Edge(to,head[from]);
head[from]=sumedge;
} int low[N],dfn[N],tim;
int Stack[N],instack[N],top;
int sumcol,col[N],point[N]; void DFS(int now)
{
low[now]=dfn[now]=++tim;
Stack[++top]=now; instack[now]=true;
for(int i=head[now];i;i=edge[i].next)
{
int go=edge[i].to;
if(!dfn[go])
DFS(go),low[now]=min(low[now],low[go]);
else if(instack[go]) low[now]=min(low[now],dfn[go]);
}
if(low[now]==dfn[now])
{
col[now]=++sumcol;
for(;Stack[top]!=now;top--)
{
col[Stack[top]]=sumcol;
instack[Stack[top]]=false;
}
instack[now]=false; top--;
}
} int cnt,ans[N],chudu[N]; void init()
{
tim=top=cnt=;
sumcol=sumedge=;
memset(ans,,sizeof(ans));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(col,,sizeof(col));
memset(head,,sizeof(head));
memset(chudu,,sizeof(chudu));
memset(Stack,,sizeof(Stack));
memset(instack,,sizeof(instack));
} int main()
{
/*freopen("made.txt","r",stdin);
freopen("myout.txt","w",stdout);*/ while(~scanf("%d",&n)&&n)
{
scanf("%d",&m); init();
for(;m;m--)
scanf("%d%d",&u,&v),ins(u,v);
for(int i=;i<=n;i++)
if(!dfn[i]) DFS(i);
for(u=;u<=n;u++)
for(v=head[u];v;v=edge[v].next)
if(col[u]!=col[edge[v].to]) chudu[col[u]]++;
/*for(int sc=1;sc<=sumcol;sc++)
if(!chudu[sc])
for(int i=1;i<=n;i++)
if(sc==col[i]) ans[++cnt]=i;*/
for(int i=;i<=n;i++)
if(!chudu[col[i]]) ans[++cnt]=i;
sort(ans+,ans+cnt+);
if(cnt)
{
for(int i=;i<cnt;i++) printf("%d ",ans[i]);
printf("%d\n",ans[cnt]);
}
else printf("\n");
}
return ;
}

POJ——T2553 The Bottom of a Graph的更多相关文章

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

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

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

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

  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【强连通分量求汇点个数】

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

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

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

  6. poj 2553 The Bottom of a Graph : tarjan O(n) 存环中的点

    /** problem: http://poj.org/problem?id=2553 将所有出度为0环中的点排序输出即可. **/ #include<stdio.h> #include& ...

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

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

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

  9. poj 2553 The Bottom of a Graph

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

随机推荐

  1. HDU 2604 Queuing( 递推关系 + 矩阵快速幂 )

    链接:传送门 题意:一个队列是由字母 f 和 m 组成的,队列长度为 L,那么这个队列的排列数为 2^L 现在定义一个E-queue,即队列排列中是不含有 fmf or fff ,然后问长度为L的E- ...

  2. Lapack下载安装

    安装 1.下载压缩文件 wget http://www.netlib.org/lapack/lapack-3.8.0.tar.gz 2.解压缩 tar -zxvf lapack-3.8.0.tar.g ...

  3. php读写excel —— PhpSpreadsheet组件

    前言 PhpSpreadsheet是一个纯PHP类库,它提供了一组类,允许您从不同的电子表格文件格式(如Excel和LibreOffice Calc)读取和写入.用PHP读取Excel.CSV文件 还 ...

  4. 2019-03-20 Python爬取需要登录的有验证码的网站

    当你向验证码发起请求的时候,就有session了,记录下这次session 因为每当你请求一次验证码 或者 请求一次登录首页,验证码都在变动 验证码的链接可能不是固定的,可能需要GET/POST请求, ...

  5. Struts2SpringHibernate整合示例,一个HelloWorld版的在线书店(项目源码+详尽注释+单元测试)

    Struts2,Spring,Hibernate是Java Web开发中最为常见的3种框架,掌握这3种框架是每个Java Web开发人员的基本功. 然而,很多初学者在集成这3个框架的时候,总是会遇到各 ...

  6. Nuxt开发经验分享

    Nuxt开发经验分享 本文章基于starter-template模板进行讲解,面向有vue-cli开发经验的宝宝 vue init nuxt-community/starter-template   ...

  7. 异构关系数据库(Sqlserver与MySql)之间的数据类型转换参考

    一.SqlServer到MySql的数据类型的转变 编号 SqlServer ToMySql MySql 1 binary(50) LONGBLOB binary 2 bit CHAR(1) bit ...

  8. Mysql优化之优化工具profiling

    程序员的成长之路 2016-11-23 22:42 Mysql优化之优化工具profiling 前言 mysql优化技术: mysql优化不是做一个操作就可以的优化,它包含很多的细节,需要一点一点的优 ...

  9. Nginx Zabbix

    https://www.cnblogs.com/wangxiaoqiangs/archive/2016/04/20/5412111.html

  10. cogs 466. [NOIP2009] 细胞分裂

    466. [NOIP2009] 细胞分裂 ★★   输入文件:cell.in   输出文件:cell.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]    Hanks ...