POJ2186 Popular Cows(强连通分量)
题目问一个有向图所有点都能达到的点有几个。
先把图的强连通分量缩点,形成一个DAG,那么DAG“尾巴”(出度0的点)所表示的强连通分量就是解,因为前面的部分都能到达尾巴,但如果有多个尾巴那解就是0了,因为尾巴间达到不了。判断是否有多个尾巴,可以从最后一个强连通分量中的某一个点出发看能否在逆图上遍历完其他点。
因为用到了逆图,所以直接用Kosaraju算法。
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
#define MAXM 111111
#define MAXN 11111
struct Edge{
int v,next;
}edge[MAXM];
int NE,head[MAXN],rhead[MAXN];
void addEdge(int u,int v){
edge[NE].v=v; edge[NE].next=head[u]; head[u]=NE++;
edge[NE].v=u; edge[NE].next=rhead[v]; rhead[v]=NE++;
} int belong[MAXN];
bool vis[MAXN];
vector<int> post;
void dfs(int u){
vis[u]=;
for(int i=head[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(vis[v]) continue;
dfs(v);
}
post.push_back(u);
}
void rdfs(int u,int k){
vis[u]=; belong[u]=k;
for(int i=rhead[u]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(vis[v]) continue;
rdfs(v,k);
}
}
int rdfs(int u){
int res=;
vis[u]=;
for(int i=rhead[i]; i!=-; i=edge[i].next){
int v=edge[i].v;
if(vis[v]) continue;
res+=rdfs(v);
}
return res;
}
int scc(int n){
memset(vis,,sizeof(vis));
post.clear();
for(int i=; i<=n; ++i){
if(!vis[i]) dfs(i);
}
memset(vis,,sizeof(vis));
int k=;
for(int i=post.size()-; i>=; --i){
int v=post[i];
if(!vis[v]) rdfs(v,++k);
}
memset(vis,,sizeof(vis));
for(int i=; i<=n; ++i){
if(belong[i]==k){
if(rdfs(i)!=n) return ;
break;
}
}
int res=;
for(int i=; i<=n; ++i){
if(belong[i]==k) ++res;
}
return res;
}
int main(){
memset(head,-,sizeof(head));
memset(rhead,-,sizeof(rhead));
int n,m,a,b;
scanf("%d%d",&n,&m);
while(m--){
scanf("%d%d",&a,&b);
addEdge(a,b);
}
printf("%d",scc(n));
return ;
}
POJ2186 Popular Cows(强连通分量)的更多相关文章
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- POJ2186 Popular Cows 强连通分量tarjan
做这题主要是为了学习一下tarjan的强连通分量,因为包括桥,双连通分量,强连通分量很多的求法其实都可以源于tarjan的这种方法,通过一个low,pre数组求出来. 题意:给你许多的A->B ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 2186 Popular Cows --强连通分量
题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...
- poj2186 Popular Cows --- 强连通
给一个有向图,问有多少结点是其它全部结点都能够到达的. 等价于,在一个有向无环图上,找出度为0 的结点.假设出度为0的结点仅仅有一个,那么这个就是答案.假设大于1个.则答案是0. 这题有环.所以先缩点 ...
- POJ 2186 Popular Cows 强连通分量模板
题意 强连通分量,找独立的块 强连通分量裸题 #include <cstdio> #include <cstdlib> #include <cstring> #in ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
- 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows
P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23445 Accepted: 9605 Des ...
随机推荐
- HDFS深入浅析
导读 Hadoop分布式文件系统(HDFS)被设计成适合运行在通用硬件(commodity hardware)上的分布式文件系统.它和现有的分布式文件系统有很多共同点.但同时,它和其他的分布式文件系统 ...
- [Unity3D]关于Assets资源目录结构管理
原地址:http://www.cnblogs.com/hisiqi/p/3203515.html 分享个我们项目常用的目录结构,微调过很多次,最终到了这个版本.个人认为这种管理资源方式是不错的.欢迎探 ...
- 关于ruby重构的过程中去除不必要的format
(文章是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) #这段话可以由下面的话替代56 respond_to do |format|57 ...
- git初学者这样就行了。
Create a new repository on the command line touch README.md git init git add README.md git commit -m ...
- HDOJ 1856
#include<cstdio> #include<cstdlib> typedef struct ufse *ufset; struct ufse { ]; ]; }UFS; ...
- nginx lua处理图片
user apache apache; worker_processes 4; worker_rlimit_nofile 100000; #error_log logs/error.log; #err ...
- CSS3.0盒模型display:-webkit-box;的使用
box-flex是css3新添加的盒子模型属性,它的出现可以解决我们通过N多结构.css实现的布局方式.经典 的一个布局应用就是布局的垂直等高.水平均分.按比例划分. 目前box-flex属性还没 ...
- VIM替换、截取及追加操作
参考: http://blog.csdn.net/love__coder/article/details/6739670 http://blog.csdn.net/love__coder/articl ...
- 【转】Linux Shell脚本调试技术
本文转载自:https://www.ibm.com/developerworks/cn/linux/l-cn-shell-debug/ Shell脚本调试技术 本文全面系统地介绍了shell脚本调试技 ...
- 电话激活windows server 2012的解决方案
在激活Windows系统时,微软一直秉承着坑爹的传统,竟然把电话激活的界面给隐藏起来了,只留一个在线激活的界面,但是如果是给服务器激活系统,基本是不会有外网可以用的,不过我们可以通过命令行的方式进行激 ...