1051: [HAOI2006]受欢迎的牛

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://www.lydsy.com/JudgeOnline/problem.php?id=1051

Description

每一头牛的愿望就是变成一头最受欢迎的牛。现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎。 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认为牛C受欢迎。你的任务是求出有多少头牛被所有的牛认为是受欢迎的。
Under two situations the player could score one point.

⋅1. If you touch a buoy before your opponent, you will get one point. For example if your opponent touch the buoy #2 before you after start, he will score one point. So when you touch the buoy #2, you won't get any point. Meanwhile, you cannot touch buoy #3 or any other buoys before touching the buoy #2.

⋅2. Ignoring the buoys and relying on dogfighting to get point.
If you and your opponent meet in the same position, you can try to
fight with your opponent to score one point. For the proposal of game
balance, two players are not allowed to fight before buoy #2 is touched by anybody.

There are three types of players.

Speeder:
As a player specializing in high speed movement, he/she tries to avoid
dogfighting while attempting to gain points by touching buoys.
Fighter:
As a player specializing in dogfighting, he/she always tries to fight
with the opponent to score points. Since a fighter is slower than a
speeder, it's difficult for him/her to score points by touching buoys
when the opponent is a speeder.
All-Rounder: A balanced player between Fighter and Speeder.

There will be a training match between Asuka (All-Rounder) and Shion (Speeder).
Since the match is only a training match, the rules are simplified: the game will end after the buoy #1 is touched by anybody. Shion is a speed lover, and his strategy is very simple: touch buoy #2,#3,#4,#1 along the shortest path.

Asuka is good at dogfighting, so she will always score one point by dogfighting with Shion, and the opponent will be stunned for T seconds after dogfighting.
Since Asuka is slower than Shion, she decides to fight with Shion for
only one time during the match. It is also assumed that if Asuka and
Shion touch the buoy in the same time, the point will be given to Asuka
and Asuka could also fight with Shion at the buoy. We assume that in
such scenario, the dogfighting must happen after the buoy is touched by
Asuka or Shion.

The speed of Asuka is V1 m/s. The speed of Shion is V2 m/s. Is there any possibility for Asuka to win the match (to have higher score)?

Input

第一行两个数N,M。 接下来M行,每行两个数A,B,意思是A认为B是受欢迎的(给出的信息有可能重复,即有可能出现多个A,B)

Output

一个数,即有多少头牛被所有的牛认为是受欢迎的。

Sample Input

3 3
1 2
2 1
2 3

Sample Output

1

HINT

题意

题解:

缩点,然后看出度为0 的点是否只有一个,如果只有一个就输出这个点的大小

否则就是0

代码

#include<iostream>
#include<stdio.h>
#include<vector>
#include<cstring>
using namespace std;
#define maxn 10005
vector<int> E[maxn];
vector<int> rE[maxn];
int belong[maxn];
int vis[maxn];
int cl[maxn];
int cnt = ;
int siz[maxn];
int kis[maxn];
void dfs1(int x)
{
vis[x]=;
for(int i=;i<E[x].size();i++)
{
int e = E[x][i];
if(vis[e])continue;
dfs1(e);
}
cl[++cnt]=x;
}
void dfs2(int x)
{
vis[x]=;
belong[x]=cnt;
siz[cnt]++;
for(int i=;i<rE[x].size();i++)
{
int e = rE[x][i];
if(vis[e])continue;
dfs2(e);
}
}
int main()
{
int n,m;scanf("%d%d",&n,&m);
for(int i=;i<m;i++)
{
int x,y;scanf("%d%d",&x,&y);
E[x].push_back(y);
rE[y].push_back(x);
}
for(int i=;i<=n;i++)
{
if(!vis[i])
dfs1(i);
}
memset(vis,,sizeof(vis));
cnt = ;
for(int i=n;i>=;i--)
{
if(!vis[cl[i]])
{
cnt++;
dfs2(cl[i]);
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<E[i].size();j++)
{
if(belong[i]==belong[E[i][j]])
continue;
kis[belong[i]]++;
}
}
int ans1=,ans2=;
for(int i=;i<=cnt;i++)
{
if(kis[i]==)
{
ans1++;
ans2=i;
}
}
if(ans1==)
printf("%d\n",siz[ans2]);
else
puts("");
}

