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. LESS学习总结

    之前在工作过程中,用到了Less,一直没有将学习心得整理归纳,今天终于空出时间整理了一下.   Less学习常用参考文档: Less 中文网 http://lesscss.cn/ 快速入门 | Les ...

  2. Java.lang.RuntimeException: Unable to instantiate activity ComponentInfo

    如果你更新了ADT的新版本,而工程文件中使用了其他的jar包,也可能会出现"java.lang.RuntimeException: Unable to instantiate activit ...

  3. ACE_linux:TCP通信

    1.涉及类 ACE_INET_Addr//ACE网络地址ACE_SOCK_Acceptor//ACE网络服务器ACE_SOCK_Connector//ACE网络客户端ACE_SOCK_Stream// ...

  4. Java学习小结(1)-数组的创建与传参

    (一)数组的创建 数组的创建包括两部分:数组的申明与分配内存空间. int score[]=null; //申明一维数组 score=new int[3]; //分配长度为3的空间 数组的申明还有另外 ...

  5. Spark Streaming揭秘 Day26 JobGenerator源码图解

    Spark Streaming揭秘 Day26 JobGenerator源码图解 今天主要解析一下JobGenerator,它相当于一个转换器,和机器学习的pipeline比较类似,因为最终运行在Sp ...

  6. 1011. World Cup Betting (20)(最大值)

    With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...

  7. TCO 2014 Round 1C 概率DP

    TCO round 1C的 250 和500 的题目都太脑残了,不说了. TCO round 1C 950 一个棋子,每次等概率的向左向右移动,然后走n步之后,期望cover的区域大小?求cover, ...

  8. H2嵌入式数据库的各种连接方式

    H2database是一款用java语言编写的开源数据库, 一般用作游戏的数据存储, 当然web项目也是可以用的, web项目也可以将该数据库 首先要安装H2数据库 http://www.h2data ...

  9. ios登陆界面

    代码较老,仅供参考 主要涉及的功能点有: 1.密码输入框要隐藏输入字符,以黑点代替,有时候会在边上设置一个按钮,让用户选择是否需要密文输入 2.Login时会检查输入框,若输入不合法,弹窗提示用户 3 ...

  10. hdu 3631

    最短路  执行一遍 Floyd算法    比赛的时候没有想到, 用了优化的Dijkstra  超时到死 #include <cstdio> #include <cstring> ...