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 ...
随机推荐
- NLP概述
1,词法分析 待续 2,文本分类 文本表示: 重点是贝叶斯模型:二项表示法和多项表示法.(向量维度为词库大小,一个是01,一个是频次).模型重点在于化后验为先验. 还有其他模型:机器学习模型,分布式模 ...
- linux按键驱动之poll
上一节应用程序的死循环里的读函数是一直在读的:在实际的应用场所里,有没有那么一种情况,偶尔有数据.偶尔没有数据,答案当然是有的.-->poll机制:Poll机制实现的是一定时间如果没有按键的话就 ...
- ✡ leetcode 172. Factorial Trailing Zeroes 阶乘中的结尾0个数--------- java
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
- .lib文件 .h文件 .dll文件
.lib代表的是静态数据连接库,在windows系统中起到链接程序和函数的作用,存放的是函数的是函数调用的信息,是obj文件的集合.相当于linux中的.a或.0. .so文件.lib文件是不对外公开 ...
- JSON.parse() 和 JSON.stringify()
JSON.parse()和JSON.stringify() 1.parse 用于从一个字符串中解析出json 对象.例如 var str='{"name":"cpf& ...
- WPF-编程问题和解决
1.wpf中点击button按钮后怎么让TextBlock显示button按钮的值? <TextBlock x:Name="CurProtext" Grid.Column=& ...
- jQuery实例——jQuery实现联动下拉列表查询框--转http://www.cnblogs.com/picaso/archive/2012/04/08/2437442.html#undefined
jQuery实例--jQuery实现联动下拉列表查询框 在查询与列表显示的时候经常用到联动列表显示,比如一级选项是国家,二级选项是省,三级是市,这样的联动是联系的实时导出的,比如你不可能选择了四川 ...
- C#中dynamic的正确用法
C#中dynamic的正确用法 http://www.cnblogs.com/qiuweiguo/archive/2011/08/03/2125982.html dynamic是FrameWork4 ...
- linux 删除进程的多种方法
kill pid kill -9 pid kill -15 pid pkill -f *.php kill -s 9 pid
- spring 中容器 map、set、list、property 的 bean 实例化
参考:http://www.kaifajie.cn/spring/9966.html <bean id="fieldMap" class="org.springfr ...