传送门(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. 在安装mysql数据库的过程中,显示msvcp100.dll丢失?则么办?

    方案一:重装操作系统为windows10专业版 方案二:问题: 解答: 报错原因是VC运行库不全或者没有安装导致,百度搜索VC集合下载安装, 链接:https://pan.baidu.com/s/1U ...

  2. 电路分析二-------基尔霍夫定律KCL和KVL

    1.先了解几个名词 (1)支路----一个二端原件视为一条支路--图中6个二端原件所以有6条支路. (2)结点----两条或以上的支路连接的点. d,e可以看做一个结点. (3).回路----- (4 ...

  3. cocos2d-x3.6 生成带类图的离线文档

    我的博客:http://blog.csdn.net/dawn_moon cocos2d-x的官网有点慢,并且最新3.6的在线API文档居然没有了类图,不知道什么原因,之前2.2.6都是有的. 只是能够 ...

  4. ExtJS4.2.1与Spring MVC实现Session超时控制

    假设你的项目使用ExtJS作为表现层.你会发现,SESSION超时控制将是一个问题. 本文将就自己的经验.来解决这一问题.当然,解决这个问题并不是仅仅有一种方法,我仅仅是提出我的方法. 首先.做超时控 ...

  5. docker centos yum 源

    aliyun yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.rep ...

  6. python-安装 pip

    https://pip.pypa.io/en/stable/installing/ wget https://bootstrap.pypa.io/get-pip.py python get-pip.p ...

  7. JVM 性能优化, Part 4: C4 垃圾回收

    ImportNew注:本文是JVM性能优化 系列-第4篇.前3篇文章请参考文章结尾处的JVM优化系列文章.作为Eva Andreasson的JVM性能优化系列的第4篇,本文将对C4垃圾回收器进行介绍. ...

  8. linux基础part2

    linux基础 一.linux基础命令 1.pwd:用来显示当前目录位置 2.cd:用来切换目录位置.(eg:cd...cd../...cd-.cd~) 3.ls:用来查看目录或文件信息(eg:ls ...

  9. 3.16课·········C#小结

    //附加//C#源码,被C#编译器,编译成执念代码(IL)//int16=short.....±32000//int32=int.......±21亿//int64=long......±922亿亿3 ...

  10. Python——轻量级web服务器flask的学习

    前言: 根据工程需要,开始上手另一个python服务器---flask,flask是一个轻量级的python服务器,简单易用.将我的学习过程记录下来,有新的知识会及时补充. 记录只为更好的分享~ 正文 ...