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. Map输出数据的处理类MapOutputBuffer分析

    MapOutputBuffer顾名思义就是Map输出结果的一个Buffer,用户在编写map方法的时候有一个参数OutputCollector: void map(K1 key, V1 value, ...

  2. 【WPF学习笔记】之如何点击“新建”按钮,在面板中加载一条条的“用户控件”的信息:动画系列之(四)

    ...... 承接上一系列动画三. 在主界面后台代码设置嵌套第二个用户控件. using System; using System.Collections.Generic; using System. ...

  3. JavaWeb学习总结第三篇--走进JSP页面元素

    JavaWeb学习(三)—走进JSP页面元素 JSP:Java Server Pages,译为Java服务器页面.其脚本采用Java语言,继承了Java所有优点.JSP元素可以分为指令元素.脚本元素和 ...

  4. 对你的 REST API 进行保护的正确办法

    设计好一个美丽的 REST + JSON API 之后,怎样对你的 API 进行保护?在 Stormpath,我们花了 18 个月来寻找最佳实践.将其一一实践于 Stormpath API 中并分析其 ...

  5. php部分--题目:投票 重点:两个div套用,显示百分比;

    1.建立两个表格:要显示百分比的话,就要在选项表中加上一列标记number 2.链接数据库,并对题目和选项进行显示 <?php $db=new MySQLi("localhost&qu ...

  6. 微信小程序设计指南

    微信小程序设计指南 · 小程序 https://developers.weixin.qq.com/miniprogram/design/index.html

  7. The template root requires exactly one element

    The template root requires exactly one element

  8. JavaScript 四种显示数据方式

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. Elasticsearch5 及 head插件 安装说明

    Elasticsearch5.X及 head插件 安装说明: 1.下载elasticsearch安装文件: a) 下载官方源码: https://artifacts.elastic.co/downlo ...

  10. 一次跨域请求出现 OPTIONS 请求的问题及解决方法

    问题背景浏览器从一个域名的网页去请求另一个域名的资源时,域名.端口.协议任一不同,都是跨域 在前后端开发过程经常会遇到跨域问题.网上也都有解决方案. 写这篇文章时,我们碰到的一个场景是:要给s系统做一 ...