bzoj1051 [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛
Time Limit: 10 Sec Memory Limit: 162 MB
Submit: 4773 Solved: 2541
[Submit][Status][Discuss]
Description
Input
Output
一个数,即有多少头牛被所有的牛认为是受欢迎的。
Sample Input
1 2
2 1
2 3
Sample Output
HINT
#include <cstdio>
#include <stack>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = , maxm = ; int n, m,head[maxn],to[maxm],nextt[maxm],tot,scc[maxn],dfsclock,low[maxn],pre[maxn],num,shuliang[maxn],ans;
int head2[maxn], to2[maxn], nextt2[maxn], tot2;
stack <int> s; void add(int x, int y)
{
tot++;
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot;
} void add2(int x, int y)
{
tot2++;
to2[tot2] = y;
nextt2[tot2] = head2[x];
head2[x] = tot2;
} void tarjan(int u)
{
s.push(u);
low[u] = pre[u] = ++dfsclock;
for (int i = head[u];i;i = nextt[i])
{
int v = to[i];
if (!pre[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else
if (!scc[v])
low[u] = min(low[u], pre[v]);
}
if (low[u] == pre[u])
{
num++;
while ()
{
int t = s.top();
s.pop();
scc[t] = num;
shuliang[num]++;
if (t == u)
break;
}
}
} void lianbian(int u)
{
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (scc[u] != scc[v])
add2(scc[u], scc[v]);
}
} int main()
{
scanf("%d%d", &n, &m);
for (int i = ; i <= m; i++)
{
int a, b;
scanf("%d%d", &a, &b);
add(a, b);
}
for (int i = ; i <= n; i++)
if (!scc[i])
tarjan(i);
for (int i = ; i <= n; i++)
lianbian(i);
for (int i = ; i <= num; i++)
if (!head2[i])
{
if (ans)
{
printf("0\n");
return ;
}
ans = shuliang[i];
}
printf("%d\n", ans); return ;
}
bzoj1051 [HAOI2006]受欢迎的牛的更多相关文章
- bzoj1051: [HAOI2006]受欢迎的牛(tarjan板子)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6064 Solved: 3179[Submit][Sta ...
- [BZOJ1051][HAOI2006] 受欢迎的牛 tarjan求联通分量
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5687 Solved: 3016[Submit][Sta ...
- [Bzoj1051][HAOI2006]受欢迎的牛(缩环)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6676 Solved: 3502[Submit][Sta ...
- bzoj1051: [HAOI2006]受欢迎的牛(强联通)
1051: [HAOI2006]受欢迎的牛 题目:传送门 题解: 今天又做一道水题... 强联通啊很明显 水个模板之后统计一下每个强联通分量中点的个数,再统计一下出度... 不难发现:缩点之后当且仅当 ...
- [bzoj1051] [HAOI2006]受欢迎的牛 (Tarjan+缩点)
强连通图,缩点 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受 ...
- [BZOJ1051] [HAOI2006] 受欢迎的牛 (强联通分量)
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...
- 【强连通分量】Bzoj1051 HAOI2006 受欢迎的牛
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认 ...
- BZOJ1051 [HAOI2006]受欢迎的牛 Tarjan 强连通缩点
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1051 题意概括 有n只牛,有m个羡慕关系. 羡慕关系具有传递性. 如果A羡慕B,B羡慕C,那么我们 ...
- bzoj1051 [HAOI2006]受欢迎的牛 tarjan&&缩点
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的——如果A喜 欢B,B喜欢C,那么A也喜欢C ...
随机推荐
- cf 732c
/* cf732c 错过的最少次数 _________________________________________________________________________________ ...
- HttpClient封装工具类
import java.io.IOException; import java.net.URI; import java.util.ArrayList; import java.util.List; ...
- Python 十进制转二进制代码实现
def my_bin(num): la = [] if num < 0: return '-' + my_bin(abs(num)) while True: num, remainder = d ...
- https/相对路径,绝对路径
1. htttps HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全 ...
- lua判断表中数据是否连续,0可以代表任何数
最近看到有lua面试题,挺有意思的,一张表中有若干个数,0可以代表任何数 比如有张表{9, 2, 4, 1, 3, 0, 0, 0, 0},按照规则这张表中的四个0可以用来代表5,6,7,8,那么这张 ...
- (转)A Survival Guide to a PhD
Andrej Karpathy blog About Hacker's guide to Neural Networks A Survival Guide to a PhD Sep 7, 2016 T ...
- CSRF攻击
1.什么是CSRF攻击CSRF(Cross-site request forgery),跨站请求伪造.CSRF攻击的原理如下:1)用户登录正常的网站A后,在本地生成Cookie2)在不登出A的情况下, ...
- golang开发环境(2016.9.16)
一.windows下安装 1.下载go1.7.3.windows-amd64.msi,建议默认安装到‘C:\Go\’ 2.环境变量 变量 值 说明 Path C:\Go\bin 安装程序默认会设置,如 ...
- MYCAT介绍(转)
从定义和分类来看,它是一个开源的分布式数据库系统,是一个实现了MySQL协议的服务器,前端用户可以把它看作是一个数据库代理,用MySQL客户端工具和命令行访问,而其后端可以用MySQL原生协议与多个M ...
- 入侵本地Mac OS X的详细过程 转自https://yq.aliyun.com/articles/22459?spm=5176.100239.blogcont24250.10.CfBYE9
摘要: 本文从提升权限漏洞的一系列巧妙的方法来绕过受保护的Mac OS X.有些已经被处于底层控制,但由于它们存在着更多的认证和修补程序,我们不妨让这些提供出来,以便需要的人学习它们.虽然我不只是要利 ...