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 ...
随机推荐
- Bootstrap教程:[4]栅格系统详解
http://jingyan.baidu.com/article/6f2f55a1852aa1b5b83e6c5a.html 们都知道bootstrap3.0使用了四种栅格选项来形成栅格系统,这四种选 ...
- ASP.NET MVC ActionFilterAttribute的执行顺序
http://diaosbook.com/Post/2014/6/3/execution-order-of-actionfilter-aspnet-mvc ASP.NET MVC里面我们要自定义Act ...
- WPF中嵌入Office编辑器(支持Word、Excel、PPT、Visio等)
现在有一个项目,需要使用wpf做一个简单的客户端,用来生成word.excel.ppt.visio等文档,这就需要能够在wpf中嵌入office的编辑器,并对office文档进行编辑. 在网上搜索了一 ...
- VS2013配置Caffe卷积神经网络工具(64位Windows 7)——准备依赖库
VS2013配置Caffe卷积神经网络工具(64位Windows 7)--准备依赖库 2014年4月的时候自己在公司就将Caffe移植到Windows系统了,今年自己换了台电脑,想在家里也随便跑跑,本 ...
- git工作量统计
#!/bin/bash function count() { local insert=0 local delete=0 while read line ;do current=`echo $line ...
- sql 树 递归
sql 树 递归 with SubQuery(No,Name,ParentNo) as ( ' union all select A.No,A.Name,A.ParentNo from [Port_D ...
- [LeetCode] Copy List with Random Pointe
题目的关键是要让新链表和原有链表发送关联,可以通过这种关联来设置新链表的random pointer 思路:将新链表的元素插入到原有链表元素的后面,如下图所示,就可以根据原有链表的radom-> ...
- web图片使用
1. jpg.png.gif 适用场景 jpg 色彩丰富.大的图片例如 写实的图像,商品图片,人像,实物素材的广告banner等 png 色彩较少,有透明,或 具备较大亮度差异及强烈对比的图像,例如 ...
- motto5
No matter what others say,I won't forsake my priciples.
- MapReduce使用JobControl管理实例
import java.io.IOException; import java.util.StringTokenizer; import org.apache.hadoop.fs.Path; impo ...