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

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

原题大意:给n头牛,m对形式(A,B)代表A觉得B很厉害,特殊的,如果A觉得B很厉害,B觉得C很厉害,那么A觉得C也很厉害,问有多少头牛被所有牛(除了它自己)觉得很厉害。
 
本题比较考验基础,涉及到链表,强连通分量的有关知识,对初学图论者还是有好处的。
 
解题思路:设n头牛中两两之间都觉得对方很厉害为一堆牛,而其中有一头牛被所有牛都认为很厉害,那么很显然,这堆牛都被所有牛认为很厉害。
              那会不会有两堆以上的牛都被所有人认为很厉害呢?显然不会,如果两堆牛都被所有人认为很厉害,那么这两堆牛两两之间一定都觉得自己很厉害,其实还是一堆牛。
              那么,这就可以演化为一个图,有n个点,m条有向边,求唯一的一个出度为0的强联通分量中元素的个数。
              为什么唯一呢?
              显然,如果有两个出度为0的强连通分量,那么这两个强连通分量都不会觉得对方分量的牛很厉害,而原题是找被所有牛都觉得很厉害,于是此时没有牛被所有牛认为很厉害。
             我们用链表存储点的关系,用tarjian找强连通分量,最后用强连通分量的出度判断是否是我们找的那一群牛,统计牛的个数就可以了。
上代码:

#include<stdio.h>
#include<string.h>
struct list
{
int v;
list *next;
};
list *head[10010],*rear[10010];
int dfn[10010],low[10010],stack[10010],s[10010],top,times,cnt;
bool instack[10010],chu[10010];
void tarjian(int v)
{
dfn[v]=low[v]=++times;
instack[v]=true;
stack[top++]=v;
for(list *p=head[v];p!=NULL;p=p->next)
if(!dfn[p->v])
{
tarjian(p->v);
if(low[p->v]<low[v]) low[v]=low[p->v];
}else if(instack[p->v]&&low[p->v]<low[v]) low[v]=low[p->v];
if(low[v]==dfn[v])
{
++cnt;
do
{
v=stack[--top];
instack[v]=false;
s[v]=cnt;
}while(low[v]!=dfn[v]);
}
return;
}
int main()
{
int n,m,i,begin,to,a,b;
scanf("%d%d",&n,&m);
memset(rear,0,sizeof(rear));
memset(head,0,sizeof(head));
for(i=0;i<m;++i)
{
scanf("%d%d",&begin,&to);
if(rear[begin]!=NULL)
{
rear[begin]->next=new list;
rear[begin]=rear[begin]->next;
}else head[begin]=rear[begin]=new list;
rear[begin]->next=NULL;
rear[begin]->v=to;
}
memset(dfn,0,sizeof(dfn));
memset(low,0,sizeof(low));
memset(stack,0,sizeof(stack));
memset(s,0,sizeof(s));
times=cnt=top=0;
for(i=1;i<=n;++i) if(!dfn[i]) tarjian(i);
if(cnt==1)
{
printf("%d\n",n);
return 0;
}
memset(chu,false,sizeof(chu));
for(i=1;i<=n;++i)
for(list *p=head[i];p!=NULL;p=p->next)
if(s[i]!=s[p->v])
chu[s[i]]=true;
a=0;
for(i=1;i<=cnt;++i) if(chu[i]) ++a;else b=i;
if(a==cnt-1)
{
a=0;
for(i=1;i<=n;++i) if(s[i]==b) ++a;
printf("%d\n",a);
}else printf("0\n");
return 0;
}

  

   

[强连通分量] POJ 2186 Popular Cows的更多相关文章

  1. 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

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

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

  3. poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  4. POJ 2186 Popular Cows (强联通)

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

  5. tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  6. POJ 2186 Popular Cows(强连通分量Kosaraju)

    http://poj.org/problem?id=2186 题意: 一个有向图,求出点的个数(任意点可达). 思路: Kosaraju算法的第一次dfs是后序遍历,而第二次遍历时遍历它的反向图,从标 ...

  7. POJ 2186 Popular Cows(强连通分量)

    [题目链接] http://poj.org/problem?id=2186 [题目大意] 给出一张有向图,问能被所有点到达的点的数量 [题解] 我们发现能成为答案的,只有拓扑序最后的SCC中的所有点, ...

  8. POJ 2186 Popular Cows --强连通分量

    题意:给定一个有向图,问有多少个点由任意顶点出发都能达到. 分析:首先,在一个有向无环图中,能被所有点达到点,出度一定是0. 先求出所有的强连通分支,然后把每个强连通分支收缩成一个点,重新建图,这样, ...

  9. poj 2186 "Popular Cows"(强连通分量入门题)

    传送门 参考资料: [1]:挑战程序设计竞赛 题意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系具有传递性,所以如果牛A认为牛 ...

随机推荐

  1. 什么是Servlet?它有哪些特点

    什么是Servlet?  它有哪些特点? Servlet是运行在JSP服务器端,用来生成Web页面的一种java程序 特点: (1)效率点 (2)功能强大 (3) Servlet之间能够共享数据 (4 ...

  2. 关于silverlight打印模糊的问题

         今天做silverlight打印实现时,发现一个问题,就是sl打印处理的文字很模糊          这样肯定不行撒,于是开始找解决办法,首先想到的是silverlight中文显示的问题,好 ...

  3. python urllib2 支持 自定义cookie

    先是在GOOGLE 上找了下, 发现就是只有2种方法,试了下,果然不行. 1, MozillaCookieJar 自定义保存到文件中 加载的时候不行,保存没问题. 2,opener.addheader ...

  4. 移动端网站的内容触摸滑动-Swiper插件

    手机平板等大多移动端站点都会有触摸滑动内容的功能,公司移动端站点(m.muzhiwan.com)的标题广告滑动以及轮播效果就是用的Swiper插件. Swiper就是常用于移动端网站的内容触摸滑动的一 ...

  5. php计算两个日期相差 年 月 日

    在PHP程序中,很多时候都会遇到处理时间的问题,比如:判断用户在线了多长时间,共登录了多少天,两个帖子发布的时间差或者是不同操作之间的日志记录等等.在文章中,简单地举例介绍了PHP中如何计算两个日期相 ...

  6. Tomcat connector元素常用配置(最大连接数等)

    在tomcat的server.xml中有类似: <Connector port=" minSpareTHreads=" URIEncoding="gbk" ...

  7. LeetCode----66. Plus One(Java)

    package plusOne66; /* Given a non-negative number represented as an array of digits, plus one to the ...

  8. 【转】Server Tomcat v7.0 Server at localhost was unable to start within 45 seconds. If

    转载地址:http://fanshuyao.iteye.com/blog/1695482 在eclipse启动tomcat时遇到超时45秒的问题: Server Tomcat v7.0 Server ...

  9. Visual Studio 2015简体中文企业版/专业版下载+有效激活密钥

    Visual Studio 2015是一个基本完整的开发工具集,它包括了整个软件生命周期中所需要的大部分工具,如UML工具.代码管控工具.集成开发环境(IDE)等等.所写的目标代码适用于微软支持的所有 ...

  10. JAVA基础知识之NIO——Buffer.Channel,Charset,Channel文件锁

    NIO机制 NIO即NEW IO的意思,是JDK1.4提供的针对旧IO体系进行改进之后的IO,新增了许多新类,放在java.nio包下,并对java.io下许多类进行了修改,以便使用与nio. 在ja ...