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. [COCI2007]PRAVOKUTNI

    题目大意:在一个平面上,有\(N\)个点,求这些点构成的直角三角形个数.解题思路:枚举直角顶点,对于每个点,将这个点当做原点,对其他点按极角排序,然后双指针扫一遍,判断弧度差即可. C++ Code: ...

  2. Python 绘图与可视化 matplotlib 制作Gif动图

    参考链接:https://blog.csdn.net/theonegis/article/details/51037850 官方文档:https://matplotlib.org/3.1.0/api/ ...

  3. tp volist需要便利两个数组时的处理办法

    你需要便利两个数组,并且需要使用key 和value的试的时候,volist是否先得有些捉鸡? 我们可以便利其中一个数组,而另一个利用数组的指针来操作 next($arr) 将数组指针下移 key($ ...

  4. HDU - 4758 Walk Through Squares (AC自己主动机+DP)

    Description   On the beaming day of 60th anniversary of NJUST, as a military college which was Secon ...

  5. 取消记录tableView选中效果

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { [self. ...

  6. USACO runaround

    /* ID:kevin_s1 PROG:runround LANG:C++ */ #include <iostream> #include <cstdio> #include ...

  7. JDBC-Statement 对象

    Statement 对象 一旦我们获得了数据库的连接,我们就可以和数据库进行交互.JDBC 的 Statement,CallableStatement 和 PreparedStatement 接口定义 ...

  8. libLAS1.8.0 编译和配置(VS2013+Win7 64)(一)

    libLAS 是一个用来读写三维激光雷达数据(LiDAR) 的 C++ 库.在学习.科研和研发中都会广泛运用.怎样编译和配置自己所须要版本号的libLAS库确是一件麻烦耗时的事情. 笔者在Win7 6 ...

  9. 通过setInterval函数在地图上每隔1s打一次点

    <?php echo <<<_END <!doctype html> <html> <head> <meta charset=&quo ...

  10. bzoj4554: [Tjoi2016&Heoi2016]游戏(二分图匹配)

    4554: [Tjoi2016&Heoi2016]游戏 题目:传送门 题解: 一道很牛逼的匈牙利..和之前模拟赛的一道题有点相似(不过这题不用完美匹配) 我们可以把连续的行和列全部编号(如果之 ...