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)个 ...
随机推荐
- flex 弹性盒子模型一些案例.html
Flexbox是布局模块,而不是一个简单的属性,它包含父元素和子元素的属性. Flex元素是可以让你的布局根据浏览器的大小变化进行自动伸缩. 自适应导航 <ul class="navi ...
- mcrypt加密与解密
$key = 'test'; $result_array = array('name' => 'ta', 'age' => 28); $str = encode($result_array ...
- Python 基础篇:介绍
1. Python 发展 1989年,为了打发圣诞节假期,Guido开始写Python语言的编译器.Python这个名字,来自Guido所挚爱的电视剧Monty Python's Flying Cir ...
- php 如何开启session
1.如果你在session_start()前没有输出内容,哪怕是一个句号也不行,就可以直接使用session_start)_; 2.如果你之前已经有输出内容了,可以使用以下方法: <?php o ...
- Critical Rendering Path
1.生成 dom & cssom https://developers.google.com/web/fundamentals/performance/critical-rendering-p ...
- Qt智能指针简明说明
下面的智能指针分别对应boost库,Qt库,c++11的智能指针 boost::scoped_ptr QScopedPointer unique_ptr 在其生命期结束后会自动删除它所指的对象(确定 ...
- Linux下反斜杠号"\"引发的思考
今天在检查home目录时发现有一个名为“\”的文件,觉得很奇怪,从来没见过,就准备用Vim打开看看,很自然地输入命令查看一下,结果居然打不开. ubuntu@ubuntu:~$ vi \> ub ...
- JUnit 4 使用 Java 5 中的注解(annotation)
JUnit 4 使用 Java 5 中的注解(annotation),以下是JUnit 4 常用的几个 annotation 介绍@Before:初始化方法@After:释放资源@Test:测试方法, ...
- TDD 实践过程
聚合根是唯一能够允许充当进入聚合的逻辑途径的实体. 是唯一能够允许聚合外的对象持有的引用成员. 聚合根--决定数据库的入口,以为:
- 【leetcode】Intersection of Two Linked Lists(easy)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...