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

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 v1to 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

思路:

终于过了。。。。

因为模板错误,让我痛不欲生。

这题完成缩点之后,找出没有出度的点就行了。

http://www.cnblogs.com/ZGQblogs/p/9104381.html

我的模板会在此更新,感谢感谢!

我的上一篇博客里面的代码(POJ 1236)的确是错的,这个里面的代码应该就没问题了。。

代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<stack>
#include<cstring>
using namespace std;
int n,m;
int book[50008];
int low[50008],num[50008],cnt=1,index;
int color[50008];
bool flag[50008];
vector<int>u[50008];
stack<int>st;
int sig=0;
void Tarjan(int t)
{
num[t]=low[t]=++index;
st.push(t);
book[t]=true;
int siz=u[t].size();
for(int i=0;i<siz;i++){
if(!num[u[t][i]]){
Tarjan(u[t][i]);
low[t]=min(low[t],low[u[t][i]]);
}
else if(book[u[t][i]]){low[t]=min(low[t],low[u[t][i]]);}
} if(num[t]==low[t]){
sig++;
while(1){ cnt=st.top();
st.pop();
color[cnt]=sig;
book[cnt]=0;
if(cnt==t){break;}
}
}
} bool init()
{
scanf("%d",&n);
for(int i=1;i<=n;i++){
u[i].clear();
}
while(!st.empty()){
st.pop();
}
memset(book,0,sizeof(book));
memset(low,0,sizeof(low));
memset(flag,0,sizeof(flag));
memset(color,0,sizeof(color));
memset(num,0,sizeof(num));
index=0;
if(n==0){return false;}
scanf("%d",&m);
int x,y;
for(int i=1;i<=m;i++){
scanf("%d%d",&x,&y);
u[x].push_back(y);
}
return true;
} void solve()
{
int siz;
int tle=0;
for(int i=1;i<=n;i++){
siz=u[i].size();
for(int j=0;j<siz;j++){
if(color[u[i][j]]!=color[i]){flag[color[i]]=true;}
}
} for(int i=1;i<=n;i++){
if(!flag[color[i]]){
tle++?printf(" %d",i):printf("%d",i);
}
}
printf("\n");
} int main()
{
while(init()){
for(int i=1;i<=n;i++){
if(!num[i]){Tarjan(i);cnt++;}
}
solve();
}
}

  

POJ 2553 The Bottom of a Graph (Tarjan)的更多相关文章

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

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

  2. POJ 2553 The Bottom of a Graph TarJan算法题解

    本题分两步: 1 使用Tarjan算法求全部最大子强连通图.而且标志出来 2 然后遍历这些节点看是否有出射的边,没有的顶点所在的子强连通图的全部点,都是解集. Tarjan算法就是模板算法了. 这里使 ...

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

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

  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(强连通分量+缩点)

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

  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 (强连通分量)

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

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

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

  9. POJ 2553 The Bottom of a Graph 【scc tarjan】

    图论之强连通复习开始- - 题目大意:给你一个有向图,要你求出这样的点集:从这个点出发能到达的点,一定能回到这个点 思路:强连通分量里的显然都可以互相到达 那就一起考虑,缩点后如果一个点有出边,一定不 ...

随机推荐

  1. SpringMVC中对多部件类型解析---文件(图片)上传

    加入上传图片jar包 commons-io-2.4.jar commons-fileupload-1.3.jar 在页面form中提交enctype="multipart/form-data ...

  2. ES 6 系列 - Proxy

    Proxy 用于修改某些操作的默认行为,等同于在语言层面做出修改,所以是一种“元编程”,即对编程语言进行编程. 简单地理解,就是在目标对象之前假设一层“拦截”,外界对改对象的访问,都必须先通过这层拦截 ...

  3. CSS遮罩效果和毛玻璃效果

    前面的话 本文将详细介绍CSS遮罩效果和毛玻璃效果 遮罩效果 普通遮罩 一般地,处理全屏遮罩的方法是使用额外标签 <style>.overlay{ position:fixed; top: ...

  4. codevs2822

    解题思路: tarjan缩点后算出度为0的点有几个,如果只有一个且这个点为爱心天使就行了: #include<iostream> #include<algorithm> #in ...

  5. BZOJ3223文艺平衡树——非旋转treap

    此为平衡树系列第二道:文艺平衡树您需要写一种数据结构,来维护一个有序数列,其中需要提供以下操作: 翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1 ...

  6. windows查看进程占用并强制结束进程

    打开命令提示符(CMD) 查看8080端口被哪个进程占用了,命令:netstat   -ano|findstr 8080 上面的8080端口的PID是30160,可以根据PID可以杀死这个进程,用下面 ...

  7. 【XSY2472】string KMP 期望DP

    题目大意 给定一个由且仅由字符'H','T'构成的字符串\(S\). ​ 给定一个最初为空的字符串\(T\) ,每次随机地在\(T\)的末尾添加'H'或者'T'. 问当\(S\)为\(T\)的后缀时, ...

  8. studio 连不上远程仓库的各种原因分析

    Unable to open the project 1.远程服务器挂了2.网络断了3.登录远程服务器的账号.密码错了4.远程仓库的url地址,被本地的hosts文件重定向了5.要下载远程仓库的某个j ...

  9. c# Redis 使用

    1.服务端两个版本窗口版与安装windows服务版 1.1.窗口版 下载地址:https://github.com/dmajkic/redis/downloads redis-server.exe:服 ...

  10. 爬虫_中国天气网_文字天气预报(xpath)

    import requests from lxml import etree headers = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/5 ...