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(强连通分量)的更多相关文章

  1. poj 2186 Popular Cows (强连通分量+缩点)

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  2. POJ 2186 Popular Cows --强连通分量

    题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...

  3. POJ2186 Popular Cows 强连通分量tarjan

    做这题主要是为了学习一下tarjan的强连通分量,因为包括桥,双连通分量,强连通分量很多的求法其实都可以源于tarjan的这种方法,通过一个low,pre数组求出来. 题意:给你许多的A->B ...

  4. POJ 2186 Popular Cows 强连通分量模板

    题意 强连通分量,找独立的块 强连通分量裸题 #include <cstdio> #include <cstdlib> #include <cstring> #in ...

  5. POJ2186 Popular Cows [强连通分量|缩点]

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31241   Accepted: 12691 De ...

  6. POJ 2186 Popular Cows(强连通分量缩点)

    题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...

  7. poj2186Popular Cows(Kosaraju算法--有向图的强连通分量的分解)

    /* 题目大意:有N个cows, M个关系 a->b 表示 a认为b popular:如果还有b->c, 那么就会有a->c 问最终有多少个cows被其他所有cows认为是popul ...

  8. 图论:POJ2186-Popular Cows (求强连通分量)

    Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...

  9. POJ-2186-Popular Cows(强连通分量,缩点)

    链接:https://vjudge.net/problem/POJ-2186 题意: 有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个 ...

随机推荐

  1. AngularJS(10)-数据验证

    AngularJS 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告.客户端的验证不能确保用户输入数据的安全,所以服务端的数据验证也是必须的. <!DOCTYPE html> & ...

  2. linux之cat命令

    1. cat 接普通文件名,会把文件内容打印到屏幕:2. cat > file,这个可以向文件“file”写入内容,最后按 Ctrl + D 结束输入,会将你输入的数据保存到文件. cat主要有 ...

  3. CSS实现不固定宽度和高度的自动居中

    有时候我们需要实现下面这种效果: 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大高度不让你剧中 嘎嘎,撑大 ...

  4. 51nod1417 天堂里的游戏

    ---恢复内容开始--- 1417 天堂里的游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 多年后,每当Noder看到吉普赛人,就会想起那个遥 ...

  5. 1009. Product of Polynomials (25)

    #include <stdio.h> struct MyStruct { int exp; double coe; }; int main() { int k1,k2,i,j; MyStr ...

  6. 1021.Deepest Root (并查集+DFS树的深度)

    A graph which is connected and acyclic can be considered a tree. The height of the tree depends on t ...

  7. buffer busy wait在RAC环境下出现

    昨天运维组的同时反映有套系统用户反映很慢,需要协助帮忙检查什么原因引起的性能问题.导出了从8点到11点的AWR报告进行分析,发现等待事件里大部分的指标都正常,就是buffer busy wait的平均 ...

  8. speed up your sharepoint

    1. warm up http://blog.nowan.hu/post/SPWakeUp-Wake-up-your-SharePoint-quickly http://blogs.msdn.com/ ...

  9. K-meams文本聚类算法C++实现

    FROM:http://www.cnblogs.com/finallyliuyu/archive/2010/09/03/1817348.html 头文件: #ifndef _Preprocess_H ...

  10. python学习笔记1(语法)

    语法 从"Hello,world"开始看吧,我们学的很多语言都是从helloworld开始的. >>> 1 + 1 2 >>> print 'H ...