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. 洛谷 P2424 约数和

    题目背景 Smart最近沉迷于对约数的研究中. 题目描述 对于一个数X,函数f(X)表示X所有约数的和.例如:f(6)=1+2+3+6=12.对于一个X,Smart可以很快的算出f(X).现在的问题是 ...

  2. 有一个无效 SelectedValue,因为它不在项目列表中

    “Drp_XX”有一个无效 SelectedValue,因为它不在项目列表中 出现以上异常的原因肯定是将DrowDownList控件的SelectedValue属性赋值为一个列表中不存在的值.那么我们 ...

  3. C、C++、C#中struct的简单比较

    比较这三者是因为C.C++.C#这三者存在着一定的血缘关系,而他们三者都有的struct却有很大的不同. 功能 C中的struct是最简单的.只能有成员变量而不能有成员函数. C++和C#中都支持,而 ...

  4. HDU 4741 Save Labman No.004 (几何)

    题意:求空间两线的最短距离和最短线的交点 题解: 线性代数和空间几何,主要是用叉积,点积,几何. 知道两个方向向量s1,s2,求叉积可以得出他们的公共垂直向量,然后公共垂直向量gamma和两线上的点形 ...

  5. [神经网络]一步一步使用Mobile-Net完成视觉识别(四)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第四篇,下载预训练模型并训练自己的数据集. 前面我们配置好了labelmap,下面我们开始下载训练好的 ...

  6. ArrayList 源码分析(JDK1.8)

    ArrayList简介  ArrayList 是一个数组队列,相当于 动态数组.与Java中的数组相比,它的容量能动态增长.它继承于AbstractList,实现了List, RandomAccess ...

  7. CentOS lvm

    1.创建PVpvcreate /dev/sdb /dev/sdc或pvcreate /dev/sdb1 /dev/sdc1 2.查看PVpvdisplay 3.创建VGvgcreate vgdata ...

  8. 为DataGridView控件实现复选功能

    实现效果: 知识运用: DataGridViewCheckBoxColumn类 实现代码: private class Fruit { public int Price { get; set; } p ...

  9. NFS缓存IO机制

    NFS的缓存IO机制<一> async 参数模式下分析 NFS 默认的mount参数为async,async 参数表示内核不会透传程序的IO请求给sever,对于写IO会延迟执行,积累一定 ...

  10. 01_1_jdom调用xml文件

    01_1_jdom调用xml文件 1. 导入jdom.jar包 2. xml文件内容 test.xml <?xml version="1.0" encoding=" ...