传送门(poj):http://poj.org/problem?id=2186

(bzoj):http://www.lydsy.com/JudgeOnline/problem.php?id=1051

Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 33482   Accepted: 13638

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

Hint

Cow 3 is the only cow of high popularity. 

Source

USACO 2003 Fall
【解析】
呵呵哒。在poj上测怎么都是WA,半天错误都没找出来。数组我又开大点也没用。网友提供的十多组数据我都A啊。然后我就去bzoj上测的A了,100ms.
所以下面的代码poj上是过不了的。哼poj浪费我时间。上面两个传送门。
 
tarjan求强连通分量+缩点。
求出缩点后出度为0的点的个数,如果个数是1,输出这个连通块的牛的个数。否则不存在被所有牛都喜欢的牛。
 
我欢迎你,你欢迎他,我就欢迎他。
如果有S头牛互相喜欢,如果有一头牛喜欢S头牛中的任意一头,那么那头牛就喜欢这S头牛。
将互相喜欢的牛缩成一个点,如果某一个点的出度为0,那么所有的牛都喜欢它。注意是结果是连通块里牛的个数。
【代码】
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define N 10009
struct Edge
{
int x,y,next;
Edge(int x=,int y=,int next=):
x(x),y(y),next(next){}
}edge[N*];
int sumedge,n,m,x,y,tim,top,sumclr,sumedge2,js,ans;
int head[N],dfn[N],low[N],Stack[N],color[N],cnt[N],out[N],head2[N];
bool vis[N],instack[N];
void ins(int x,int y)
{
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
}
void ins2(int x,int y)
{
edge[++sumedge2]=Edge(x,y,head2[x]);
head2[x]=sumedge2;
}
map<int,bool>Map[N];
void tarjan(int x)
{
dfn[x]=low[x]=++tim;
vis[x]=;instack[x]=;Stack[++top]=x;
for(int u=head[x];u;u=edge[u].next)
if(instack[edge[u].y])
low[x]=min(low[x],dfn[edge[u].y]);
else
if(!vis[edge[u].y])
{
tarjan(edge[u].y);
low[x]=min(low[x],low[edge[u].y]);
}
else
{
}
if(dfn[x]==low[x])
{
sumclr++;
color[x]=sumclr;
cnt[sumclr]++;
while(Stack[top]!=x)
{
color[Stack[top]]=sumclr;//染色
instack[Stack[top]]=;
top--;
cnt[sumclr]++;//记录这个连通块里牛的个数
}
instack[x]=;
top--;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
ins(x,y);
}
for(int i=;i<=n;i++)
{
if(!vis[i])tarjan(i);
top=;
}
for(int i=;i<=n;i++)
for(int u=head[i];u;u=edge[u].next)
if(color[i]!=color[edge[u].y])
if(Map[color[i]].find(color[edge[u].y])==Map[color[i]].end()) //缩点
{
Map[color[i]][color[edge[u].y]]=true;
ins2(color[i],color[edge[u].y]);
out[color[i]]++;
}
int cnnt=;
for(int i=;i<=sumclr;i++)
{
if(out[i]==)//度为0意味着所有牛都欢迎他
{
js++;
ans=i;
cnnt+=cnt[i];//加上这个连通块里牛的个数
}
}
if(js>||js==)puts("");//如果有大于1个的度为0的点,说明这个图不是连通的。
else
printf("%d",cnnt);
return ;
}

Popular Cows的更多相关文章

  1. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

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

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

  3. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

  4. [强连通分量] POJ 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31815   Accepted: 12927 De ...

  5. POJ 2186 Popular Cows(强连通)

                                                                  Popular Cows Time Limit: 2000MS   Memo ...

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

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

  7. poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27698   Accepted: 11148 De ...

  8. POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Des ...

  9. POJ 2186 Popular Cows (强联通)

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

  10. 强连通分量tarjan缩点——POJ2186 Popular Cows

    这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...

随机推荐

  1. Manager模块 队列 管道 进程池

    Manager模块 作用:  多进程共享变量. Manager的字典类型: 如果value是简单类型,比如int,可以直接赋值给共享变量,并可以后续直接修改 如果value是复杂类型 ,比如list, ...

  2. Earth Hour(最短路)

    Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total ...

  3. K-Piggy-Bank

    Piggy-Bank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  4. 6.2.3-Bean的加载处理

    在AbstractBeanFactory中doGetBean方法中始终调用了getObjectForBeanInstance方法,这个方法是对参数进行过滤; @SuppressWarnings(&qu ...

  5. 浅谈命令查询职责分离(CQRS)模式---转载

    在常用的三层架构中,通常都是通过数据访问层来修改或者查询数据,一般修改和查询使用的是相同的实体.在一些业务逻辑简单的系统中可能没有什么问题,但是随着系统逻辑变得复杂,用户增多,这种设计就会出现一些性能 ...

  6. unity里standard pbr(一)

    关注forwardbase下的 standard.shader #pragma vertex vertBase #pragma fragment fragBase #include "Uni ...

  7. k近邻算法(k-nearest neighbor,k-NN)

    kNN是一种基本分类与回归方法.k-NN的输入为实例的特征向量,对应于特征空间中的点:输出为实例的类别,可以取多类.k近邻实际上利用训练数据集对特征向量空间进行划分,并作为其分类的"模型&q ...

  8. linux 中 用户管理 (composer 时不能root 遇到)

    linux 是支持多用户的,可以同时多个用户在线操作,这点与 Windows 不同. 在我们项目组 操作linux 服务器时,可进行多用户管理,并赋予不同权限,下面是我学习并用的比较频繁的命令: 1. ...

  9. 很详细、很移动的Linux makefile 教程

    近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出 ...

  10. mysql 数据库备份方案及策略

    由于mysql存在多种数据库备份方式,而且各有利弊,对于我们初学者来说,选择合适的备份方式确实有些困难.个人觉得,首先要基于公司的需求,考虑能够容忍丢失多少数据.花多少人力时间成本等,这是我们制定备份 ...