Popular Cows
传送门(poj):http://poj.org/problem?id=2186
(bzoj):http://www.lydsy.com/JudgeOnline/problem.php?id=1051
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 33482 | Accepted: 13638 |
Description
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
* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular.
Output
Sample Input
3 3
1 2
2 1
2 3
Sample Output
1
Hint
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;
#define N 10009
struct Edge
{
int x,y,next;
Edge(int x=,int y=,int next=):
x(x),y(y),next(next){}
}edge[N*];
int sumedge,n,m,x,y,tim,top,sumclr,sumedge2,js,ans;
int head[N],dfn[N],low[N],Stack[N],color[N],cnt[N],out[N],head2[N];
bool vis[N],instack[N];
void ins(int x,int y)
{
edge[++sumedge]=Edge(x,y,head[x]);
head[x]=sumedge;
}
void ins2(int x,int y)
{
edge[++sumedge2]=Edge(x,y,head2[x]);
head2[x]=sumedge2;
}
map<int,bool>Map[N];
void tarjan(int x)
{
dfn[x]=low[x]=++tim;
vis[x]=;instack[x]=;Stack[++top]=x;
for(int u=head[x];u;u=edge[u].next)
if(instack[edge[u].y])
low[x]=min(low[x],dfn[edge[u].y]);
else
if(!vis[edge[u].y])
{
tarjan(edge[u].y);
low[x]=min(low[x],low[edge[u].y]);
}
else
{
}
if(dfn[x]==low[x])
{
sumclr++;
color[x]=sumclr;
cnt[sumclr]++;
while(Stack[top]!=x)
{
color[Stack[top]]=sumclr;//染色
instack[Stack[top]]=;
top--;
cnt[sumclr]++;//记录这个连通块里牛的个数
}
instack[x]=;
top--;
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
ins(x,y);
}
for(int i=;i<=n;i++)
{
if(!vis[i])tarjan(i);
top=;
}
for(int i=;i<=n;i++)
for(int u=head[i];u;u=edge[u].next)
if(color[i]!=color[edge[u].y])
if(Map[color[i]].find(color[edge[u].y])==Map[color[i]].end()) //缩点
{
Map[color[i]][color[edge[u].y]]=true;
ins2(color[i],color[edge[u].y]);
out[color[i]]++;
}
int cnnt=;
for(int i=;i<=sumclr;i++)
{
if(out[i]==)//度为0意味着所有牛都欢迎他
{
js++;
ans=i;
cnnt+=cnt[i];//加上这个连通块里牛的个数
}
}
if(js>||js==)puts("");//如果有大于1个的度为0的点,说明这个图不是连通的。
else
printf("%d",cnnt);
return ;
}
Popular Cows的更多相关文章
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- poj 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29908 Accepted: 12131 De ...
- [强连通分量] POJ 2186 Popular Cows
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31815 Accepted: 12927 De ...
- POJ 2186 Popular Cows(强连通)
Popular Cows Time Limit: 2000MS Memo ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- poj 2186 Popular Cows【tarjan求scc个数&&缩点】【求一个图中可以到达其余所有任意点的点的个数】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27698 Accepted: 11148 De ...
- POJ2186 Popular Cows 【强连通分量】+【Kosaraju】+【Tarjan】+【Garbow】
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23445 Accepted: 9605 Des ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
随机推荐
- Manager模块 队列 管道 进程池
Manager模块 作用: 多进程共享变量. Manager的字典类型: 如果value是简单类型,比如int,可以直接赋值给共享变量,并可以后续直接修改 如果value是复杂类型 ,比如list, ...
- Earth Hour(最短路)
Earth Hour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 125536/65536 K (Java/Others)Total ...
- K-Piggy-Bank
Piggy-Bank Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 6.2.3-Bean的加载处理
在AbstractBeanFactory中doGetBean方法中始终调用了getObjectForBeanInstance方法,这个方法是对参数进行过滤; @SuppressWarnings(&qu ...
- 浅谈命令查询职责分离(CQRS)模式---转载
在常用的三层架构中,通常都是通过数据访问层来修改或者查询数据,一般修改和查询使用的是相同的实体.在一些业务逻辑简单的系统中可能没有什么问题,但是随着系统逻辑变得复杂,用户增多,这种设计就会出现一些性能 ...
- unity里standard pbr(一)
关注forwardbase下的 standard.shader #pragma vertex vertBase #pragma fragment fragBase #include "Uni ...
- k近邻算法(k-nearest neighbor,k-NN)
kNN是一种基本分类与回归方法.k-NN的输入为实例的特征向量,对应于特征空间中的点:输出为实例的类别,可以取多类.k近邻实际上利用训练数据集对特征向量空间进行划分,并作为其分类的"模型&q ...
- linux 中 用户管理 (composer 时不能root 遇到)
linux 是支持多用户的,可以同时多个用户在线操作,这点与 Windows 不同. 在我们项目组 操作linux 服务器时,可进行多用户管理,并赋予不同权限,下面是我学习并用的比较频繁的命令: 1. ...
- 很详细、很移动的Linux makefile 教程
近期在学习Linux下的C编程,买了一本叫<Linux环境下的C编程指南>读到makefile就越看越迷糊,可能是我的理解能不行. 于是google到了以下这篇文章.通俗易懂.然后把它贴出 ...
- mysql 数据库备份方案及策略
由于mysql存在多种数据库备份方式,而且各有利弊,对于我们初学者来说,选择合适的备份方式确实有些困难.个人觉得,首先要基于公司的需求,考虑能够容忍丢失多少数据.花多少人力时间成本等,这是我们制定备份 ...