【BZOJ1051】[HAOI2006]受欢迎的牛
【BZOJ1051】[HAOI2006]受欢迎的牛
题面
题解
假如\(A\)喜欢\(B\)就连一条\(A\)到\(B\)的边
然后缩点,如果图不连通就\(Impossible\)
否则输出出度为\(0\)的环的大小
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
inline int gi(){
register int data=0, w = 1;
register char ch=0;
while (ch != '-' && (ch > '9' || ch < '0')) ch = getchar();
if (ch == '-') w = -1 , ch = getchar();
while (ch >= '0' && ch <= '9') data = (data << 1) + (data << 3) + (ch ^ 48), ch = getchar();
return w * data;
}
#define MAX_N 10005
#define MAX_M 50005
struct Edge {
int to, next;
} e[MAX_M];
int h[MAX_N], cnt;
int n, m;
void Add_Edge(int u, int v) {
e[cnt].to = v, e[cnt].next = h[u], h[u] = cnt++;
}
int tim = 0, num = 0, top = 0;
int low[MAX_N], dfn[MAX_N], color[MAX_N], st[MAX_N];
int tot[MAX_N];
void tarjan(int x) {
low[x] = dfn[x] = ++tim;
st[++top] = x;
for (register int i = h[x]; ~i; i = e[i].next) {
if (!dfn[e[i].to]) {
tarjan(e[i].to);
low[x] = min(low[x], low[e[i].to]);
} else if (!color[e[i].to])
low[x] = min(low[x], dfn[e[i].to]);
}
if (low[x] == dfn[x]) {
color[x] = ++num;
tot[num]++;
while (st[top] != x) {
color[st[top]] = num;
tot[num]++;
--top;
}
--top;
}
}
int d[MAX_N];
int main () {
memset(h, -1, sizeof(h));
n = gi(), m = gi();
for (register int i = 1; i <= m; i++) {
int u = gi(), v = gi();
Add_Edge(u, v);
}
for (register int i = 1; i <= n; i++)
if (!dfn[i]) tarjan(i);
int ans = 0, F = 0;
for (register int x = 1; x <= n; x++)
for (int i = h[x]; ~i; i = e[i].next)
if (color[x] != color[e[i].to]) d[color[x]]++;
for (register int i = 1; i <= num; i++) {
if (d[i] == 0) F++, ans = tot[i];
}
if (F == 1) printf("%d\n", ans);
else printf("0\n");
return 0;
}
【BZOJ1051】[HAOI2006]受欢迎的牛的更多相关文章
- bzoj1051 [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4773 Solved: 2541[Submit][Sta ...
- 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 ...
随机推荐
- [WinCE | VS2008 | Solution] VS2008 building WinCE projects taking a long time
1. Open C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.CompactFramework.Common.targets 2. Find pa ...
- [翻译] MotionBlur
MotionBlur https://github.com/fastred/MotionBlur MotionBlur allows you to add motion blur effect to ...
- [翻译] GoogleMaterialDesignIcons
GoogleMaterialDesignIcons Google Material Design Icons Font for iOS GMD图片样式的字体,用于iOS开发. It is based ...
- iOS设计模式 - 责任链
iOS设计模式 - 责任链 原理图 说明 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链.请求在这个链上传递,直到链上的某一个对象决定处理此请求.发出这个请求的客户端并不知道链 ...
- 使用开源库 MagicalRecord 操作 CoreData
MagicalRecord https://github.com/magicalpanda/MagicalRecord 注意: MagicalRecord 在 ARC 下运作,Core Data ...
- Request URL参数
登录跳转完整参考: http://www.cnblogs.com/dreamer-fish/p/5435274.html request.META.get('HTTP_REFERER', '/') # ...
- 北美PM活着的攻略
http://www.followmedoit.com/bbs/forum.php?mod=viewthread&tid=47&extra=page%3D1 在北美,做PM不易,需要交 ...
- Linux dumpe2fs命令详解
dumpe2fs: 查看格式化之后的文件系统信息. dumpe2fs使用 [root@localhost omc]# dumpe2fs --help<BR>dumpe2fs 1.41.12 ...
- web开发方面会遇到哪些缓存?分别如何优化
Web缓存定义: Web缓存游走于服务器和客户端之间,这个服务器可能是源服务器(资源所驻留的服务器Add),数量可能是1个或多个. Web缓存就在服务器-客户端之间搞监控,监控请求,并且把请求输出的内 ...
- .net mvc Html.DropDownListFor 设置默认值无效
错误描述: 控制器部分: //从数据字典中加载下拉框 (使用DropDownListFor,SelectList 中不需要设置选中值,即便设置了选中值,也会优先读取Model中对应的值) ViewBa ...