BZOJ 1051: [HAOI2006]受欢迎的牛 缩点的更多相关文章

  1. bzoj 1051: [HAOI2006]受欢迎的牛 tarjan缩点

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2092  Solved: 1096[Submit][Sta ...

  2. BZOJ 1051: [HAOI2006]受欢迎的牛( tarjan )

    tarjan缩点后, 有且仅有一个出度为0的强连通分量即answer, 否则无解 ----------------------------------------------------------- ...

  3. BZOJ 1051: [HAOI2006]受欢迎的牛(SCC)

    1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8172  Solved: 4470[Submit][Sta ...

  4. BZOJ 1051: [HAOI2006]受欢迎的牛 强连通缩点

    题目链接: http://www.lydsy.com/JudgeOnline/problem.php?id=1051 题解: 强连通缩点得到DAG图,将图转置一下,对入度为零的点跑dfs看看能不能访问 ...

  5. bzoj 1051: [HAOI2006]受欢迎的牛 (Tarjan 缩点)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1051 思路: 首先用Tarjan把环缩成点,要想收到所有人的欢迎,那么这个点的出度必为0,且 ...

  6. bzoj 1051 [HAOI2006]受欢迎的牛(tarjan缩点)

    题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1051 题解:缩点之后判断出度为0的有几个,只有一个那么输出那个强连通块的点数,否者 ...

  7. 洛谷 P2341 BZOJ 1051 [HAOI2006]受欢迎的牛

    题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...

  8. BZOJ 1051: [HAOI2006]受欢迎的牛

    Description 一个有向图,求所以能被别的点到达的点的个数. Sol Tarjan + 强连通分量 + 缩点. 缩点以后找强连通分量,缩点,然后当图有且仅有1个出度为1的点时,有答案. Cod ...

  9. 【BZOJ 1051】 1051: [HAOI2006]受欢迎的牛 (SCC)

    1051: [HAOI2006]受欢迎的牛 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如 ...

随机推荐

  1. 解决WCF大数据量传输 ,System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接

    开发中所用的数据需要通过WCF进行数据传输,结果就遇到了WCF大量传输问题 也就是提示System.Net.Sockets.SocketException: 远程主机强迫关闭了一个现有的连接 网上解决 ...

  2. Hadoop集群中Hbase的介绍、安装、使用

    导读 HBase – Hadoop Database,是一个高可靠性.高性能.面向列.可伸缩的分布式存储系统,利用HBase技术可在廉价PC Server上搭建起大规模结构化存储集群. 一.Hbase ...

  3. BS与CS的联系与区别

    C/S是Client/Server的缩写.服务器通常采用高性能的PC.工作站或小型机,并采用大型数据库系统,如Oracle.Sybase.InFORMix或SQL Server.客户端需要安装专用的客 ...

  4. iOS开发相关图书推荐

    Objective-C编程之道:iOS设计模式解析 作      者 [美] Carlo Chung 著:刘威 译 出 版 社 人民邮电出版社 出版时间 2011-11-01 版      次 1 页 ...

  5. Designing Evolvable Web API with ASP.NET 随便读,随便记 “The Internet,the World Wide Web,and HTTP”——HTTP

    HTTP 我们将只聚焦在于与创建 Web APIs有关的部分. HTTP 是信息系统中的一个应用层协议,是Web的支柱. 其原先由 Berners-Lee, Roy Fielding 和 Henrik ...

  6. Android Environment 类详解

    Android应用开发中,常使用Environment类去获取外部存储目录,在访问外部存储之前一定要先判断外部存储是否已经是可使用(已挂载&可使用)状态, 并且需要在AndroidManife ...

  7. Modules-nodejs

    Modules Node有一个简易的模块加载系统.在node中,文件和模块是一一对应的.下面示例是foo.js加载同一目录下的circle.js. foo.js的内容: var circle = re ...

  8. Javascript手记-垃圾收集

    如果有人问.net的垃圾回收,大家会马上想到gc,那如果有人问你javascript如何进行内存管理的呢?挠挠头,一口香瓜,听我细细道来! javascript具有自动垃圾收集机制,执行环境会负责管理 ...

  9. <Stackoverflow> 声望和节制

    什么是声望(reputation)?我是怎样获得(或失去)它的? 声望是一种粗略的测量,用来表示社区对你的信任度.通过让别人相信你知道自己正在讨论什么来获得.对网站的基本使用,包括问一个问题,回答,建 ...

  10. fedora20安装spin以及用户界面ispin

    (博客园-番茄酱原创) (最近感觉用make会出现库错误,所以改进了教程,把之前的make步骤省掉了,直接下载可执行文件进行配置最简单啦...) 1.首先,下载对应版本的spin,我64位的fedor ...