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

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 Gand 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
题意:给定一幅有向图,若某点所能到达的点也能到达其本身,那么这个点为sink。由小到大输出sink.
思路:有向图缩点得到一棵树,答案为构成叶子(出度为0)结点的连通分量。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=;
vector<int> mp[MAXN];
int n,m;
int dfn[MAXN],low[MAXN],time;
int stack[MAXN],top;
bool ins[MAXN];
int belong[MAXN],cnt;
void dfs(int u)
{
dfn[u]=low[u]=++time;
stack[top++]=u;
ins[u]=true;
for(int i=;i<mp[u].size();i++)
{
int v=mp[u][i];
if(!dfn[v])
{
dfs(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v]) low[u]=min(low[u],dfn[v]);
}
if(dfn[u]==low[u])
{
int v;
cnt++;
do{
v=stack[--top];
belong[v]=cnt;
ins[v]=false;
}while(u!=v);
}
}
int deg[MAXN];
bool flag[MAXN];
void solve()
{
/*
for(int i=1;i<=n;i++)
printf("%d\n",belong[i]);*/ for(int i=;i<=n;i++)
for(int j=;j<mp[i].size();j++)
{
int v=mp[i][j];
if(belong[i]!=belong[v])
{
deg[belong[i]]++;
}
}
for(int i=;i<=cnt;i++)
if(deg[i]==)
flag[i]=true; for(int i=;i<=n;i++)
if(flag[belong[i]])
printf("%d ",i);
printf("\n");
}
int main()
{
while(scanf("%d",&n)!=EOF&&n)
{
scanf("%d",&m);
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(ins,false,sizeof(ins));
memset(deg,,sizeof(deg));
memset(flag,false,sizeof(flag));
top=;
time=;
cnt=;
for(int i=;i<=n;i++)
mp[i].clear();
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
mp[u].push_back(v);
}
for(int i=;i<=n;i++)
if(!dfn[i])
dfs(i);
solve();
}
}

下面是kosaraju算法

#include"cstdio"
#include"cstring"
#include"vector"
using namespace std;
const int MAXN=;
vector<int> G[MAXN];
vector<int> rG[MAXN];
vector<int> vs;
int V,E; int cpnt[MAXN];
int vis[MAXN];
void dfs(int u)
{
vis[u]=;
for(int i=;i<G[u].size();i++)
if(!vis[G[u][i]]) dfs(G[u][i]);
vs.push_back(u);
} void rdfs(int u,int k)
{
cpnt[u]=k;
vis[u]=;
for(int i=;i<rG[u].size();i++)
if(!vis[rG[u][i]]) rdfs(rG[u][i],k);
} void scc()
{
memset(vis,,sizeof(vis));
for(int i=;i<=V;i++)
if(!vis[i]) dfs(i);
memset(vis,,sizeof(vis));
int k=;
for(int i=vs.size()-;i>=;i--)
if(!vis[vs[i]]) rdfs(vs[i],k++);
} int deg[MAXN];
void solve()
{
scc();
for(int i=;i<=V;i++)
{
for(int j=;j<G[i].size();j++)
{
int to=G[i][j];
if(cpnt[i]!=cpnt[to])
{
deg[cpnt[i]]++;
}
}
}
int flag=;
for(int i=;i<=V;i++)
{
if(deg[cpnt[i]]==)
{
if(flag==)
{
printf("%d",i);
flag=;
}
else
{
printf(" %d",i);
}
}
}
printf("\n");
}
int main()
{
while(scanf("%d",&V)!=EOF&&V)
{
scanf("%d",&E);
vs.clear();
memset(cpnt,,sizeof(cpnt));
memset(deg,,sizeof(deg));
for(int i=;i<=V;i++)
{
G[i].clear();
rG[i].clear();
}
for(int i=;i<E;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
rG[v].push_back(u);
}
solve();
} return ;
}

