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. POJ 2356 Find a multiple( 鸽巢定理简单题 )

    链接:传送门 题意:题意与3370类似 注意:注意输出就ok,输出的是集合的值不是集合下标 /***************************************************** ...

  2. Label,PushButton,ToolButton 实现动态图片按钮,Label显示gif动画

    .h文件 public: explicit event(QWidget *parent = 0); ~event(); QImage image; QLabel *label; QLabel *lab ...

  3. python课堂知识的几点总结

    1.执行python脚本的两种方式:利用python进入解释器和找到可执行文件1.py 2.位和字节:一个字节是8位,计算机运行时是以字节为单位,存储的时候是以位为单位 3.编码发展:ASCII只有0 ...

  4. js实现新闻滚动实例

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  5. windows下使用libsvm3.2

    一.官方介绍 libsvm主页:https://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html libsvm介绍文档:http://www.csie.ntu. ...

  6. C++的标准模板库STL中实现的数据结构之链表std::list的分析与使用

    摘要 本文主要借助对C++的标准模板库STL中实现的数据结构的学习和使用来加深对数据结构的理解,即联系数据结构的理论分析和详细的应用实现(STL),本文是系列总结的第二篇.主要针对线性表中的链表 ST ...

  7. UESTC 1143 数据传输 网络流 最大流 Dinic

    数据传输 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  Sta ...

  8. node.js mongodb ReplSet

    随着web2.0兴起,高并发大数据量的应用对数据库高速响应的性能要求日趋明显,传统的关系型数据库在这方面显得有些乏力.有矛自有盾,内存DB的出现弥补了传统关系型db的不足.眼下市面流行的内存db主要有 ...

  9. Hibernate的xml方法配置和操作代码

    一.gradle中包: compile group: 'org.hibernate', name: 'hibernate-core', version: '5.2.12.Final' compile ...

  10. [LeetCOde][Java] Best Time to Buy and Sell Stock III

    题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...