题意:给出一个有向图G,寻找所有的sink点。“sink”的定义为:{v∈V|∀w∈V:(v→w)⇒(w→v)},对于一个点v,所有能到达的所有节点w,都能够回到v,这样的点v称为sink。

分析:由(v→w),(w→v)可知,节点v,w构成强连通,很自然的想到要缩点。缩点之后,DAG上的每一条边,都是单向的(v->w),无回路(w->v)。

错误:对于v可达的点w,不仅是直接连边——从一个强连通子集A到另一个强连通子集B,意味着,子集A中的点都不可能是sink点。

 #include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std; const int MAXN=; struct Edge{
int v,next;
Edge(){}
Edge(int _v,int _next):v(_v),next(_next){}
}edge[MAXN*MAXN]; int head[MAXN],tol;
int pre[MAXN],low[MAXN],sccno[MAXN],dfs_clock,scc_cnt;
int vis[MAXN]; stack<int>stk; void init(int n)
{
tol=;
memset(head,-,sizeof(head));
} void add(int u,int v)
{
edge[tol]=Edge(v,head[u]);
head[u]=tol++;
} void dfs(int u)
{
int v;
pre[u]=low[u]=++dfs_clock;
stk.push(u);
for(int i=head[u];i!=-;i=edge[i].next)
{
v=edge[i].v;
if(!pre[v]){
dfs(v);
low[u]=min(low[u],low[v]);
}else if(!sccno[v])
low[u]=min(low[u],pre[v]);
}
if(pre[u]==low[u]){
scc_cnt++;
do{
v=stk.top();
stk.pop();
sccno[v]=scc_cnt;
}while(u!=v);
}
} void find_scc(int n)
{
dfs_clock=scc_cnt=;
memset(pre,,sizeof(pre));
memset(low,,sizeof(low));
memset(sccno,,sizeof(sccno)); for(int i=;i<=n;i++)
if(!pre[i])
dfs(i);
} void check(int n)
{
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
for(int j=head[i];j!=-;j=edge[j].next)
{
int v=edge[j].v;
if(sccno[i]!=sccno[v])
vis[sccno[i]]=;
}
}
} void print(int n)
{
int cnt=;
for(int i=;i<=n;i++)
if(!vis[sccno[i]]){
if(cnt==)printf("%d",i);
else printf(" %d",i);
cnt++;
}
printf("\n");
} int main()
{
int n,m;
while(~scanf("%d",&n))
{
if(!n)
return ;
scanf("%d",&m); init(n);
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
} find_scc(n); check(n); print(n);
}
return ;
}

poj 2553 The Bottom of a Graph(强连通、缩点、出入度)的更多相关文章

  1. poj 2553 The Bottom of a Graph(强连通分量+缩点)

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

  2. POJ 2553 The Bottom of a Graph (强连通分量)

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

  3. 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 ...

  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【强连通分量求汇点个数】

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

  6. POJ 2553 The Bottom of a Graph (Tarjan)

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

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

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

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

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

  9. POJ 2553 The Bottom of a Graph(强连通分量的出度)

    题意: 求出图中所有汇点 定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径:若v不可以到达u,则u到v的路径可有可无. 模板:http://www.cnblogs.co ...

随机推荐

  1. 理解Java垃圾回收

    stop-the-world 原文链接:http://www.cubrid.org/blog/de... 了解Java的垃圾回收(GC)原理能给我们带来什么好处?对于软件工程师来说,满足技术好奇心可算 ...

  2. jQuery滑动方法

    jQuery 滑动方法 通过 jQuery,您可以在元素上创建滑动效果. jQuery 拥有以下滑动方法: slideDown() slideUp() slideToggle() jQuery sli ...

  3. linux-shell父子进程

          用户登录到Linux系统后,系统将启动一个用户shell.在这个shell中,可以使用shell命令声明变量,也可以创建并运行 shell脚本程序.运行shell脚本程序时,系统将创建一个 ...

  4. sourceinsight----tabsiplus颜色配置文件

    参考: http://blog.csdn.net/orbit/article/details/7585607 下面是我的颜色配置 http://files.cnblogs.com/pengdongli ...

  5. CENTOS设置MYSQL字符集

    CENTOS设置MYSQL字符集 show variables like 'character_set%'; vim etc/my.cnf 修改成如下: [client] port=3306 sock ...

  6. ASIHTTPRequest框架使用总结系列之阿堂教程4(下载数据)

    从本篇开始,阿堂准备进一步介绍ASIHTTPRequest框架下载数据和上传数据的实际应用.        为了实现多线程并发请求网络能力,ASIHTTPRequest被设计成 NSOperation ...

  7. Signing Identities, Missing Private Key, Cannot sign App

    这个问题发生在重新安装系统后,丢失了之前的private key等.所以解决方法就是提示的revoke and request. 到developer center中找到certificate中对应的 ...

  8. 使用ASIHTTPRequest 编译提示找不到"libxml/HTMLparser.h"的解决方法

    使用ASIHTTPRequest xcode编译提示找不到"libxml/HTMLparser.h",解决方法如下: 1>.在xcode中左边选中项目的root节点,在中间编 ...

  9. select自己定义属性值

    select自己定义属性值 1.问题背景 下拉框能够传递值和内容,只是有时为了传值,还须要连带其它的值也一起传过来.假设用title属性.鼠标移到下拉框上方会显示出来,这样就会导致被暴露出来.所以,为 ...

  10. 淘宝开源项目之Tsar

    软件介绍: Tsar是淘宝开发的一个非常好用的系统监控工具,在淘宝内部大量使用,它不仅可以监控CPU.IO.内存.TCP等系统状态,也可以监控Apache,Nginx/Tengine,Squid等服务 ...