Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27531   Accepted: 11077

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

 
本题题意时每个牛都可以仰慕求它的牛,而且仰慕关系可以互相传递
问最受欢迎的牛的数量有多少个
可以利用强联通的缩点法,最后求缩点之后的初度为0的点的数量,
注意一个问题就是:
如果没有出度为0的点,那么结果输出答案为0
下面提供了几组样例,主要注意的输出为0的点需要特别判断
解决该题目的模板同上面的那个解决学校之间传递数量文件关系的拿到题所用的模板都是一套模板,而且判断入度出度那里也咩有变化
ps::附上嗲吗
#include <string.h>
#include <stdio.h>
#define V 10500
#define E 50500 struct edge
{
int to, next;
}Edge[E];
int head[V], e, n; int indeg[V], outdeg[V]; //点的入度和出度数
int belong[V], low[V], dfn[V], scc, cnt;//dfn[]:遍历到u点的时间; low[]:u点可到达的各点中最小的dfn[v]
int S[V], top;
bool vis[V];//v是否在栈中 int addedge(int u, int v)
{
Edge[e].to = v;
Edge[e].next = head[u];
head[u] = e++;
return ;
}
void tarjan(int u)
{
int v;
dfn[u] = low[u] = ++cnt;//开始时dfn[u] == low[u]
S[top++] = u;//不管三七二十一进栈
vis[u] = true;
for (int i=head[u]; i!=-; i=Edge[i].next)
{
v = Edge[i].to;
if (dfn[v] == )//如果v点还未遍历
{
tarjan(v);//向下遍历
low[u] = low[u] < low[v] ? low[u] : low[v];//确保low[u]最小
}
else if (vis[v] && low[u] > dfn[v])//v在栈中,修改low[u]
low[u] = dfn[v];
}
if (dfn[u] == low[u])//u为该强连通分量中遍历所成树的根
{
++scc;
do
{
v = S[--top];//栈中所有到u的点都属于该强连通分量,退栈
vis[v] = false;
belong[v] = scc;
} while (u != v);
} } int solve(){
scc = top = cnt = ;
memset(dfn, , sizeof(dfn));
memset(vis, false, sizeof(vis));
for (int u=; u<=n; ++u)
if (dfn[u] == )
tarjan(u);
return scc;
} void count_deg()
{
memset(indeg, , sizeof(indeg));
memset(outdeg, , sizeof(outdeg));
for (int u=; u<=n; ++u)
for (int i=head[u]; i!=-; i=Edge[i].next)
{
int v = Edge[i].to;
if (belong[u] != belong[v])
{
indeg[belong[v]]++;
outdeg[belong[u]]++;
}
}
} int main()
{
int u, v, i;
int m;
while (~scanf("%d%d", &n,&m))
{
e = ;
memset(head, -, sizeof(head));
for (int i=;i<=m;i++){
scanf("%d%d",&u,&v);
addedge(u, v);
}
solve();
count_deg(); int num=,tmp;
for(int i = ; i <= scc; i++)
{
if(!outdeg[i])
{
num++;
tmp = i;
} }
int ans=;
if(num == )
{
for(int i = ; i <= n; i++)
{
if(belong[i] == tmp)
ans++;
}
printf("%d\n", ans);
}
else
{
printf("0\n");
} }
return ;
}

