POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186
|
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 Sample Output 1 Hint
Cow 3 is the only cow of high popularity.
Source |
题意:
一群牛中找被其它全部牛觉得是受欢迎的牛的数量。当中受欢迎有传递性。比方A觉得B受欢迎。B觉得C受欢迎,那么A觉得C也是受欢迎的。
分析:
假设某头牛是受欢迎的。那么从其它全部牛出发都能到达这头牛。假设用搜索做似乎太过复杂。首先进行强联通缩点。这样得到一个DAG,假设该DAG有且仅有一个出度为0的缩点点(极大强联通分量),那么这个缩点包含的牛的数量即为答案。
/*
*
* Author : fcbruce <fcbruce8964@gmail.com>
*
* Time : Tue 14 Oct 2014 03:00:16 PM CST
*
*/
#include <cstdio>
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cctype>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10 #ifdef _WIN32
#define lld "%I64d"
#else
#define lld "%lld"
#endif #define maxm 50007
#define maxn 10007 using namespace std; int n,m;
int fir[maxn];
int u[maxm],v[maxm],nex[maxm];
int e_max; int pre[maxn],low[maxn],sccno[maxn],w[maxn];
int st[maxn],top;
int scc_cnt,dfs_clock; int deg[maxn]; inline void add_edge(int s,int t)
{
int e=e_max++;
u[e]=s;v[e]=t;
nex[e]=fir[u[e]];fir[u[e]]=e;
} void tarjan_dfs(int s)
{
pre[s]=low[s]=++dfs_clock;
st[++top]=s;
for (int e=fir[s];~e;e=nex[e])
{
int t=v[e];
if (pre[t]==0)
{
tarjan_dfs(t);
low[s]=min(low[s],low[t]);
}
else
{
if (sccno[t]==0)
low[s]=min(low[s],pre[t]);
}
} if (pre[s]==low[s])
{
scc_cnt++;
for (;;)
{
int x=st[top--];
sccno[x]=scc_cnt;
w[scc_cnt]++;
if (x==s) break;
}
}
} void find_scc()
{
top=-1;
scc_cnt=dfs_clock=0;
memset(pre,0,sizeof pre);
memset(low,0,sizeof low);
memset(w,0,sizeof w);
for (int i=1;i<=n;i++)
if (pre[i]==0) tarjan_dfs(i);
} int main()
{
#ifdef FCBRUCE
freopen("/home/fcbruce/code/t","r",stdin);
#endif // FCBRUCE scanf("%d%d",&n,&m); e_max=0;
memset(fir,-1,sizeof fir); for (int e=0,u,v;e<m;e++)
{
scanf("%d%d",&u,&v);
add_edge(u,v);
} find_scc(); memset(deg,0,sizeof deg); for (int e=0;e<e_max;e++)
{
if (sccno[u[e]]==sccno[v[e]]) continue;
deg[sccno[u[e]]]++;
} int cnt=0,the_one; for (int i=1;i<=scc_cnt;i++)
{
if (deg[i]==0)
{
cnt++;
the_one=i;
}
} if (cnt==1) printf("%d\n",w[the_one]);
else puts("0"); return 0;
}
POJ 2186 Popular Cows (强联通)的更多相关文章
- POJ 2186 Popular Cows(强联通分量)
题目链接:http://poj.org/problem?id=2186 题目大意: 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种 ...
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- POJ 2186 Popular cows(Kosaraju+强联通分量模板)
题目链接:http://poj.org/problem?id=2186 题目大意:给定N头牛和M个有序对(A,B),(A,B)表示A牛认为B牛是红人,该关系具有传递性,如果牛A认为牛B是红人,牛B认为 ...
- POJ 2186 Popular Cows(强联通+缩点)
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
- [强连通分量] POJ 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- POJ 2186 Popular Cows(强连通分量缩点)
题目链接:http://poj.org/problem?id=2186 题目意思大概是:给定N(N<=10000)个点和M(M<=50000)条有向边,求有多少个“受欢迎的点”.所谓的“受 ...
随机推荐
- SMTP邮件传输协议发送邮件和附件(转)
1. SMTP 常用命令简介 1). SMTP 常用命令 HELO/EHLO 向服务器标识用户身份 MAIL 初始化邮件传输 mail from: RCPT 标识单个的邮件接收人:常在MAIL ...
- Python函数式编程:内置函数map()使用说明
一.概述 map操作是函数式编程中的重要技术之一,其作用就是对一个集合中的每个元素做处理,生成一个新的元素,由这些新的元素组成一个新的集合的返回. 所以map操作后,产生的新集合的元素个数和原集合的元 ...
- 当webview遇到了Slidingmenu,webView出现卡白,解决方案
先介绍一下什么是SlidingMenu: Sliding Menu的是一种比较新的设置界面或配置界面效果,在主界面左滑或者右滑出现设置界面,能方便的进行各种操作.如Evernote.Google+.F ...
- 另外一种方式装win2008r2
装系统有很多方法,但是这种,我很少用. 注意第二个红圈处,是要启动的电话引导盘符.容易选择你的启动U盘,如果是后者,表现出的结果就是引导U盘不能引导,且安装的电脑也会显示ntdl丢失.当然,也可以解决 ...
- c++,extern “c”
C++中extern "C"的设立动机是实现C++与C及其它语言的混合编程. C++支持函数重载,而过程式语言C则不支持.函数被C++编译后在符号库中的名字与C语言的不同. 例如, ...
- webform--常用的控件
一.简单控件 1.Lable——标签:在网页中呈现出来的时候会变成span标签 属性:Text——标签上的文字 BackColor,ForeColor——背景色,前景色 Font——字体 Bold- ...
- django-template-loader
当在settings.py中设置了如下 TEMPLATE_LOADERS=( 'django.template.loaders.filesystem.Loader', 'django.template ...
- iOS开发之自己封装一个progressHUD控件
看了几个轻量级的progress view 我觉得KVNProgress做的最漂亮吧 突然我想为什么我自己不封装一个控件 然后我研究了一下KVNProgress KVN简单的界面是由storyboar ...
- windows phone7开发环境配置错误
遇到下面这样一个问题:在配置windows phoe7开发环境的时候出现如下错误,以及相应的解决方案,希望对大家有所帮助. 装完环境后出现下面错误: [caption id="attachm ...
- LifecyclePhaseNotFoundException(转)
This error is generated if you try to invoke a build command that Maven does not understand. In gene ...