poj2186Popular Cows(强连通分量)
http://poj.org/problem?id=2186
用tarjan算出强连通分量的个数 将其缩点 连成一棵树 则题目所求即变成求出度为0 的那个节点 在树中是唯一的 即树根
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<stack>
using namespace std;
#define M 50010
#define N 10010
struct node
{
int u,v,next;
}edge[M*];
int head[N],lowlink[N],pre[N],t,sccno[N],scc,dep,dout[N];
stack<int>s;
void init()
{
t = ;
memset(head,-,sizeof(head));
memset(lowlink,,sizeof(lowlink));
memset(pre,,sizeof(pre));
memset(sccno,,sizeof(sccno));
dep=;scc=;
}
void add(int u,int v)
{
edge[t].u = v;
edge[t].v = v;
edge[t].next = head[u];
head[u] = t++;
}
void dfs(int u)
{
lowlink[u] = pre[u] = ++dep;
s.push(u);
for(int i = head[u] ; i != - ; i = edge[i].next)
{
int v = edge[i].v;
if(!pre[v])
{
dfs(v);
lowlink[u] = min(lowlink[u],lowlink[v]);
}
else if(!sccno[v])
lowlink[u] = min(lowlink[u],pre[v]);
}
if(lowlink[u]==pre[u])
{
scc++;
for(;;)
{
int x = s.top();s.pop();
sccno[x] = scc;
if(x==u) break;
}
}
}
void find_scc(int n)
{
for(int i = ; i <= n ; i++)
if(!pre[i]) dfs(i);
}
int main()
{
int i,j,n,m,a,b;
while(cin>>n>>m)
{
init();
while(m--)
{
cin>>a>>b;
add(a,b);
}
find_scc(n);
for(i = ; i <= scc ; i++)
dout[i] = ;
for(i = ; i <= n ; i++)
for(j = head[i] ; j!=- ; j = edge[j].next)
{
int v = edge[j].v;
if(sccno[i]!=sccno[v])
dout[sccno[i]] = ;
}
int num=,o,w=;
for(i = ; i <= scc ; i++)
if(dout[i]==)
{
o = i;
num++;
}
if(num==)
cout<<n<<endl;
else if(num==)
{
for(i = ; i <= n ; i++)
if(sccno[i]==o)
{
w++;
}
cout<<w<<endl;
}
else
cout<<"0\n";
}
return ;
}
poj2186Popular Cows(强连通分量)的更多相关文章
- 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 强连通分量tarjan
做这题主要是为了学习一下tarjan的强连通分量,因为包括桥,双连通分量,强连通分量很多的求法其实都可以源于tarjan的这种方法,通过一个low,pre数组求出来. 题意:给你许多的A->B ...
- POJ 2186 Popular Cows 强连通分量模板
题意 强连通分量,找独立的块 强连通分量裸题 #include <cstdio> #include <cstdlib> #include <cstring> #in ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
- poj2186Popular Cows(Kosaraju算法--有向图的强连通分量的分解)
/* 题目大意:有N个cows, M个关系 a->b 表示 a认为b popular:如果还有b->c, 那么就会有a->c 问最终有多少个cows被其他所有cows认为是popul ...
- 图论:POJ2186-Popular Cows (求强连通分量)
Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...
- POJ-2186-Popular Cows(强连通分量,缩点)
链接:https://vjudge.net/problem/POJ-2186 题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个 ...
随机推荐
- discuz 注册用户用到的几个表
通过开启记录:mysql记录日志,筛出如下信息: INSERT INTO `userclub`.pre_ucenter_members SET secques='', username='pthlp1 ...
- DISCUZ! X2.5设置仅允许QQ登录注册论坛 加固会员注册机制
论坛稍微有点起色之后,很多站长就会担心论坛经常被人恶意灌水.注册机.顶贴机等等一些列非法的手段.通常站长都会通过一些后台的设置和插件等等一 切有效的方法预防,但更多的站长会通过限制用户注册会员,需注册 ...
- MySql 服务端与客户端下载地址
mysql官网的注册,要上传户口,才能下载. 在网上搜了个下载地址. mysql-5.6.8-rc http://mysql.stu.edu.tw/Downloads/MySQL-5.6/mysql- ...
- 种子填充找连通块 floodfill
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- c#3位一分(money)
NumberFormatInfo num = new NumberFormatInfo(); num.NumberDecimalDigits = 2; string ...
- 微软Hololens学院教程-Hologram 230-空间场景建模(Spatial mapping )【微软教程已经更新,本文是老版本】
这是老版本的教程,为了不耽误大家的时间,请直接看原文,本文仅供参考哦!原文链接:https://developer.microsoft.com/EN-US/WINDOWS/HOLOGRAPHIC/ho ...
- poj 1236 Network of Schools(又是强连通分量+缩点)
http://poj.org/problem?id=1236 Network of Schools Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- C#设计模式学习资料--观察者模式
http://www.cnblogs.com/promise-7/archive/2012/05/14/2500759.html http://www.cnblogs.com/zhenyulu/art ...
- c++ 链接
header.h #ifndef HEADER_H #define HEADER_H unsigned long getFac(unsigned short num); ; #endif // HEA ...
- Mybatis 示例之 SelectKey(转)
参考:http://blog.csdn.net/isea533/article/details/21153791 SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问 ...