连通图 poj2186 最受欢迎的牛(求最受欢迎的牛的数量)的更多相关文章

  1. 求1+2+3...+n 牛客网 剑指Offer

    求1+2+3...+n 牛客网 剑指Offer 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). ...

  2. HihoCoder1084: 扩展KMP(二分+hash,求T串中S串的数量,可以失配一定次数)

    时间限制:4000ms 单点时限:4000ms 内存限制:256MB 描述 你知道KMP吗?它是用于判断一个字符串是否是另一个字符串的子串的算法.今天我们想去扩展它. 在信息理论中,在两个相同长度的字 ...

  3. 《编程题》穷举法求N年后有多少头牛

    若一头小母牛,从出生起第四个年头开始每年生一头母牛,按这个规律,第N年时有多少头母牛? #include <iostream> int main(int argc, const char ...

  4. POJ-3107 Godfather 求每个节点连接的联通块数量

    dp[n][2],维护儿子的联通块数量和父亲的联通块数量. 第一遍dfs求儿子,第二遍dfs求爸爸. #include<iostream> #include<cstring> ...

  5. 牛客oi测试赛 二 B 路径数量

    题目描述 给出一个 n * n 的邻接矩阵A. A是一个01矩阵 . A[i][j]=1表示i号点和j号点之间有长度为1的边直接相连. 求出从 1 号点 到 n 号点长度为k的路径的数目. 输入描述: ...

  6. 求数组中的逆序对的数量----剑指offer36题

    在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数: 如数组{7,5,6,4},逆序对总共有5对,{7,5},{7,6},{7, ...

  7. 547. Friend Circles 求间接朋友形成的朋友圈数量

    [抄题]: There are N students in a class. Some of them are friends, while some are not. Their friendshi ...

  8. JSF页面中的JS取得受管bean的数据(受管bean发送数据到页面)

    JSF中引入jsf.js文件之后,可以拦截jsf.ajax.request请求.一直希望有一种方法可以像jquery的ajax一样,能在js中异步取得服务器端发送的数据.无奈标准JSF并没有提供这样的 ...

  9. 不裸缩点》。。。POJ2186受欢迎的牛

    不裸缩点>...POJ2186受欢迎的牛 :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: ...

随机推荐

  1. 什么是Office Online Server和SharePoint 2016

    Microsoft Office Online Server是Microsoft Office Web Apps(OWA)服务器的下一个版本,最初于2012年发布.,可以下载Office Online ...

  2. windows 密钥

    server 2016数据中心CB7KF-BWN84-R7R2Y-793K2-8XDDG

  3. python基础教程总结4—基本语句

    一.print 和 import 的更多信息 print 打印多个表达式也是可行的,只要将它们用逗号隔开就好: >>> print('Age:' , 42) Age: 42 可以看到 ...

  4. UVA 1451 Average平均值 (数形结合,斜率优化)

    摘要:数形结合,斜率优化,单调队列. 题意:求一个长度为n的01串的子串,子串长度至少为L,平均值应该尽量大,多个满足条件取长度最短,还有多个的话,取起点最靠左. 求出前缀和S[i],令点Pi表示(i ...

  5. Spark性能调优之道——解决Spark数据倾斜(Data Skew)的N种姿势

    原文:http://blog.csdn.net/tanglizhe1105/article/details/51050974 背景 很多使用Spark的朋友很想知道rdd里的元素是怎么存储的,它们占用 ...

  6. FreeRTOS笔记

    任务的创建和删除(静态方法) 任务创建后要开启调度器. FreeRTOSConfig.h 1. 改宏 使能静态创建函数. 会出现,有两个函数未定义. Cortex-M中断管理(上) NVIC:嵌套向量 ...

  7. batchsize对收敛速度的影响

    想象一下,当mini-batch 是真个数据集的时候,是不是就退化成了 Gradient Descent,这样的话,反而收敛速度慢.你忽略了batch 增大导致的计算 batch 代价变大的问题.如果 ...

  8. 记录一次mysql中自定义获取UUID的函数

    循环方式一: DELIMITER :; drop function if exists test.fn_test:; create function test.fn_test() ) begin ) ...

  9. oracle 命中率

    一般在I/O 使用中,为了提高系统处理速度,系统提前将数据读入一块内存区,叫高速缓存,但提前读入的数据未必就是需要的,这就是命中率..计算公式为 命中率=1-(physical reads/(db b ...

  10. iOS开发遇到的坑之五--解决工程已存在plist表,数据却不能存入的问题

    想写这篇博客其实在一两个月前开发遇见的时候就想把这个问题写成博客的,奈何自己一直懒外加一直没有时间,就把这个事情给耽搁了,好在当时知道下自己一定要把这个问题给描述出来,免得以后其他人遇到这个问题会纠结 ...