POJ2186
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
题意:10000头奶牛,50000对仰慕关系,且仰慕关系可以传递,问所有牛都仰慕的对象有多少?
————————————————————————————————————————————————
强连通分量中相互仰慕,所点后各点形成有向无环图,而其中如果有一个点(强连通分量)出度为0,那么这个强连通分量包含的点数就是答案;若果有多个点出度为0,则没有所有牛都仰慕的牛,答案0。
————————————————————————————————————————————————
//poj2186 popular cows
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack> using namespace std;
const int maxn=;
const int maxm=;
int n,m;
struct edge
{
int u,v,next;
}e[maxm],ee[maxm];
int head[maxn],js,headd[maxn],jss;
int dfsn[maxn],low[maxn],belong[maxn];
bool ins[maxn];
int sshu;
int chudu[maxn],ss[maxn];
stack<int>st;
int visx;
void init()
{
memset(head,,sizeof(head));
memset(e,,sizeof(e));
js=;
memset(headd,,sizeof(headd));
memset(ee,,sizeof(ee));
jss=;
memset(dfsn,-,sizeof(dfsn));
memset(low,-,sizeof(low));
sshu=;
memset(ss,,sizeof(ss));
while(!st.empty())st.pop();
visx=;
memset(ins,,sizeof(ins));
memset(chudu,,sizeof(chudu));
memset(belong,,sizeof(belong));
}
void addage(int u,int v,edge e[],int head[],int &js)
{
e[++js].u=u;e[js].v=v;
e[js].next=head[u];head[u]=js;
} void tarjan(int u)
{
dfsn[u]=low[u]=++visx;
st.push(u);
ins[u]=;
for(int i=head[u];i;i=e[i].next)
{
int v=e[i].v;
if(dfsn[v]==-)
{
tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(ins[v] && low[u]>dfsn[v])low[u]=dfsn[v];
}
if(low[u]==dfsn[u])
{
sshu++;
int tp;
do
{
tp=st.top();
st.pop();
ins[tp]=;
ss[sshu]++;
belong[tp]=sshu;
}while(u!=tp);
}
}
int main()
{
while(scanf("%d%d",&n,&m)==)
{
init();
for(int i=,u,v;i<m;i++)
{
scanf("%d%d",&u,&v);
addage(u,v,e,head,js);
}
for(int i=;i<=n;i++)
if(dfsn[i]==-)tarjan(i);
for(int i=;i<=m;i++)
{
int u=e[i].u,v=e[i].v;
if(belong[u]!=belong[v])
{
addage(belong[u],belong[v],ee,headd,jss);
chudu[belong[u]]++;
}
}
int count=,mn;
for(int i=;i<=sshu;i++)
if(chudu[i]==)count++,mn=i;
if(count==)printf("%d\n",ss[mn]);
else printf("0\n");
}
return ;
}
POJ2186的更多相关文章
- 【poj2186】 Popular Cows
http://poj.org/problem?id=2186 (题目链接) 题意 给出一个n个点m条边的有向图,求其中没有出度强连通分量所包含的点有几个 Solution 其实这道题的题解已经在“题意 ...
- 强连通分量tarjan缩点——POJ2186 Popular Cows
这里的Tarjan是基于DFS,用于求有向图的强联通分量. 运用了一个点dfn时间戳和low的关系巧妙地判断出一个强联通分量,从而实现一次DFS即可求出所有的强联通分量. §有向图中, u可达v不一定 ...
- 不裸缩点》。。。POJ2186受欢迎的牛
不裸缩点>...POJ2186受欢迎的牛 :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: ...
- 最近切的两题SCC的tarjan POJ1236 POJ2186
两题都是水题,1236第一问求缩点后入度为0的点数,第二问即至少添加多少条边使全图强连通,属于经典做法,具体可以看白书 POJ2186即求缩点后出度为0的那个唯一的点所包含的点数(即SCC里有多少点) ...
- 强连通分量+poj2186
强连通分量:两个点能够互相连通. 算法分解:第一步.正向dfs全部顶点,并后序遍历 第二步,将边反向,从最大边dfs,构成强连通分量 标号最大的节点属于DAG头部,cmp存一个强连通分量的拓扑序. p ...
- 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows
P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...
- POJ2186 Popular Cows [强连通分量|缩点]
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31241 Accepted: 12691 De ...
- poj2186 强连通缩点
Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29773 Accepted: 12080 De ...
- POJ2186 Popular Cows
Description Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= ...
随机推荐
- 使用nginx部署Yii 2.0\yii-advanced-app-2.0.6
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #erro ...
- openlayers中实现自定冒泡的效果
自定义的Openlayers.Popup.FreshCloud继承自Openlayers.Popup.Framed,实现了比较简洁的冒泡效果,详细代码如下 /** * Class: OpenLayer ...
- cordova + ionic 使用中碰到的一些问题
cordova + ionic 使用中碰到的一些问题 No Content-Security-Policy meta tag found. Please add one when using ...
- 数据库使用--MySQL: InnoDB 还是 MyISAM?
MyISAM存储引擎 MyISAM是 默认存储引擎.它基于更老的ISAM代码,但有很多有用的扩展.MyISAM存储引擎的一些特征: · 所有数据值先存储低字节.这使得数据机和操作系统分离.二 ...
- Android自动化学习笔记之MonkeyRunner:用Eclipse执行MonkeyRunner脚本
Info: 初步学习,难免会有疏漏,以后我会不断修改补全,直到完美.转载请注明出处,谢谢. 2014-10-11: 初版 2014-10-16: 完善 ------------------------ ...
- flex-布局,轻松制作移动端网页
Flex 布局教程 网页布局(layout)是CSS的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display属性 + position属性 + float属性.它对于那些特殊布局非常不 ...
- According to TLD or attribute directive in tag file, attribute test does not accept any expressions
HTTP Status 500 - /WEB-INF/views/emp/list.jsp (line: 30, column: 4) According to TLD or attribute di ...
- IO操作概念。同步、异步、阻塞、非阻塞
“一个IO操作其实分成了两个步骤:发起IO请求和实际的IO操作. 同步IO和异步IO的区别就在于第二个步骤是否阻塞,如果实际的IO读写阻塞请求进程,那么就是同步IO. 阻塞IO和非阻塞IO的区别在于第 ...
- DB2操作命令
本文详细汇总了DB2的常用操作命令,分享给大家.对于使用db2的朋友可以参考下. DB2数据库管理客户端从v9.7版本之后就不再带有控制中心了,而是使用 Data Studio Client.安装 D ...
- 通过SQL Server 2008数据库复制实现数据库同步备份
SQL Server 2008数据库复制是通过发布/订阅的机制进行多台服务器之间的数据同步,我们把它用于数据库的同步备份.这里的同步备份指的是备份服务器与主服务器进行 实时数据同步,正常情况下只使用主 ...