Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 37111   Accepted: 15124

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. 
 
题意    n头牛 m个关系 a认为b受欢迎   b认为c受欢迎的话 a也认为c受欢迎 问有多少头牛被其他所有人受欢迎
解析   我们可以试着去分组 把互相喜欢的人分到一组(组内可以喜欢其他人 但组内人员必须互相喜欢,有向图任意两点相互可达)如果a组内一个人喜欢b组一个人的话  那么就是a组的人都喜欢b组的所有人,但是,就不同时存在b组有人也喜欢a的人,因为如果存在的话a和b就是一个组了。然后我们就可以推出,当且仅当只有一个组x不喜欢其他组时 有答案 答案是这个组的大小否则输出0 
强联通图缩点,再看下缩完点之后每个点的出度就好了。
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <vector>
#include <iomanip>
using namespace std;
const int maxn = 1e5+;
const int maxm = 1e4+;
const int inf = 0x3f3f3f3f;
const double epx = 1e-;
typedef long long ll;
const ll INF = 1e18;
const double pi = acos(-1.0);
int mp[maxn];
struct node
{
int v,next;
}edge[maxn];
int dfn[maxn],low[maxn],index,visit[maxn],cnt,tot;
int point[maxn];
int heads[maxn],stack[maxn],num;
void add(int x,int y)
{
edge[++cnt].next=heads[x];
edge[cnt].v=y;
heads[x]=cnt;
return;
}
void tarjan(int x)
{
dfn[x]=low[x]=++tot;
stack[++index]=x;
visit[x]=;
for(int i=heads[x];i!=-;i=edge[i].next)
{
if(!dfn[edge[i].v])
{
tarjan(edge[i].v);
low[x]=min(low[x],low[edge[i].v]);
}
else if(visit[edge[i].v])
{
low[x]=min(low[x],dfn[edge[i].v]);
}
}
if(low[x]==dfn[x])
{
do{
int temp=stack[index];
point[temp]=num;
visit[temp]=;
index--;
}while(x!=stack[index+]);
num++;
}
return;
}
int main()
{
int n,m;
memset(heads,-,sizeof(heads));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(mp,,sizeof(mp));
scanf("%d%d",&n,&m);
tot=cnt=num=index=;
for(int i=;i<m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
add(u,v);
}
for(int i=;i<=n;i++)
if(!dfn[i]) tarjan(i);
int sum=;
for(int i=;i<=n;i++)
{
for(int j=heads[i];j!=-;j=edge[j].next)
{
if(point[i]!=point[edge[j].v])
{
mp[point[i]]=;
}
}
}
int cont=,index;
for(int i=;i<num;i++)
if(mp[i]==)
{
cont++,index=i;
}
if(cont==)
{
int ans=;
for(int i=;i<=n;i++)
if(point[i]==index)
ans++;
cout<<ans<<endl;
}
else
cout<<""<<endl;
return ;
}

推荐两篇博客 https://blog.csdn.net/qq_34374664/article/details/77488976

http://blog.miskcoo.com/2016/07/tarjan-algorithm-strongly-connected-components

POJ 2186 tarjan+缩点 基础题的更多相关文章

  1. poj 2186(tarjan+缩点)

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 37083   Accepted: 15104 De ...

  2. poj 2955 Brackets (区间dp基础题)

    We give the following inductive definition of a “regular brackets” sequence: the empty sequence is a ...

  3. POJ 2762 tarjan缩点+并查集+度数

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15494 ...

  4. POJ - 2186  Popular Cows tarjain模板题

    http://poj.org/problem?id=2186 首先求出所有的强连通分量,分好块.然后对于每一个强连通分量,都标记下他们的出度.那么只有出度是0 的块才有可能是答案,为什么呢?因为既然你 ...

  5. POJ 3694 (tarjan缩点+LCA+并查集)

    好久没写过这么长的代码了,题解东哥讲了那么多,并查集优化还是很厉害的,赶快做做前几天碰到的相似的题. #include <iostream> #include <algorithm& ...

  6. poj 2186 tarjan求强连通分量

    蕾姐讲过的例题..玩了两天后才想起来做 貌似省赛之后确实变得好懒了...再努力两天就可以去北京玩了! 顺便借这个题记录一下求强连通分量的算法 1 只需要一次dfs 依靠stack来实现的tarjan算 ...

  7. poj 2186 (强连通缩点)

    题意:有N只奶牛,奶牛有自己认为最受欢迎的奶牛.奶牛们的这种“认为”是单向可传递的,当A认为B最受欢迎(B不一定认为A最受欢迎),且B认为C最受欢迎时,A一定也认为C最受欢迎.现在给出M对这样的“认为 ...

  8. POJ 2672 Tarjan + 缩点 + 拓扑思想

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17383 ...

  9. poj 2762(tarjan缩点+判断是否是单链)

    Going from u to v or from v to u? Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19234 ...

随机推荐

  1. sdut1650I-Keyboard(dp)

    链接 题目大意就相当于 跟你一串字符串 让你截成k段 使总体的值最小 想法是递归的 递归太慢 可以转换为递推的 这样就有可以推出状态方程 dp[i][j] = max(dp[i][j],dp[i-1] ...

  2. [转]Resolve Team Foundation Version Control conflicts

    本文转自:http://msdn.microsoft.com/en-us/library/ms181432.aspx An advantage of using Team Foundation ver ...

  3. windows8.1专业版 关闭ie11总是已停止工作

    该问题通常原因: 1 系统重病毒: 2 系统和安装的软件不兼容导致. 解决方案: 1 杀毒更新至最新进行杀毒,仍未解决,重新安装系统: 2 目前身边人员多数属于该情况: 1 如安装了输入法.迅雷或其它 ...

  4. web+ admin template,spa管理应用后台,easyui后台正式发布

    演示地址:http://admintemplate.webplus.org.cn/ v1.0 (2016/7/27) 扁平化风格 全屏支持 后台管理不使用iframe,全ajax开发 权限管理 商品管 ...

  5. java 对象流的简单使用

    对象的输入输出流的作用: 用于写入对象 的信息和读取对象的信息. 使得对象持久化.   ObjectInputStream   : 对象输入流   ObjectOutPutStream  :对象输出流 ...

  6. iOS Programming Controlling Animations 动画

    iOS Programming Controlling Animations 动画 The word "animation" is derived from a Latin wor ...

  7. JavaScript——XMLHttpRequest 家族

    https://www.zhangxinxu.com/wordpress/2013/10/understand-domstring-document-formdata-blob-file-arrayb ...

  8. leetcode_654. Maximum Binary Tree

    https://leetcode.com/problems/maximum-binary-tree/ 给定数组A,假设A[i]为数组最大值,创建根节点将其值赋为A[i],然后递归地用A[0,i-1]创 ...

  9. Maven的pom报错的解决方法

    如果在MyEclipse里面导入项目,导入不了,如下图 接下来可以点击Import Maven Projects里的Action那一行Resolve Later. 点击Do Not Execute(a ...

  10. MRC转ARC

    转载请注明出处:http://blog.csdn.net/cywn_d/article/details/18222671 1.删除所有retain,release和autorelease. 2.把原来 ...