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. linux下proc里关于磁盘性能的参数(转)

    我们在磁盘写操作持续繁忙的服务器上曾经碰到一个特殊的性能问题.每隔 30 秒,服务器就会遇到磁盘写活动高峰,导致请求处理延迟非常大(超过3秒).后来上网查了一下资料,通过调整内核参数,将写活动的高峰分 ...

  2. 详解spring boot实现多数据源代码实战

    之前在介绍使用JdbcTemplate和Spring-data-jpa时,都使用了单数据源.在单数据源的情况下,Spring Boot的配置非常简单,只需要在application.propertie ...

  3. 开发及应用中 Linux与Window 取舍

    Linux是开源的,而Windows不是.这个也是Linux与Windows之间最大的差异.一般来说,开源似乎收到了更多系统管理员的亲睐,而开源的软件似乎更受个人电脑用户的欢迎.两种类型之间有很多不同 ...

  4. tomcat端口问题

    https://segmentfault.com/q/1010000008858162?_ea=1777730

  5. VI带行号查看

        :set nu         带行号查看,并不改变文件内容 :set nonu     取消带行号查看 在每个用户的主目录下,都有一个 vi 的配置文件".vimrc"或 ...

  6. JS深入理解系列(一):编写高质量代码

    在for循环中,你可以循环取得数组或是数组类似对象的值,譬如arguments和HTMLCollection对象.通常的循环形式如下: // 次佳的循环for (var i = 0; i < m ...

  7. Kubernetes TensorFlow 默认 特定 集群管理器

    Our goal is to foster an ecosystem of components and tools that relieve the burden of running applic ...

  8. API的理解和使用——全局命令

    全局命令 命令 功能 set 创建键值对 keys 遍历查看所有键 exists 判断一个键是否存在,1存在,0不存在 dbsize 当前数据库中有多少个键 del 删除一个或多个键 expire 设 ...

  9. Java for LeetCode 110 Balanced Binary Tree

    Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...

  10. PAT 天梯赛 L2-028. 秀恩爱分得快 【数据处理】

    题目链接 https://www.patest.cn/contests/gplt/L2-028 思路 0.只处理被询问的情侣的亲密度,否则会超时 1.要注意输入数字要用字符串,还要标记性别 因为 输出 ...