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 ...
随机推荐
- pthread_cond_wait()函数的理解(摘录)
/************pthread_cond_wait()的使用方法**********/pthread_mutex_lock(&qlock); /*lock*/pthread_c ...
- ASP.NET Razor——ASP.NET Razor - C#代码语法
Razor 同时支持 C# (C sharp) 和 VB (Visual Basic). 主要的 Razor C# 语法规则 Razor 代码块包含在 @{ ... } 中 内联表达式(变量和函数)以 ...
- 解决java使用https协议请求出现证书不信任问题(PKIX path building failed)
解决https请求时出现pkix path building fail错误 方法 将submail.cer 安全证书导入到java中的cacerts证书库 (sumail是我从https://api. ...
- qsort C++ VS2013 leetcode
class Solution { private: static int compare(const void * a, const void * b) { return (*(int*)a - *( ...
- [原创]cocos2d-x研习录-第三阶 特性之加速度传感器
智能手机的游戏与应用中,也经常会用到加速传感器事件来丰富用户的体验,比如飞翔的企鹅(英文AirPenguin)游戏就是通过加速度传感器来控制角色的移动和跳跃方向.下面学习Cocos2D-x中如何使用加 ...
- Python-SocketServer源码
贴到博客,地铁上看- """Generic socket server classes. This module tries to capture the various ...
- Does Lamda expression return value?
Basically, the compiler does this for you. If you write a lambda as a single statement (and don't in ...
- tar 命令详解
tar命令[root@Linux ~]# tar [-cxtzjvfpPN] 文件与目录 -C 目标目录(注:解压时)参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个 ...
- 关于databinding的细节
原文在此:http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial 完整译文在此:http://www.cn ...
- 动态sql语句基本语法--Exec与Exec sp_executesql 的区别
http://www.cnblogs.com/goody9807/archive/2010/10/19/1855697.html 动态sql语句基本语法 1 :普通SQL语句可以用Exec执行 ...