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. 【Oracle】rollup函数

    当我们在做报表统计的时候,很多时候需要用到‘合计’这个功能,比如我们想得到如下格式的报表: 这张表是按照deptno分组,然后按照deptno分组合计.rollup函数可以完美的解决这个问题. 1.建 ...

  2. 2星|《工业X.0》:物联网的资料汇编

    工业X.0:实现工业领域数字价值 看完比较失望,没有看到新的观点想法.基本算是物联网的资料汇编.总体评价2星. 以下是书中一些内容的摘抄: 1:例如,埃森哲为其员工开发了一个用例,用增强现实技术解决实 ...

  3. Architecture:话说科学家/工程师/设计师/商人

    从使命.目的.行为的不同,可以归类人群到科学家.工程师.设计师.商人等等.使命分别是:1.携带当下社会的财富对未来探索,希望引发变革:2.掌握工程全貌.完成整个工程的圣经周期:3.在工程的设计层面做文 ...

  4. 【C++】颜色的设置

    1.改变整个控制台的颜色用 system("color 0A"); 其中color后面的0是背景色代号,A是前景色代号.各颜色代码如下: 0=黑色 1=蓝色 2=绿色 3=湖蓝色  ...

  5. HDU_5690_快速幂,同余的性质

    All X Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  6. 备份xx

    https://www.tuicool.com/articles/V3EBzev https://www.tuicool.com/topics/11080087?st=0&lang=1& ...

  7. snv报错

    ERROR svn: E160028: Commit failed svn出现上面错误,先更新再提交即可解决.

  8. 基于日志实现ssh服务防护脚本

    grep -n "Failed password" secure | sed -nr 's/.*from(.*)port.*/\1/gp' | sort -n |uniq -c|s ...

  9. [luogu2624 HNOI2008]明明的烦恼 (prufer+高精)

    传送门 Solution 根据prufer序列做的题,具体可以看这里 还知道了一种避免高精除的方法quq Code #include <cmath> #include <cstdio ...

  10. python-flask-2 安装及设定 flask

    https://linoxide.com/linux-how-to/install-flask-python-ubuntu/ 1. prerequisites > create a new us ...