传送门(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. 将非递减有序排列(L L1)归并为一个新的线性表L2 线性表L2中的元素仍按值非递减

    #include "stdio.h"#include "stdlib.h"#include "function.h"void main(){ ...

  2. IntelliJ IDEA集成JProfiler,入门教程

    说明: JProfiler是用于分析J2EE软件性能瓶颈并能准确定位到Java类或者方法有效解决性能问题的主流工具,它通常需要与性能测试工具如:LoadRunner配合使用,因为往往只有当系统处于压力 ...

  3. ubuntu下操作端口的方法

    最简单的一个操作:sudo ufw status可检查防火墙的状态,我的返回的是:不活动 sudo ufw version防火墙版本: ufw 0.29-4ubuntu1 Copyright 2008 ...

  4. is assembler instruction and machine instuction atomic

    1 assembler instruction depends,有的汇编指令会被assemble成多条机器指令. 2 机器指令 depends,有的机器指令也不是atomic的. 所以,不要希望在单条 ...

  5. iOS9 3D Touch使用

    http://www.cnblogs.com/zhanglinfeng/p/5133939.html

  6. 【python】-- try except (异常捕获)、断言

    try except (异常捕获) 当程序出错了,但是我们又不想让用户看到这个错误,而且我在写程序的时候已经预料到了它可以出现这样的错误,出现这样的错误代表着什么,我们可以提前捕获这些个错误 1.异常 ...

  7. matlab 代码加速

    ~ 使用 parfor 代替 for 1. 安装 Parallel Computing Toolbox, 使用 parfor代替for, 使用的是cpu多核,速度能提高三倍左右. ~ 在for循环中的 ...

  8. C#练习委托、事件、事件处理

    控制台应用程序效果: 代码: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  9. jQuery:[1]实现图片上传并预览

    jQuery:[1]实现图片上传并预览 原理 预览思路 1.当上传对象的input被触发并选择本地图片之后获取要上传的图片对象的URL: 2.把对象URL赋值给实现写好的img标签的src属性 Fil ...

  10. PAT 天梯赛 L2-005. 集合相似度 【SET】

    题目链接 https://www.patest.cn/contests/gplt/L2-005 思路 因为集合中的元素 是不重复的 所以用SET 来保存 集合 然后最后 查找一下 两个集合中共有元素 ...