Popular Cows

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 2   Accepted Submission(s) : 1
Problem 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
/*找出最受欢迎的牛*/
#include<stdio.h>
#include<string.h>
#include<queue>
#include<stack>
#include<vector>
#include<algorithm>
using namespace std;
#define MAX 50010
struct node
{
int u,v;
int next;
}edge[MAX];
int low[MAX],dfn[MAX];
int sum,sumin,sumout;
int sccno[MAX],scc_cnt;
int head[MAX],dfs_clock,cnt;
bool Instack[MAX];
vector<int>G[MAX];
vector<int>scc[MAX];
stack<int>s;
int in[MAX],out[MAX];
int m,n;
void init()
{
memset(head,-1,sizeof(head));
cnt=0;
}
void add(int u,int v)
{
edge[cnt].u=u;
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}
void getmap()
{
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
add(a,b);
}
}
void tarjan(int u,int fa)
{
int v;
low[u]=dfn[u]=++dfs_clock;
s.push(u);
Instack[u]=true;
for(int i=head[u];i!=-1;i=edge[i].next)
{
v=edge[i].v;
if(!dfn[v])
{
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(Instack[v])
low[u]=min(low[u],dfn[v]);
}
if(low[u]==dfn[u])
{
scc_cnt++;
scc[scc_cnt].clear();
for(;;)
{
v=s.top();
s.pop();
Instack[v]=false;
scc[scc_cnt].push_back(v);
sccno[v]=scc_cnt;
if(u==v) break;
}
}
}
void find(int l,int r)
{
memset(low,0,sizeof(low));
memset(dfn,0,sizeof(dfn));
memset(sccno,0,sizeof(sccno));
memset(Instack,false,sizeof(Instack));
scc_cnt=dfs_clock=0;
for(int i=l;i<=r;i++)
if(!dfn[i])
tarjan(i,-1);
}
void suodian()
{
for(int i=1;i<=scc_cnt;i++)
G[i].clear(),in[i]=0,out[i]=0;
for(int i=0;i<cnt;i++)
{
int u=sccno[edge[i].u];
int v=sccno[edge[i].v];
if(v!=u)
{
G[u].push_back(v);
out[u]++,in[v]++;
}
}
}
void solve()
{
int sum=0;
int ans=0;
for(int i=1;i<=scc_cnt;i++)
{
if(out[i]==0)
//出度为零说明在新图里他是叶子节点,现在统计叶子节点个数
{
sum++;
ans+=scc[i].size();
}
}
if(sum>1) printf("0\n");
if(sum==0) printf("%d\n",n);
//如果只有1个scc,说明全部的牛都是最受欢迎的
if(sum==1) printf("%d\n",ans);
//如果sum==0说明在新图里叶子节点只有一个
//那么当前的scc中所有的牛都是最受欢迎的
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
getmap();
find(1,n);
suodian();
solve();
}
return 0;
}

poj--2186--Popular Cows (scc+缩点)的更多相关文章

  1. POJ 2186 Popular cows(SCC 缩点)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

  2. POJ 2186 Popular Cows tarjan缩点算法

    题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...

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

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

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

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

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

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

  6. POJ 2186 Popular Cows (强联通)

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

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

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

  8. poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27698   Accepted: 11148 De ...

  9. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

  10. poj 2186 Popular Cows

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29908   Accepted: 12131 De ...

随机推荐

  1. 利用VMware14安装虚拟机(Win7&CentOS6.4)

    安装Win7 https://blog.csdn.net/Yangchenju/article/details/80694597 安装CentOS6.4 https://blog.csdn.net/u ...

  2. RecyclerView 悬浮/粘性头部效果3种方式

    但是以上两种方式onDrawOver()方法实现逻辑对初次查看该段代码要花时间理解.下面代码逻辑(原理一样,同样参考大神代码)相对清晰,易理解 public class StickyDecoratio ...

  3. Qt中采用多线程实现Socket编程

    Socket通常也称作"套接字",应用程序通常通过"套接字"向网络发出请求或者应答网络请求. 本文介绍的是Qt中采用多线程Socket编程,由于工作的需要,开始 ...

  4. Super Poker II UVA - 12298 FFT_生成函数

    Code: #include<bits/stdc++.h> #define maxn 1000000 #define ll long long #define double long do ...

  5. jQuery默认select选择第一个元素

    $("#id option:first").prop("selected", 'selected');

  6. Nginx服务器部署SSL证书手机不信任解决方法

    在wosign申请证书并按指南正确部署证书后,如果发现PC浏览器访问正常,手机或safari浏览器提示证书不受信任,那肯定是在文件传输解压过程中导致证书文件中出现空格.乱码之类的情况,这里教您轻松四步 ...

  7. 【剑指Offer】23、二叉搜索树的后序遍历序列

      题目描述:   输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同.   解题思路:   对于后续遍历序列,序 ...

  8. STL源码分析之第一级配置器

    前言 上一节我们分析了空间配置器对new的配置, 而STL将空间配置器分为了两级, 第一级是直接调用malloc分配空间, 调用free释放空间, 第二级三就是建立一个内存池, 小于128字节的申请都 ...

  9. POJ3617 Best Cow Line【贪心】

    Description  给定长度为n的字符串S,要构造一个长度为n的字符串T.起初,T是空串,随后反复进行下列任意操作:  1.从S的头部删除一个字符,加到T的尾部  2.从S的尾部删除一个字符,加 ...

  10. [luogu2587 ZJOI2008] 泡泡堂 (贪心)

    传送门 Description 第XXXX届NOI期间,为了加强各省选手之间的交流,组委会决定组织一场省际电子竞技大赛,每一个省的代表队由n名选手组成,比赛的项目是老少咸宜的网络游戏泡泡堂.每一场比赛 ...