Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30680   Accepted: 12445

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive, if A thinks B is popular and B thinks C is popular, then A will also think that C is 
popular, even if this is not explicitly specified by an ordered pair in the input. Your task is to compute the number of cows that are considered popular by every other cow. 

Input

* Line 1: Two space-separated integers, N and M

* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

大意:n只牛,m对数(a,b)表示a认为b牛逼。若a认为b牛逼,b认为c牛逼,那么a也认为c牛逼。问n只牛中,有多少只牛是其他所有牛都认为牛逼的。

分析:若图为不连通,则答案为0.当图连通时,在同一个强连通分量A中的牛在被该强连通分量中其他牛认为牛逼;若在A和另一个强连通分量B之间有路,假设A指向B,则A中的牛都认为B中的牛是牛逼的。任意一个有向图都可以分解成若干不相交的强连通分量,把每个强连通分量缩点,就得到一个DAG(有向无环图)。该题要找的是缩点后DAG中出度为0的点所表示的强连通分量中的点数。(该出度为0的点一定唯一,否则不可能有其他牛都认为牛逼的牛)

求强连通分量可使用tarjan算法。
Tarjan算法是基于对图深度优先搜索的算法,每个强连通分量为搜索树中的一棵子树。搜索时,把当前搜索树中未处理的节点加入一个堆栈,回溯时可以判断栈顶到栈中的节点是否为一个强连通分量。

定义DFN(u)为节点u搜索的次序编号(时间戳),Low(u)为u或u的子树能够追溯到的最早的栈中节点的次序号。

当DFN(u)=Low(u)时,以u为根的搜索子树上所有节点是一个强连通分量
 
void tarjan(int s)
{
vis[s]=;
dfn[s]=low[s]=++now;
sta.push(s);
for(int i=; i<gra[s].size(); i++)
{
int t=gra[s][i];
if(dfn[t]==)
{
tarjan(t);
low[s]=min(low[s],low[t]);
}
else if(vis[t]==)
low[s]=min(low[s],dfn[t]);
}
if(low[s]==dfn[s])
{
sum++;
while(!sta.empty())
{
int t=sta.top();
sta.pop();
vis[t]=;
num[t]=sum;
if(t==s)
break;
}
}
}

 AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<stack>
using namespace std;
#define LL long long
#define N 10005
vector<int> gra[N];
stack<int>sta;
int n,m,dfn[N],low[N],vis[N],num[N];
int degree[N],sum,now,res,loc;
void init()
{
sum=;
now=;
res=;
memset(vis,,sizeof(vis));
memset(low,,sizeof(low));
memset(dfn,,sizeof(dfn));
memset(degree,,sizeof(degree));
for(int i=; i<=n; i++)
gra[i].clear();
while(!sta.empty())
sta.pop();
} void read()
{
int a,b;
scanf("%d%d",&n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d",&a,&b);
gra[a].push_back(b);
}
} void tarjan(int s)
{
vis[s]=;
dfn[s]=low[s]=++now;
sta.push(s);
for(int i=; i<gra[s].size(); i++)
{
int t=gra[s][i];
if(dfn[t]==)
{
tarjan(t);
low[s]=min(low[s],low[t]);
}
else if(vis[t]==)
low[s]=min(low[s],dfn[t]);
}
if(low[s]==dfn[s])
{
sum++;
while(!sta.empty())
{
int t=sta.top();
sta.pop();
vis[t]=;
num[t]=sum; //缩点
if(t==s)
break;
}
}
} void solve()
{
for(int i=; i<=n; i++)
if(!dfn[i])
tarjan(i);
for(int i=; i<=n; i++)
for(int j=; j<gra[i].size(); j++)
if(num[i]!=num[gra[i][j]])
degree[num[i]]++; //对缩点后的DAG进行出度的统计
for(int i=; i<=sum; i++)
{
if(degree[i]==)
{
res++;
loc=i; //第几个强连通分量
}
}
if(res>)
printf("0\n");
else
{
res=;
for(int i=; i<=n; i++)
if(num[i]==loc)
res++;
printf("%d\n",res);
}
} int main()
{ init();
read();
solve();
return ;
}
/*
3 3
1 2
2 1
2 3 */
 

