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. (转载)WPF:DataGrid设置行、单元格的前景色

    WPF:DataGrid设置行.单元格的前景色 0. 说明 /********************************** 本示例实现功能1.DataGrid基本操作2.列标题样式3.内容居中 ...

  2. Page_Load与sender -- PostBack是由哪个 ASP.NET控件引起 ?

    Page_Load与sender -- PostBack是由哪个 ASP.NET控件引起 ? 之前有讨论过ASP.NET Web Form "事件"里面的 "sender ...

  3. ASP.NET(Web Form)绘制图表 -- Google Chart 三部曲

    ASP.NET(Web Form)绘制图表 -- Google Chart 三部曲 ASP.NET(Web Form)绘制图表 -- Google Chart 三部曲 1.  网页绘制图表 Googl ...

  4. [学习笔记] C++ 历年试题解析(二)--程序题

    发现程序题也挺有价值的. 顺便记录下来几道. 1.题目 #include <iostream> #include <cstring> using namespace ① std ...

  5. js原型,原型链的理解

    1.所有引用类型(函数.数组.对象)都拥有_proto_属性(隐式原型) 2.所有函数拥有prototype属性(显式原型)(仅限函数) 3.原型对象:拥有prototype属性的对象,在定义函数时就 ...

  6. 2018.4.10 Ubuntu cat命令解答、用法

    cat命令是linux下的一个文本输出命令,通常是用于观看某个文件的内容的: cat主要有三大功能: 1.一次显示整个文件. $ cat filename 2.从键盘创建一个文件. $ cat > ...

  7. NPM下载模块包说明

    博主对npm包安装收集了各种资料和实践后对它们之间的差异整理,写下这篇文章避免自己忘记,同时也给node.js猿友一点指引. 我们在使用 npm install 安装模块的模块的时候 ,一般会使用下面 ...

  8. PAT (Basic Level) Practise (中文)- 1018. 锤子剪刀布 (20)

    http://www.patest.cn/contests/pat-b-practise/1018 大家应该都会玩“锤子剪刀布”的游戏:两人同时给出手势,胜负规则如图所示: 现给出两人的交锋记录,请统 ...

  9. Linux学习日记:第一天

    一,登录Linux Login:test Password:123456 Last Login:Wed Dec 3 22:40:02 on tty1 test@ubuntu: startx    进入 ...

  10. vue 前端判断输入框不能输入0 空格。特殊符号。

    oninput="value=value.replace(/[^\d.]/g,'').replace(/\.{2,}/g,'.').replace('.','$#$').replace(/\ ...