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 ...
随机推荐
- 5G/NR 频带详解
原文链接:http://www.sharetechnote.com/html/5G/5G_FR_Bandwidth.html 在NR中,3GPP中规定了大约两个大的频率范围.一个是我们通常所说的(su ...
- idea中跑mapreduce报错, PATH设置错误
问题如题,报错: [root@node01 servers]# hadoop jar loginVisit.jar cn.itcast.loginVisit.step1.Step1Main19/07/ ...
- jenkins#安装jenkins
1. 访问官网下载地址https://jenkins.io/zh/download/ 2. 选择自己的平台,然后按照文档进行操作: 主要按照文档来,下面是我按照文档按照的一个记录 #访问 https: ...
- Unable to execute dex:Multuple dex files define 解决方法
困扰我两天的问题终于解决了,在网上查的方法无非有三种 一. Eclipse->Project->去掉Build Automatically->Clear ->Build Pro ...
- 008.Delphi插件之QPlugins,服务的两种调用方法
这个QPlugins自带的DEMO,大概的意思就是,创建2个服务类,程序启动的时候注册这2个服务类.点击不同的按钮,使用不同的方法来调用这个服务. 效果界面如下 unit Frm_Main; inte ...
- Python 常用的标准库以及第三方库有哪些?
作者:史豹链接:https://www.zhihu.com/question/20501628/answer/223340838来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明 ...
- 在 Delphi 中使用微软全文翻译的小例子
使用帮助 需要先去申请一个 AppID: http://www.bing.com/toolbox/bingdeveloper/使用帮助在: http://msdn.microsoft.com/en-u ...
- python用于web题里写解密脚本
题源自bugku里的WEB3 选择选项让他停止,F12后出现如下代码,一看数字就知道是ASC: 复制出来,写pyhton脚本如下,在编译器里跑一下 s='KEY{J2sa42ahJK-HS11III} ...
- Django 项目搭建
django(mvt结构) 虚拟环境 创建虚拟环境 mkvirtualenv django_py3 -p python3 切换虚拟环境 wokeon 虚拟环境名称 删除虚拟环境 rmvirtualen ...
- HYSBZ - 1588 营业额统计 (伸展树)
题意:营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每天的营业额.分析营 ...