POJ_2186_Popular Cows_强连通分量的更多相关文章

  1. HDU5934 强连通分量

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5934 根据距离关系建边 对于强连通分量来说,只需引爆话费最小的炸弹即可引爆整个强连通分量 将所有的强连通分 ...

  2. POJ1236Network of Schools[强连通分量|缩点]

    Network of Schools Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16571   Accepted: 65 ...

  3. 有向图的强连通分量的求解算法Tarjan

    Tarjan算法 Tarjan算法是基于dfs算法,每一个强连通分量为搜索树中的一颗子树.搜索时,把当前搜索树中的未处理的结点加入一个栈中,回溯时可以判断栈顶到栈中的结点是不是在同一个强连通分量中.当 ...

  4. Tarjan算法--强连通分量

    tarjan的过程就是dfs过程. 图一般能画成树,树的边有三种类型,树枝边 + 横叉边(两点没有父子关系) + 后向边(两点之间有父子关系): 可以看到只有后向边能构成环,即只有第三张图是强连通分量 ...

  5. 强连通分量的一二三 | | JZOJ【P1232】 | | 我也不知道我写的什么

    贴题: 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的教学地点.人间之 ...

  6. 有向图强连通分量的Tarjan算法

    有向图强连通分量的Tarjan算法 [有向图强连通分量] 在有向图G中,如果两个顶点间至少存在一条路径,称两个顶点强连通(strongly connected).如果有向图G的每两个顶点都强连通,称G ...

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

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

  8. Tarjan应用:求割点/桥/缩点/强连通分量/双连通分量/LCA(最近公共祖先)【转】【修改】

    一.基本概念: 1.割点:若删掉某点后,原连通图分裂为多个子图,则称该点为割点. 2.割点集合:在一个无向连通图中,如果有一个顶点集合,删除这个顶点集合,以及这个集合中所有顶点相关联的边以后,原图变成 ...

  9. POJ 1236 Network of Schools(强连通分量/Tarjan缩点)

    传送门 Description A number of schools are connected to a computer network. Agreements have been develo ...

随机推荐

  1. Postman做http接口功能测试

    首先,做接口测试前要有明确的接口文档(e.g. http://test.nnzhp.cn/wiki/index.php?doc-view-59) ,假设已经在PC上安装好了Postman. 1. 普通 ...

  2. [cf 599A]Patrick and Shopping

    傻逼题,但是我还是wa了一发. #include <iostream> using namespace std; int main() { long long a,b,c,Ans=0x7f ...

  3. Segmentation fault到底是何方妖孽

    http://blog.chinaunix.net/uid-23069658-id-3959636.html?page=2 进程运行的时候,它虚拟地址空间的布局和它所占用的物理内存到底是什么样子呢?虚 ...

  4. ggplot画基本图形类型

    df<-data.frame( x=c(3,1,5), y=c(2,4,6), label=c("a","b","c"))p<- ...

  5. 【ACM】hdu_1089_A+BI_201307261121

    A+B for Input-Output Practice (I)Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  6. mysql 源码 王恒 [mysql数据结构+mysql 执行计划]

    http://blog.chinaunix.net/uid/26896862/cid-156733-list-1.html http://www.it168.com/redian/wangheng/

  7. Gevent的协程实现原理

    之前之所以看greenlet的代码实现,主要就是想要看看gevent库的实现代码. .. 然后知道了gevent的协程是基于greenlet来实现的...所以就又先去看了看greenlet的实现... ...

  8. spring:使用<prop>标签为Java持久属性集注入值

    spring:使用<prop>标签为Java持久属性集注入值 使用 spring 提供的<prop>为Java持久属性集注入值,也就是向 java.util.Propertie ...

  9. B1816 扑克牌 二分答案 + 贪心

    这个题我一开始想到了二分答案,但是去写了另一个算法,用优先队列直接模拟,最后GG了...因为我没考虑每个套牌只能有一个joker...尴尬. 后来二分答案,然后暴力验证就行了. 题干: Descrip ...

  10. Fisher 线性判别

    Multiplying both sides of this result by wT and adding w0, and making use of y(x)=wTx+w0 and  y(xΓ)= ...