POJ 2186:Popular Cows Tarjan模板题

时间:2023-01-09 14:15:02
Popular Cows
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 25945   Accepted: 10612

Description

Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10,000) cows, you are given up to M (1 <= M <= 50,000) ordered pairs of the form (A, B) that tell you that cow A thinks that cow B is popular. Since popularity is transitive,
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

* Line 1: Two space-separated integers, N and M 



* Lines 2..1+M: Two space-separated numbers A and B, meaning that A thinks B is popular. 

Output

* Line 1: A single integer that is the number of cows who are considered popular by every other cow. 

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

Hint

Cow 3 is the only cow of high popularity.

题意是给你几头牛(......),然后给你一个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模板题的更多相关文章

  1. POJ - 2186  Popular Cows tarjain模板题

    http://poj.org/problem?id=2186 首先求出所有的强连通分量,分好块.然后对于每一个强连通分量,都标记下他们的出度.那么只有出度是0 的块才有可能是答案,为什么呢?因为既然你 ...

  2. 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 ...

  3. &lbrack;poj 2186&rsqb;Popular Cows&lbrack;Tarjan强连通分量&rsqb;

    题意: 有一群牛, a会认为b很帅, 且这种认为是传递的. 问有多少头牛被其他所有牛认为很帅~ 思路: 关键就是分析出缩点之后的有向树只能有一个叶子节点(出度为0). 做法就是Tarjan之后缩点统计 ...

  4. POJ 2186 Popular Cows tarjan缩点算法

    题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...

  5. 强连通分量分解 Kosaraju算法 &lpar;poj 2186 Popular Cows&rpar;

    poj 2186 Popular Cows 题意: 有N头牛, 给出M对关系, 如(1,2)代表1欢迎2, 关系是单向的且能够传递, 即1欢迎2不代表2欢迎1, 可是假设2也欢迎3那么1也欢迎3. 求 ...

  6. tarjan缩点练习 洛谷P3387 【模板】缩点&plus;poj 2186 Popular Cows

    缩点练习 洛谷 P3387 [模板]缩点 缩点 解题思路: 都说是模板了...先缩点把有环图转换成DAG 然后拓扑排序即可 #include <bits/stdc++.h> using n ...

  7. poj 2186 Popular Cows 【强连通分量Tarjan算法 &plus; 树问题】

    题目地址:http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  8. poj 2186 Popular Cows &lpar;强连通分量&plus;缩点&rpar;

    http://poj.org/problem?id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissi ...

  9. POJ 2186 Popular Cows &lpar;强联通&rpar;

    id=2186">http://poj.org/problem? id=2186 Popular Cows Time Limit: 2000MS   Memory Limit: 655 ...

随机推荐

  1. jQuery&sol;js 正则收集(邮件验证、)

    var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; //验证邮箱的正则表达式if( ...

  2. 0316-复利计算器3&period;0---release

    目录       一.项目简介       二.Github链接推送       三.客户需求       四.需求分析       五.项目设计       六.完成效果       七.JUnit ...

  3. nginx 多域名配置 (nginx如何绑定多个域名)

         nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可又写在一个总的配置文件里. 一.每个域名一个 ...

  4. 转型函数 Boolean&lpar;&rpar;

    布尔值有2种可能的值true和false;但 ECMAScript中所有类型的值都有与这两个 Boolean 值 等价的值.要将一个值转换为其对应的 Boolean 值,可以调用转型函数 Boolea ...

  5. &lbrack;JavaScript&rsqb; 判断键盘同时按某些键时执行操作。

    前言:之前知乎上看到过一个介绍国外炫酷网站的,其中一个敏感网站用同时按住"q.a.p.l" 才能观看视频 放手则立即强制停止 (手动斜眼).这个功能的实际用处,我认为是可以在做一些 ...

  6. logstash 各种时间转换

    <pre name="code" class="html">日期格式转换: /***** nginx 访问日志 [elk@zjtest7-front ...

  7. Ubuntu14&period;0&period;4 64位 ADT 连接手机调试问题

    1:使用 lsusb 命令查看USB 设备 y@y:~$ lsusbBus 001 Device 002: ID 8087:8000 Intel Corp. Bus 001 Device 001: I ...

  8. &lbrack;DB&rsqb; - Mysql创建定时任务

    mysql支持定时任务的创建,要求mysql服务器开始定时任务调度. 1. 查看是否开启定时任务执行 SHOW VARIABLES LIKE 'event_scheduler'; // OFF表示没有 ...

  9. Django组件之Form表单

    一.Django中的Form表单介绍 我们之前在HTML页面中利用form表单向后端提交数据时,都会写一些获取用户输入的标签并且用form标签把它们包起来. 与此同时我们在好多场景下都需要对用户的输入 ...

  10. scrapy meta信息丢失

    在做58同城爬二手房时,由于房产详情页内对价格进行了转码处理,所以只能从获取详情页url时同时获取该url对应房产的价格,并通过meta传递给下回调函数 现在问题是,在回调函数中找不到原函数meta信 ...