poj2186 强连通
题意:有 n 头牛,以及一些喜欢关系,牛 A 喜欢牛 B,这种关系可以传递,问有多少头牛被牧场上所有牛喜欢。
首先强连通,因为在同一个强连通分量中牛是等价的,然后对于一个有向无环图看是否只有一个强连通分量出度为 0 ,如果是,则这个强连通分量中的点都是答案,否则为 0。
#include<stdio.h>
#include<string.h>
#include<stack>
#include<queue>
using namespace std; const int maxn=;
const int maxm=; int head[maxn],point[maxm],nxt[maxm],size;
int n,t,scccnt;
int stx[maxn],low[maxn],scc[maxn],od[maxn];
stack<int>S; void init(){
memset(head,-,sizeof(head));
size=;
memset(od,,sizeof(od));
} void add(int a,int b){
point[size]=b;
nxt[size]=head[a];
head[a]=size++;
} void dfs(int s){
stx[s]=low[s]=++t;
S.push(s);
for(int i=head[s];~i;i=nxt[i]){
int j=point[i];
if(!stx[j]){
dfs(j);
low[s]=min(low[s],low[j]);
}
else if(!scc[j]){
low[s]=min(low[s],stx[j]);
}
}
if(low[s]==stx[s]){
scccnt++;
while(){
int u=S.top();S.pop();
scc[u]=scccnt;
if(s==u)break;
}
}
} void setscc(){
memset(stx,,sizeof(stx));
memset(scc,,sizeof(scc));
t=scccnt=;
for(int i=;i<=n;++i)if(!stx[i])dfs(i);
for(int i=;i<=n;++i){
for(int j=head[i];~j;j=nxt[j]){
int k=point[j];
if(scc[i]!=scc[k]){
od[scc[i]]++;
}
}
}
} int main(){
int m;
while(scanf("%d%d",&n,&m)!=EOF){
init();
while(m--){
int a,b;
scanf("%d%d",&a,&b);
add(a,b);
}
setscc();
int ans=;
if(scccnt==)printf("%d\n",n);
else{
for(int i=;i<=scccnt;++i)if(!od[i])ans++;
if(ans!=){
printf("0\n");
}
else{
ans=;
for(int i=;i<=n;++i){
if(!od[scc[i]])ans++;
}
printf("%d\n",ans);
}
}
}
return ;
}
poj2186 强连通的更多相关文章
- poj2186 强连通缩点
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29773 Accepted: 12080 De ...
- POJ2186 强连通分量+缩点
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40234 Accepted: 16388 De ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
- 强连通分量+poj2186
强连通分量:两个点能够互相连通. 算法分解:第一步.正向dfs全部顶点,并后序遍历 第二步,将边反向,从最大边dfs,构成强连通分量 标号最大的节点属于DAG头部,cmp存一个强连通分量的拓扑序. p ...
- POJ2186 Popular Cows(强连通分量)
题目问一个有向图所有点都能达到的点有几个. 先把图的强连通分量缩点,形成一个DAG,那么DAG“尾巴”(出度0的点)所表示的强连通分量就是解,因为前面的部分都能到达尾巴,但如果有多个尾巴那解就是0了, ...
- POJ2186 Popular Cows 强连通分量tarjan
做这题主要是为了学习一下tarjan的强连通分量,因为包括桥,双连通分量,强连通分量很多的求法其实都可以源于tarjan的这种方法,通过一个low,pre数组求出来. 题意:给你许多的A->B ...
- poj2186 Popular Cows(强连通)
崇拜有传递性.求所有牛都崇拜的牛tarjan算法求强连通. 如果不连通就不存在.如果联通,缩点后唯一一个出度为零的点就是答案,有多个则不存在. #include <vector> #inc ...
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23445 Accepted: 9605 Des ...
- poj2186 Popular Cows --- 强连通
给一个有向图,问有多少结点是其它全部结点都能够到达的. 等价于,在一个有向无环图上,找出度为0 的结点.假设出度为0的结点仅仅有一个,那么这个就是答案.假设大于1个.则答案是0. 这题有环.所以先缩点 ...
随机推荐
- hdu 1033 (bit masking, utilization of switch, '\0' as end of c string) 分类: hdoj 2015-06-15 21:47 37人阅读 评论(0) 收藏
bit masking is very common on the lower level code. #include <cstdio> #include <algorithm&g ...
- DotNetBar v12.2.0.7 Fully Cracked
PS: 博客园的程序出现问题,导致我的博客不能访问(转到登录页),而我自己由于 Cookies 问题,一直可以访问,所以一直未发现该问题. 感谢冰河之刃告知,thx! 更新信息: http://www ...
- HTML特殊符号对应代码
HTML中的特殊符号所对应代码,收藏使用 符号 HTML 符号 HTML & & < < > > ⁄ ⁄ " " ¸ ¸ ° ° ½ ...
- gsoap框架下的onvif程序流程分析
SOAP_FMAC5 int SOAP_FMAC6 soap_serve(struct soap *soap) { do { unsigned int k = soap->max_keep_al ...
- Browser GetImage
using Microsoft.Win32; using System; using System.ComponentModel; using System.Drawing; using System ...
- C#特性
一.概念:C#的特性也是一个类,继承自System.Attribute,用于描述类.方法.字段等 二.类型: 2.1 .net中特性用来处理多种问题,比如序列化.程序的安全特性.防止即时编译器对 ...
- Shell获取当前用户
id | sed -e 's/).*//g' -e 's/.*(//' 比$LOGNAME $NAME who am i都要准确一些
- Tomcat容器运行struts2+spring+mybatis架构的java web应用程序简单分析
1.具体的环境为 MyEclipse 8.5以及自带的tomcat spring3.0.5 struts2.3.15.1 mybatis3.0.5 2.想弄明白的一些问题 tomcat集成spring ...
- IAP 程序内购
最近用到IAP内置购买,阅读官方文档,在网上找了些资料,在这里作下整理,以便日后查找和修改,主要流程方向确定,文档和相关转载内容截图不一一指出,google一堆. 1.查找官方文档,两张目录截图,对主 ...
- Cisco ASA 5505 Routing Between Two (Internal) VLANS
Referenced:http://www.petenetlive.com/KB/Article/0000869 Problem I had to set this up for a client t ...