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. 嵌入式 hi3518c下ramdisk文件系统与文件系统烧写以及uboot中change-the-env

    NULL RAM : mkdir ramdisk_test  临时挂在点 dd if=/dev/zero of=123 bs=1k count=10000 建立空硬盘 losetup /dev/loo ...

  2. Oracle中函数/过程返回结果集的几种方式

    原文 Oracle中函数/过程返回结果集的几种方式 Oracle中函数/过程返回结果集的几种方式:    以函数return为例,存储过程只需改为out参数即可,在oracle 10g测试通过.    ...

  3. 2016计蒜之道复赛 菜鸟物流的运输网络 网络流EK

    题源:https://nanti.jisuanke.com/t/11215 分析:这题是一个比较经典的网络流模型.把中间节点当做源,两端节点当做汇,对节点进行拆点,做一个流量为 22 的流即可. 吐槽 ...

  4. HTML 表单提交 的简单代码

    <form action="check.php" method="post"> 用户名:<input type="text" ...

  5. bzoj 2959 长跑(LCT+BCC+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2959 [题意] n个点,提供操作:连边,修改点权,查询自定义边的方向后起点a终点b能经 ...

  6. Linux下用Intel编译器编译安装NetCDF-Fortan库(4.2以后版本)

    本来这个问题真的没必要写的,可是真的困扰我太久%>_<%,决定还是记录一下. 首先,最权威清晰的安装文档还是官方的: Building the NetCDF-4.2 and later F ...

  7. 纯css3鼠标经过图片显示描述特效

    http://***/Article/5582 今天给大家带来的是用css3技术实现鼠标经过图片,显示图片描述的动画效果.鼠标经过图片时,图片动画缩小并渐变显示描述.我们一起看看效果图: 在线预览   ...

  8. The First Pig Task

                         The First Pig Program 环境: Hadoop-1.1.2 pig-0.11.1 linux系统为CentOS6.4 jdk1.6 在伪分布 ...

  9. SQL入门

    ​ # SQL入门 数据库表 一个数据库(database)通常包含一个或多个表(table). 每一个表都有一个名字标识. 表单包含数据的记录(行). 一些重要的SQL命令(常用的吧) 命令 说明 ...

  10. HDU 1272 小希的迷宫 (并查集)

    小希的迷宫 题目链接: http://acm.hust.edu.cn/vjudge/contest/123393#problem/L Description 我们的小伙伴Bingo身为大二学长,他乐于 ...