POJ 2186:Popular Cows Tarjan模板题
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 25945 | Accepted: 10612 |
Description
if A thinks B is popular and B thinks C is popular, then A will also think that C is
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
题意是给你几头牛(......),然后给你一个A牛喜欢B牛 这类的关系,这种喜欢的关系还可以传递,比如A喜欢B,B喜欢C,那就A也喜欢C了。问有多少头牛被所有牛喜欢。
Tarjan模板题,话说看懂了Tarjan之后还是很爽的。
用Tarjan算法缩点之后,要出度为0的点只有一个才满足要求,想象一下,要是两个的话,就不会有牛被所有其他牛喜欢的。
然后看出度为0的点内又有多少个点即可。
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <queue>
#pragma warning(disable:4996)
using namespace std; int head[10005],LOW[10005],DFN[10005],instack[10005],Stack[10005],Belong[10005],out[10005];
int n,m,edge_num,Dindex,Stop,Bcnt; struct edge{
int to;
int next;
}Edge[50005]; void init()
{
edge_num=0;
Stop=Bcnt=Dindex=0; memset(Edge,-1,sizeof(Edge));
memset(head,-1,sizeof(head));
memset(LOW,0,sizeof(LOW));
memset(DFN,0,sizeof(DFN));
memset(instack,0,sizeof(instack));
memset(Stack,0,sizeof(Stack));
memset(Belong,0,sizeof(Belong));
memset(out,0,sizeof(out));
} void addedge(int u,int v)
{
Edge[edge_num].to=v;
Edge[edge_num].next=head[u];
head[u]=edge_num;
edge_num++;
} void tarjan(int i)
{
int j;
DFN[i]=LOW[i]=++Dindex;
instack[i]=true;
Stack[++Stop]=i; for(j=head[i];j!=-1;j=Edge[j].next)
{
int v=Edge[j].to;
if(DFN[v]==0)
{
tarjan(v);
LOW[i]=min(LOW[i],LOW[v]);
}
else if(instack[v]==1)
{
LOW[i]=min(LOW[i],DFN[v]);
}
} if(DFN[i]==LOW[i])
{
Bcnt++;
do
{
j=Stack[Stop--];
instack[j]=false;
Belong[j]=Bcnt;
}
while(j!=i);
}
} void solve()
{
int i,j,u,v;
init(); cin>>n>>m;
for(i=1;i<=m;i++)
{
cin>>u>>v;
addedge(u,v);
} for(i=1;i<=n;i++)
{
if(!DFN[i])
tarjan(i);
}
for(i=1;i<=n;i++)
{
for(j=head[i];j!=-1;j=Edge[j].next)
{
if(Belong[i]!=Belong[Edge[j].to])
out[Belong[i]]++;//计算缩点后每个点的出度
}
}
int out_num=0,import;
for(i=1;i<=Bcnt;i++)
{
if(!out[i])
{
out_num++;
import=i;
}
}
int temp=0;
if(out_num==1)
{
for(i=1;i<=n;i++)
{
if(Belong[i]==import)
{
temp++;
}
}
cout<<temp<<endl;
}
else
{
cout<<0<<endl;
} } int main()
{
//freopen("i.txt","r",stdin);
//freopen("o.txt","w",stdout); solve();
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 2186:Popular Cows Tarjan模板题的更多相关文章
- POJ - 2186 Popular Cows tarjain模板题
http://poj.org/problem?id=2186 首先求出所有的强连通分量,分好块.然后对于每一个强连通分量,都标记下他们的出度.那么只有出度是0 的块才有可能是答案,为什么呢?因为既然你 ...
- poj 2186 Popular Cows tarjan
Popular Cows Description Every cow's dream is to become the most popular cow in the herd. In a herd ...
- [poj 2186]Popular Cows[Tarjan强连通分量]
题意: 有一群牛, a会认为b很帅, 且这种认为是传递的. 问有多少头牛被其他所有牛认为很帅~ 思路: 关键就是分析出缩点之后的有向树只能有一个叶子节点(出度为0). 做法就是Tarjan之后缩点统计 ...
- POJ 2186 Popular Cows tarjan缩点算法
题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...
- 强连通分量分解 Kosaraju算法 (poj 2186 Popular Cows)
poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...
- tarjan缩点练习 洛谷P3387 【模板】缩点+poj 2186 Popular Cows
缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...
- poj 2186 Popular Cows 【强连通分量Tarjan算法 + 树问题】
题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- poj 2186 Popular Cows (强连通分量+缩点)
http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissi ...
- POJ 2186 Popular Cows (强联通)
id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS Memory Limit: 655 ...
随机推荐
- Windows 10长脸了!
Windows 10一直毁誉参半,但是在微软的持续升级完善之下,同时随着时间的流逝,已经顺利成为全球第一大桌面操作系统,并开始逐渐甩开Windows 7,全球设备安装量已经达到约8亿部. 根据最新的S ...
- linux中df和du查看磁盘大小不一致解决方法
挂了一块50G到/data目录下# df -h Filesystem Size Used Avail Use% Mounted on /dev/xvdb1 50G 46G 1.2G 98% /dat ...
- Element 以二进制的形式 自定义上传图片
一,只有在上传文件之前的钩子函数中才可以获得最初的文件(文件本身的二进制形式),用以以上传服务器. 还需要使用formdata来承载数据,便于接收 <template> <div ...
- MongoDB 监控指标
MongoDB uptime 启动时长 asserts.user 用户的断言数量 asserts.warning 警告的断言数量 connections.current 当前的连接数 大于 650co ...
- Linux分发版本的试用及选择工具
https://www.forbes.com/sites/jasonevangelho/2019/06/15/how-to-test-drive-200-linux-distributions-wit ...
- 怎样设置使IntelliJ IDEA智能提示忽略大小写?
打开设置(CTRL+ALT+S)打开editor,找到“Code Completion”->点击Match case前面的框不勾选即可.如下图:
- java中的几种单例模式
目前比较常见的有4种(DCL为懒汉模式的线程安全版本). 单例模式的实现一般需要满足以下条件: 1.构造方法私有化,实例属性私有化. 2.必须仅在类的内部完成实例的初始化过程. 3.提供公共静态方法, ...
- tomcat启动报错The JRE could not be found.Edit the server and change the JRE location
解决: 在Windows->Preferences->Server->Runtime Environments 选择Tomcat->Edit,在jre中选择相应的jdk版本,完 ...
- (二)Spring初步搭建、IOC创建对象
环境准备: 见java环境搭建 新建maven项目,同时搭好项目结构,新建相应的包 Spring的初步搭建 1.导入jar包,Spring版本为5.1.10,同时导入junit包 <depend ...
- 干货分享:深度解析Supplement Essay写作
今天Hotessay小编给同学们介绍下附加文书的创作思路.因为附加文书基本上都是短essay,所以简洁才是硬道理! 通常,我们可以把美国大学的附加文书分为以下几类: 1.Tell us about y ...