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 ...
随机推荐
- ✡ leetcode 174. Dungeon Game 地牢游戏 --------- java
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- Servlet中The requested resource is not available错误
自己为了测试servlet,用MyEclipse2015写了一个简单的登录程序. 1.登录页面index.jsp. <%@ page language="java" impo ...
- [转] "self = [super init]"的解释与潜藏bug
Objective-C的推荐init方法写法如下: - (id) init { if(self = [super init]) { //为子类增加属性进行初始化 } return self; } 这里 ...
- javascript的this
关于JavaScript中的this的取值: 函数在创建的时候,会创建两个隐藏属性:函数的上下文.实现函数行为的代码(调用属性):以及prototype属性.length属性. 函数在调用的时候,除了 ...
- sql语句executeQuery和executeUpdate之间的区别
方法一.executeQuery 用于产生单个结果集(ResultSet)的语句,例如 SELECT 语句. 被使用最多的执行 SQL 语句的方法.这个方法被用来执行 SELECT 语句,它几乎是使用 ...
- [Linux] - CentOS 安装nginx
linux版本:CentOS 6.0+ 安装nginx方法: 1.下载nginx rpm包命令: wget http://nginx.org/packages/centos/6/noarch/RPMS ...
- ios 写项目的时候遇到的问题及解决方案(3)
22.看了苹果的文档,里面有这一句话:All launch images must be PNG files and must reside in the top level of your appl ...
- IOS开发--自定义segment控件,方便自定义样式
系统的segment控件太封闭,想换个颜色加个背景太难了,忍不住自己写一个,以备不时之需 这个控件给出了很多自定义属性的设置,用起来还是比较方便的,需要注意的 itemWidth如果不设置,则会按照控 ...
- SQLite中的时间日期函数(转)
SQLite包含了如下时间/日期函数: datetime().......................产生日期和时间date()...........................产生日期tim ...
- javascript判断某种元素是否进入可视区域
判断是否在指定的可视区域内,先用最简单的方式,比如整个页面为可视区域 找到几个关键因素: sTop= $(window).scrollTop(); //滚动条距顶部的高度 clientHeight= ...