POJ2553( 有向图缩点)的更多相关文章

  1. poj2553 有向图缩点,强连通分量。

    //求这样的sink点:它能达到的点,那个点必能达到他,即(G)={v∈V|任意w∈V:(v→w)推出(w→v)} //我法:tarjan缩点后,遍历点,如果该点到达的点不在同一个强连通中,该点排除, ...

  2. hdu 3072 有向图缩点成最小树形图计算最小权

    题意,从0点出发,遍历所有点,遍历边时候要付出代价,在一个SCC中的边不要付费.求最小费用. 有向图缩点(无需建立新图,,n<=50000,建则超时),遍历边,若不在一个SCC中,用一个数组更新 ...

  3. HDU1269(有向图缩点模板题)

    迷宫城堡 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  4. POJ2186(有向图缩点)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28379   Accepted: 11488 De ...

  5. POJ1904(有向图缩点+输入输出挂参考)

    King's Quest Time Limit: 15000MS   Memory Limit: 65536K Total Submissions: 8311   Accepted: 3017 Cas ...

  6. hdu 1827 有向图缩点看度数

    题意:给一个有向图,选最少的点(同时最小价值),从这些点出发可以遍历所有. 思路:先有向图缩点,成有向树,找入度为0的点即可. 下面给出有向图缩点方法: 用一个数组SCC记录即可,重新编号,1.... ...

  7. HDU 4635 (完全图 和 有向图缩点)

    题目链接:HDU  4635 题目大意: 给你一个有向图,加有向边,使得这个图是简单有向图.问你最多加多少条有向边. 简单有向图: 1.不存在有向重边. 2.不存在图循环.(注意是不存在 “图” 循环 ...

  8. 对Tarjan——有向图缩点算法的理解

    开始学tarjan的时候,有关无向图的割点.桥.点双边双缩点都比较容易地理解了,唯独对有向图的缩点操作不甚明了.通过对luoguP2656_采蘑菇一题的解决,大致搞清了tarjan算法的正确性. 首先 ...

  9. hdu 3639 有向图缩点+建反向图+搜索

    题意:给个有向图,每个人可以投票(可以投很多人,一次一票),但是一个人只能支持一人一次,支持可以传递,自己支持自己不算,被投支持最多的人. 开始想到缩点,然后搜索,问题是有一点想错了!以为支持按票数计 ...

随机推荐

  1. shell 获取当前svn代码目录版本号

    在当前svn代码目录下执行以下命令: svn info | grep "Last Changed Rev:" | awk -F ': ' '{print $2}' > svn ...

  2. 【文献阅读】Augmenting Supervised Neural Networks with Unsupervised Objectives-ICML-2016

    一.Abstract 从近期对unsupervised learning 的研究得到启发,在large-scale setting 上,本文把unsupervised learning 与superv ...

  3. NDK以及C语言基础语法(一)

    一.什么是NDK? Native Development Kit (本地开发工具包): NDK中提供了一系列的工具,帮助我们快速开发C/C++的动态库,并能自动将so文件和java文件一起打包成apk ...

  4. Win10上Python3通过pip安装时出现UnicodeDecodeError

    http://blog.csdn.net/qq_33530388/article/details/68933201 解决方法: 打开 c:\program files\python36\lib\sit ...

  5. 错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)【转】

    启动Tomcat服务器报错: Several ports (8005, 8080, 8009) required by Tomcat v5.5 Server at localhost are alre ...

  6. 【BZOJ4197】[Noi2015]寿司晚宴 状压DP+分解质因数

    [BZOJ4197][Noi2015]寿司晚宴 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴 ...

  7. EasyPlayer windows RTSP播放器OCX插件使用说明

    鉴于大家对于EasyPlayer插件的使用还不太熟悉,特此写一篇插件的使用文档,供大家参考:EasyPlayer插件有两种,一种是基于IE的ActiveX控件,一种是基于FireFox(也支持多浏览器 ...

  8. windows系统下nodejs、npm、express的下载和安装教程——2016.11.09

    1. node.js下载 首先进入http://nodejs.org/dist/,这里面的版本呢,几乎每个月都出几个新的,建议大家下载最新版本,看看自己的电脑是多少位的,别下错了. 下载完解压到你想放 ...

  9. 【docker】学习笔记一:制作自己的centos6.9镜像

    前言: 最近开始研究docker,在这里做一个记录. 本来开始想用centos7系列做镜像,毕竟是最新版本的centos,但是centos7有一个严重的bug,就是正常启动的镜像不能使用systemc ...

  10. python之virtualenv 与 virtualenvwrapper 详解

    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...