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 ...
随机推荐
- Python测试进阶——(4)Python程序监控、存储、分析并可视化CPU和内存利用率
monitor190617.py 监控cpu和内存利用率信息,组织成json格式,并写入到 record.txt 文件中: import psutil import time import json ...
- python isdigit()函数
isdigit() 函数是作用于字符串的,用来判断字符串是否全部由数字组成. x = '123456' y = 'iloveyou123' print(x.isdigit(),y.isdigit()) ...
- Java基础学习总结(一)——Java开发学习介绍
Java平台: 1.J2SE java开发平台标准版 2.J2EE java开发费平台企业版 Java程序需要在虚拟机上才可以运行,换言之只要有虚拟机的系统都可以运行java程序.不同的系统上要安装对 ...
- 学生选课数据库MySQL语句练习题45道
1. 查询Student表中的所有记录的Sname.Ssex和Class列. select Sname,Ssex,Class from Student;2. 查询教师所有的单位即不重复的Depart列 ...
- C# 控件缩写规范
标准控件缩写规范 类 型 前 缀 示 例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calend ...
- CSS隐藏商务通等内容
CSS隐藏商务通等内容<style>#qiao-wrap{display:none !important;} </style>
- S7-200 smart 网线下载与调试配置
打开 step microwin 7 smart 软件. 连接PLC 打开 通讯模块 我把电脑的改成了如下 我编写的简单的程序 通过外部一个开关 实现输出的一个 IO 的接通与断开 下载完成程序以后 ...
- NASA航天技术演示实验集
为了支持太空探索NASA开起了一系列先进太空技术演示实验. 具体的实验实验地址在:https://www.nasa.gov/mission_pages/tdm/main/overview.html 通 ...
- GNS3 模拟icmp分片不可达
R1 : conf t int f0/0 no shutdown ip add 192.168.1.1 255.255.255.0 no ip routing end R2 f0/0: conf t ...
- 使用CORDIC算法求解角度正余弦及Verilog实现
本文是用于记录在了解和学习CORDIC算法期间的收获,以供日后自己及他人参考:并且附上了使用Verilog实现CORDIC算法求解角度的正弦和余弦的代码.简单的testbench测试代码.以及在Mod ...