Popular Cows(codevs 2186)
题意:
有N(N<=10000)头牛,每头牛都想成为most poluler的牛,给出M(M<=50000)个关系,如(1,2)代表1欢迎2,关系可以传递,但是不可以相互,即1欢迎2不代表2欢迎1,但是如果2也欢迎3那么1也欢迎3.给出N,M和M个欢迎关系,求被所有牛都欢迎的牛的数量。
/*
tarjan缩点后,统计每个所点的出度,当有且只有一个出度为0的缩点是,
答案即为这个缩点所包含的点的数量,否则答案为0
*/
#include<cstdio>
#include<iostream>
#include<cstring>
#include<vector>
#include<stack>
#define M 10010
using namespace std;
int low[M],num[M],belong[M],vis[M],instack[M],in[M],out[M],sum[M],indexx,cnt,n,m;
vector<int> grap[M];
stack<int> s;
void tarjan(int v)
{
low[v]=num[v]=++indexx;
s.push(v);
vis[v]=;
instack[v]=;
for(int i=;i<grap[v].size();i++)
{
int w=grap[v][i];
if(!vis[w])
{
tarjan(w);
low[v]=min(low[v],low[w]);
}
else if(instack[w])
low[v]=min(low[v],num[w]);
}
int u;
if(low[v]==num[v])
{
++cnt;
do
{
u=s.top();
s.pop();
belong[u]=cnt;
sum[cnt]++;
instack[u]=;
}
while(u!=v);
}
}
void work()
{
for(int i=;i<=m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
grap[y].push_back(x);
}
for(int i=;i<=n;i++)
if(!vis[i])
tarjan(i);
for(int i=;i<=n;i++)
for(int j=;j<grap[i].size();j++)
if(belong[i]!=belong[grap[i][j]])
{
in[belong[i]]++;
out[belong[grap[i][j]]]++;
}
int tot=,p;
for(int i=;i<=cnt;i++)
if(!out[i])
{
tot++;
p=i;
}
if(tot==)printf("%d\n",sum[p]);
else printf("0\n");
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
work();
memset(low,,sizeof(low));
memset(num,,sizeof(num));
memset(belong,,sizeof(belong));
memset(vis,,sizeof(vis));
memset(instack,,sizeof(instack));
memset(in,,sizeof(in));
memset(out,,sizeof(out));
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++)grap[i].clear();
while(!s.empty())s.pop();
indexx=;
cnt=;
}
}
Popular Cows(codevs 2186)的更多相关文章
- POJ 2186 Popular Cows(强连通)
Popular Cows Time Limit: 2000MS Memo ...
- Popular Cows(POJ 2186)
原题如下: Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40746 Accepted: 16 ...
- (连通图 缩点 强联通分支)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 2186 Popular Cows(Tarjan)
http://poj.org/problem?id=2186 题意 :给你n头牛,m对关系,每对关系由两个编号组成,u和v代表着u认为v是受欢迎的,如果1认为2是受欢迎的,2认为3是受欢迎的,那1认为 ...
- POJ 2186.Popular Cows (强连通)
强连通缩点,统计入度为1的缩点后的点的个数 个数1的话输出这个强连通分量的点的数量 否则输出0: code /* Kosaraju算法,无向图的强连通分量,时间复杂度O(n+m) 思路: 按照图G的深 ...
- Popular Cows POJ - 2186(强连通分量)
Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...
- poj2186 Popular Cows(强连通)
崇拜有传递性.求所有牛都崇拜的牛tarjan算法求强连通. 如果不连通就不存在.如果联通,缩点后唯一一个出度为零的点就是答案,有多个则不存在. #include <vector> #inc ...
- 【2186】Popular Cows(强连通分支及其缩点)
id=2186">[2186]Popular Cows(强联通分支及其缩点) Popular Cows Time Limit: 2000MS Memory Limit: 65536 ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
随机推荐
- c链表实现遇到的错误
想完成一个链表发现有错误,代码如下: //http://ac.jobdu.com/problem.php?pid=1511 //֮ǰÓÃlistʵÏֵ쬽ñÌìÊÔÒ»ÏÂÓÃstruct ...
- python运行报错:urllib2.URLError: <urlopen error [Errno 10061] >
Traceback (most recent call last): File "F:\adt-bundle-windows-x86_64-20140702\eclipse\workspac ...
- 轻量级应用开发之(04)UIScrollView-1
本文是我在学习OC中的一些经验总结,在学习中总结了常用的Mac技巧,欢迎群友对本文提出意见,如有问题请联系我. 一 什么是UIScrollView 1)移动设备的屏幕大小是极其有限的,因此直接展示在用 ...
- ECSHOP验证码背景图修改教程
ECSHOP验证码背景图修改教程 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-11-18 ECSHOP验证码背景图修改教程: ECSHOP前后台的某些地 ...
- Protocol Buffer技术详解(语言规范)
Protocol Buffer技术详解(语言规范) 该系列Blog的内容主体主要源自于Protocol Buffer的官方文档,而代码示例则抽取于当前正在开发的一个公司内部项目的Demo.这样做的目的 ...
- 锋利的jQuery-5--下拉框的应用(看写法)
如图,可以通过中间的按钮将左边选中的选项添加到右边,或者全部添加到右边,也可通过双击添加.反之也可以. 左边选中加到右边: $("#add").click(function(){ ...
- Repository
namespace MyRepository.Domain.Infrastructure { public class Repository<TEntity> : IRepository& ...
- 利用ps橡皮擦工具快速抠图
原图 最终效果 1.打开图片,ctrl+j得到图层1,点击红圈处,创建图层2,放于图层1与背景层之间,填充白色作为检查效果和新的背景. 2.按图示给出的参数,用背景橡皮擦在图层1里擦吧,注意擦的时候尽 ...
- struts+spring action应配置为scope="prototype"
truts+spring action应配置为scope="prototype" <bean id="personAction" scope=" ...
- [工具]Mac平台开发几个网络抓包工具(sniffer)
Cocoa Packet Analyzer http://www.tastycocoabytes.com/cpa/ Cocoa Packet Analyzer is a native Mac OS X ...