Popular Cows

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.


说实话这个强连通分量看的挺迷的,似懂非懂的(思想看懂了,代码的实现方法不太懂),暂时就不写解题心得了,等以后熟悉了再写。


#include<stdio.h>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 100;
int dfn[maxn],low[maxn],head[maxn],degree[maxn],_stack[maxn],top,ans,num[maxn],Time;
bool vis[maxn];
struct node
{
int to,Next;
}edge[maxn]; void add_edge(int m)
{
int u,v;
int cnt = 0;
while(m--)
{
scanf("%d%d",&u,&v);
//注意存图的方法
edge[cnt].Next = head[u];
edge[cnt].to = v;
head[u] = cnt++;
}
} void init()
{
//很烦的初始化但是要看清楚,edge和head是初始化为-1,这样可以便于分别
Time = 0;
ans = 0;
top = 0;
memset(dfn,0,sizeof(dfn));
memset(head,-1,sizeof(head));
memset(low,0,sizeof(low));
memset(edge,-1,sizeof(edge));
memset(vis,false,sizeof(vis));
memset(degree,0,sizeof(degree));
} void tarjan(int u)
{
_stack[top] = u;
top++;
low[u] = dfn[u] = Time;
Time++;
vis[u] = true;
for(int i=head[u];i!=-1;i=edge[i].Next)
{
int v = edge[i].to;
if(!vis[v])
{
tarjan(v);
low[u] = min(low[u],low[v]);//找到这个点第一次在stack中出现的位置
}
else
low[u] = min(low[u],dfn[v]);
} if(low[u] == dfn[u])//找到一个分量
{
ans++;//分量数目加一,从stack中弹出
while(top>0 && _stack[top] != u)
{
top--;
vis[_stack[top]] = true;
num[_stack[top]] = ans;
}
}
} int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init();
add_edge(m); for(int i=1;i<=n;i++)
if(!vis[i])
tarjan(i); int sum = 0,x;
for(int i=1;i<=n;i++)
for(int j=head[i];j!=-1;j=edge[j].Next)
if(num[i] != num[edge[j].to])//计算缩点后每个点的出度
degree[num[i]]++; for(int i=1;i<=ans;i++)
if(!degree[i])
{
sum++;
x = i;
} int Ans = 0;
if(sum == 1)//只能形成一个缩点
{
for(int i=1;i<=n;i++)
if(num[i] == x)
Ans++;
printf("%d\n",Ans);
}
else
printf("0\n");
}
return 0;
}

图论:POJ2186-Popular Cows (求强连通分量)的更多相关文章

  1. POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】

    Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23445   Accepted: 9605 Des ...

  2. POJ2186 Popular Cows 题解 强连通分量入门题

    题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...

  3. POJ2186 Popular Cows 题解 强连通分量

    题目链接:http://poj.org/problem?id=2186 题目大意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系 ...

  4. POJ2186 Popular Cows(强连通分量)

    题目问一个有向图所有点都能达到的点有几个. 先把图的强连通分量缩点,形成一个DAG,那么DAG“尾巴”(出度0的点)所表示的强连通分量就是解,因为前面的部分都能到达尾巴,但如果有多个尾巴那解就是0了, ...

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

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

  6. poj 2186 "Popular Cows"(强连通分量入门题)

    传送门 参考资料: [1]:挑战程序设计竞赛 题意: 每头牛都想成为牛群中的红人. 给定N头牛的牛群和M个有序对(A, B),(A, B)表示牛A认为牛B是红人: 该关系具有传递性,所以如果牛A认为牛 ...

  7. POJ 2186 Popular Cows(强连通分量Kosaraju)

    http://poj.org/problem?id=2186 题意: 一个有向图,求出点的个数(任意点可达). 思路: Kosaraju算法的第一次dfs是后序遍历,而第二次遍历时遍历它的反向图,从标 ...

  8. poj2186 Popular Cows(强连通)

    崇拜有传递性.求所有牛都崇拜的牛tarjan算法求强连通. 如果不连通就不存在.如果联通,缩点后唯一一个出度为零的点就是答案,有多个则不存在. #include <vector> #inc ...

  9. POJ 2186 Popular Cows(强连通分量)

    [题目链接] http://poj.org/problem?id=2186 [题目大意] 给出一张有向图,问能被所有点到达的点的数量 [题解] 我们发现能成为答案的,只有拓扑序最后的SCC中的所有点, ...

  10. [poj 2186]Popular Cows[Tarjan强连通分量]

    题意: 有一群牛, a会认为b很帅, 且这种认为是传递的. 问有多少头牛被其他所有牛认为很帅~ 思路: 关键就是分析出缩点之后的有向树只能有一个叶子节点(出度为0). 做法就是Tarjan之后缩点统计 ...

随机推荐

  1. Gym - 101810F ACM International Collegiate Programming Contest (2018)

    bryce1010模板 http://codeforces.com/gym/101810 #include<bits/stdc++.h> using namespace std; #def ...

  2. 关于java中的不可变类(转)

    如何在Java中写出Immutable的类? 要写出这样的类,需要遵循以下几个原则: 1)immutable对象的状态在创建之后就不能发生改变,任何对它的改变都应该产生一个新的对象. 2)Immuta ...

  3. Oracle 恢复数据后,数据库中中文变成问号解决方法

    1.右击---我的电脑---环境变量 2.新增环境变量 变量名:LANG=zh_CN.GBK NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 3.重启PLSQL或 ...

  4. session 跟 cookie 关系

    面试经验: 谈到Session的时候就侃Session和Cookie的关系:Cookie中的SessionId. 和别人对比说自己懂这个原理而给工作带来的方便之处.   客户第一次发送请求给服务器,此 ...

  5. MVC dropdownlist 后端设置select属性后前端依然不能默认选中的解决方法

    -----------------------------------来自网上的解决方法--------------------------------------------- ASP.Net MV ...

  6. 用java代码写一个简单的网上购物车程序

    需求:1.写一个商品类,有商品编号.商品名称.商品分类.商品单价属性.2.写一个商品条目信息类,有商品和数量两个属性,有商品总价格方法. 3.写一个购物车类,有添加商品方法.查看订单信息,删除商品,修 ...

  7. T4310 祖玛游戏

    题目描述 祖玛是一款曾经风靡全球的游戏,其玩法是:在一条轨道上初始排列着若干 个彩色珠子,其中任意三个相邻的珠子不会完全同色.此后,你可以发射珠子到 轨道上并加入原有序列中.一旦有三个或更多同色的珠子 ...

  8. 阿里云栖社区dubbo 资源整理

    1.apache dubbo pdf git 地址:https://github.com/dubbo/awesome-dubbo/tree/master/slides/meetup/201905%40 ...

  9. android textview添加滚动条

    给textview添加滚动条 方式一: xml代码: //设置滚动条的方向 android:scrollbars="vertical" java中设置 tView=(TextVie ...

  10. FPGA的嵌入式RAM

    FPGA中的嵌入式RAM分为两种:专用的BRAM和分布是RAM(用LUT实现的).这两种RAM又可以配置成单端口和双端口的RAM和ROM.双端口RAM又可以根据读写地址是否在同一块分为Double P ...