poj 2553 The Bottom of a Graph(强连通、缩点、出入度)
题意:给出一个有向图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(强连通、缩点、出入度)的更多相关文章
- poj 2553 The Bottom of a Graph(强连通分量+缩点)
题目地址:http://poj.org/problem?id=2553 The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K ...
- POJ 2553 The Bottom of a Graph (强连通分量)
题目地址:POJ 2553 题目意思不好理解.题意是:G图中从v可达的全部点w,也都能够达到v,这种v称为sink.然后升序输出全部的sink. 对于一个强连通分量来说,全部的点都符合这一条件,可是假 ...
- 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 ...
- POJ 2553 The Bottom of a Graph(强连通分量)
POJ 2553 The Bottom of a Graph 题目链接 题意:给定一个有向图,求出度为0的强连通分量 思路:缩点搞就可以 代码: #include <cstdio> #in ...
- poj 2553 The Bottom of a Graph【强连通分量求汇点个数】
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 9641 Accepted: ...
- POJ 2553 The Bottom of a Graph (Tarjan)
The Bottom of a Graph Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 11981 Accepted: ...
- POJ 2553 The Bottom of a Graph Tarjan找环缩点(题解解释输入)
Description We will use the following (standard) definitions from graph theory. Let V be a nonempty ...
- [poj 2553]The Bottom of a Graph[Tarjan强连通分量]
题意: 求出度为0的强连通分量. 思路: 缩点 具体有两种实现: 1.遍历所有边, 边的两端点不在同一强连通分量的话, 将出发点所在强连通分量出度+1. #include <cstdio> ...
- POJ 2553 The Bottom of a Graph(强连通分量的出度)
题意: 求出图中所有汇点 定义:点v是汇点须满足 --- 对图中任意点u,若v可以到达u则必有u到v的路径:若v不可以到达u,则u到v的路径可有可无. 模板:http://www.cnblogs.co ...
随机推荐
- Web开发之编码与解码、签名、加密与解密
在Web开发中,编码与解码.签名.加密与解密是非常常见的问题.本文不会介绍具体实例,而是介绍这些的原理.用途与区别.一.编码与解码 在Web开发中,需要通过URL的query参数来传递数 ...
- 求int最大值以及int二进制
求int最大值:(((unsigned int)(~0))>>1) 求int的2进制串 string str = ""; int iNum = 100; for(int ...
- ELK获取用户真实IP
原理:在filebeat这台服务器上的nginx中获取到客户端真实IP($clientRealIp), 然后在访问日志中添加"$clientRealIp"字段.1. 通过ma ...
- Count Primes -- LeetCodes (primality test)
Description: Count the number of prime numbers less than a non-negative number, n. 思路:这题第一种思路是写一个is_ ...
- luogu P1069 细胞分裂
题目描述 Hanks 博士是 BT (Bio-Tech,生物技术) 领域的知名专家.现在,他正在为一个细胞实 验做准备工作:培养细胞样本. Hanks 博士手里现在有 N 种细胞,编号从 1~N,一个 ...
- springboot + 拦截器 + 注解 实现自定义权限验证
springboot + 拦截器 + 注解 实现自定义权限验证最近用到一种前端模板技术:jtwig,在权限控制上没有用springSecurity.因此用拦截器和注解结合实现了权限控制. 1.1 定义 ...
- Ubuntu 16.04安装Shell管理工具PAC Manager
下载: (链接: https://pan.baidu.com/s/1nvqrVgH 密码: 45wz) 安装: sudo dpkg -i pac-4.5.5.7-all.deb
- EF for oracle中无法读取配置 显示无法open问题解决方式
1.更新以上设置为 非注销部分 弄了很久很久哈.
- 细说JavaScript对象(3):hasOwnProperty
判断一个属性是定义在对象本身而不是继承自原型链,我们需要使用从 Object.prototype 继承而来的 hasOwnProperty 方法. hasOwnProperty 方法是 JavaScr ...
- Intellij IDEA 14.x 菜单项中Compile、Make和Build的区别
Compile.Make和Build的区别 针对Java的开发工具,一般都有Compile.Make和Build三个菜单项,完成的功能的都差不多,但是又有区别. 编译,是将源代码转换为可执行代码的过程 ...