Popular Cows---poj2186(缩点,强联通)
题目链接:http://poj.org/problem?id=2186
求有多少个点满足其他n-1个点都能到达这个点,是单向图;
所以我们可以把图进行缩点,之后求出度为0的那个点内包含的点的个数就是求得答案;
如果出度为0的不止一个,那么答案为0;
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#define N 10010
#define Lson r*2
#define Rson r*2+1
#define INF 0xfffffff
using namespace std; int Stack[N], top, Out[N], Time, flag;
int nBlock, Block[N], dfn[N], low[N], Is[N], n, Head[N], cnt; struct Edge
{
int v, next;
}e[N*N];
void Init()
{
nBlock = cnt = top = Time = flag = ;
memset(Head, -, sizeof(Head));
memset(low, , sizeof(low));
memset(dfn, , sizeof(dfn));
memset(Block, , sizeof(Block));
memset(Out, , sizeof(Out));
memset(Is, , sizeof(Is));
memset(Stack, , sizeof(Stack));
}
void Add(int u, int v)
{
e[cnt].v=v;
e[cnt].next=Head[u];
Head[u]=cnt++;
}
void Tajar(int u)
{
dfn[u]=low[u]=++Time;
Is[u]=;
Stack[top++]=u;
int v;
for(int i=Head[u]; i!=-; i=e[i].next)
{
v=e[i].v;
if(!dfn[v])
{
Tajar(v);
low[u]=min(low[u], low[v]);
}
else if(Is[v])
{
low[u]=min(low[u], dfn[v]);
}
}
if(low[u]==dfn[u])
{
++nBlock;
do
{
v=Stack[--top];
Is[v] = ;
Block[v]=nBlock;
}while(u!=v);
}
}
int main()
{
int u, v, m;
while(scanf("%d%d", &n, &m)!=EOF)
{
Init();
for(int i=; i<m; i++)
{
scanf("%d%d", &u, &v);
Add(u,v);
}
for(int i=; i<=n; i++)
if(!low[i])
{
Tajar(i);
}
for(int i=; i<=n; i++)
{
for(int j=Head[i]; j!=-; j=e[j].next)
{
u=Block[i]; v=Block[e[j].v];
if(u!=v)
Out[u]++;
}
}
flag=;
int Index;
for(int i=; i<=nBlock; i++)
{
if(Out[i]==)
{
flag++;
Index=i;
}
}
if(flag>)
{
printf("0\n");
continue;
}
int ans=;
for(int i=; i<=n; i++)
{
if(Block[i]==Index)
ans++;
}
printf("%d\n", ans);
}
return ;
}
Popular Cows---poj2186(缩点,强联通)的更多相关文章
- (连通图 缩点 强联通分支)Popular Cows -- poj --2186
http://poj.org/problem?id=2186 Description Every cow's dream is to become the most popular cow in th ...
- POJ 2168 Popular cows [Tarjan 缩点]
...
- USACO 2003 Fall Orange Popular Cows /// tarjan缩点 oj22833
题目大意: n头牛,m个崇拜关系,并且崇拜具有传递性 如果a崇拜b,b崇拜c,则a崇拜c 求最后有几头牛被所有牛崇拜 强连通分量内任意两点都能互达 所以只要强联通分量内有一点是 那么其它点也都会是 按 ...
- [poj2186]Popular Cows(targin缩点)
题意:求其他所有牛都认为其牛的牛的个数. 解题关键:targin算法模板题,缩点形成一棵树,并不一定是棵树,可能含有多个入度为0的点,寻找出度为0的点(缩点之后的点)的个数,如果个数大于0,则无解,否 ...
- 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, ...
- POJ 2186 Popular Cows tarjan缩点算法
题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...
- 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 <= ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- 【Tarjan缩点】POJ2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 35644 Accepted: 14532 De ...
随机推荐
- CentOS命令top下你不一定懂的cpu显示信息
在使用top命令的时候会看到这么一行: 里面的各个值分别是什么意思呢? 今天被问到这个问题,发现答的不是很清楚.果然啊,天天用最多的top命令都还没摸透...惭愧...于是就查了些资料: 官方解释 C ...
- 【Deeplearning】关注书目
Deep Learning: A Practitioner's Approach http://www.amazon.com/Deep-Learning-Practitioners-Adam-Gibs ...
- jquery 中的回调函数,回调函数(callback)是什么?
知乎上果然大牛比较多 大神解释如下: 你到一个商店买东西,刚好你要的东西没有货,于是你在店员那里留下了你的电话,过了几天店里有货了,店员就打了你的电话,然后你接到电话后就到店里去取了货.在这个例子里, ...
- 用cocos2d 2.1制作一个过河小游戏(4): 游戏主逻辑BaseLayer设计
前段时间一直在忙.没有时间更新博客.今天还是抽点时间把最后一小部分游戏的实现放上来吧. BaseLayer.h: #import <GameKit/GameKit.h> #import & ...
- 超全面的JavaWeb笔记day18<事务&连接池&DBUtils>
1.事务 ACID 原子性 一致性 隔离性 持久性 mysql中开启和关闭事务 开启事务:START TRANSACTION 结束事务 提交事务:COMMIT 回滚事务:ROLLBACK JDBC中开 ...
- Python 插件安装
0.下载Python包文件后,解压缩: 1.cd 到 插件解压缩目录,里面有setup.py的文件: 2.定位到当前目录: 3.执行:python setup.py install; 4.结束安装. ...
- 原来javascript 自带 encodeURI 和 decodeURI文 方法了
今天百度一下才知道js 自带 encodeURI 和 decodeURI 方法了,之前还找了其他代码来处理(笑哭了.jpg <script type="text/javascript& ...
- linux命令在文件中根据命令查找
find . -type f -name "*.tmp" | xargs grep -ri "2016-08-30 04:00:00|2016-08-30 05:00:0 ...
- linux命令之用户和用户组
知识点: 1.-rwx--x--x (711) 只有所有者才有读,写,执行的权限,组群和其他人只有执行的权限 2.将root用户添加到supergroup用户组 groupadd supergroup ...
- ch5-处理数据,抽取-整理-推导
场景:教练kelly有4个选手James\Sarah\Julie\Mikey,他们每跑600米,教练就会计时并把时间记录在计算机的一个文件中,总共4个文件:James.txt\Sarah.txt\Ju ...