_bzoj1051 [HAOI2006]受欢迎的牛【强联通】
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1051
保存tarjan模版。
求强联通分量,缩点。
#include <cstdio>
#include <cstring>
#include <set>
#include <algorithm> const int maxn = 10005, maxm = 50005; int n, m, t1, t2;
int top[maxn], dfn[maxn], dfs_clock, scc[maxn], scc_cnt, stk[maxn], top_;
int head1[maxn], to1[maxm], next1[maxm], lb1;
int siz[maxn];
char out[maxn]; inline void ist1(int aa, int ss) {
to1[lb1] = ss;
next1[lb1] = head1[aa];
head1[aa] = lb1;
++lb1;
}
void dfs(int r) {
dfn[r] = top[r] = ++dfs_clock;
stk[top_++] = r;
for (int j = head1[r]; j != -1; j = next1[j]) {
if (!dfn[to1[j]]) {
dfs(to1[j]);
top[r] = std::min(top[r], top[to1[j]]);
}
else if (!scc[to1[j]]) {
top[r] = std::min(top[r], dfn[to1[j]]);
}
}
if (dfn[r] == top[r]) {
++scc_cnt;
while (stk[top_ - 1] != r) {
scc[stk[top_ - 1]] = scc_cnt;
--top_;
++siz[scc_cnt];
}
scc[r] = scc_cnt;
--top_;
++siz[scc_cnt];
}
} int main(void) {
//freopen("in.txt", "r", stdin);
memset(head1, -1, sizeof head1);
memset(next1, -1, sizeof next1);
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d", &t1, &t2);
ist1(t1, t2);
} for (int i = 1; i <= n; ++i) {
if (!scc[i]) {
dfs(i);
}
} for (int i = 1; i <= n; ++i) {
for (int j = head1[i]; j != -1; j = next1[j]) {
if (scc[i] != scc[to1[j]]) {
out[scc[i]] = 1;
}
}
} char flag = 0;
int ans = 0;
for (int i = 1; i <= scc_cnt; ++i) {
if (!out[i]) {
if (flag) {
ans = 0;
break;
}
else {
flag = 1;
ans = siz[i];
}
}
}
printf("%d\n", ans);
return 0;
}
_bzoj1051 [HAOI2006]受欢迎的牛【强联通】的更多相关文章
- [BZOJ1051] [HAOI2006] 受欢迎的牛 (强联通分量)
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也 ...
- bzoj1051: [HAOI2006]受欢迎的牛(强联通)
1051: [HAOI2006]受欢迎的牛 题目:传送门 题解: 今天又做一道水题... 强联通啊很明显 水个模板之后统计一下每个强联通分量中点的个数,再统计一下出度... 不难发现:缩点之后当且仅当 ...
- [BZOJ1051][HAOI2006] 受欢迎的牛 tarjan求联通分量
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 5687 Solved: 3016[Submit][Sta ...
- 1051: [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2276 Solved: 1190[Submit][Sta ...
- 【BZOJ 1051】 1051: [HAOI2006]受欢迎的牛 (SCC)
1051: [HAOI2006]受欢迎的牛 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如 ...
- P2341 [HAOI2006]受欢迎的牛(tarjan+缩点)
P2341 [HAOI2006]受欢迎的牛 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的“喜欢”是可以传递的 ...
- 洛谷——P2341 [HAOI2006]受欢迎的牛//POJ2186:Popular Cows
P2341 [HAOI2006]受欢迎的牛/POJ2186:Popular Cows 题目背景 本题测试数据已修复. 题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所 ...
- bzoj1051 [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4773 Solved: 2541[Submit][Sta ...
- 【BZOJ】1051: [HAOI2006]受欢迎的牛
[HAOI2006]受欢迎的牛 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢 ...
随机推荐
- Callable线程
写一个Callable线程 import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; im ...
- 【转载】同步和互斥的POSIX支持(互斥锁,条件变量,自旋锁)
上篇文章也蛮好,线程同步之条件变量与互斥锁的结合: http://www.cnblogs.com/charlesblc/p/6143397.html 现在有这篇文章: http://blog.cs ...
- 【深度探索c++对象模型】Function语义学之成员函数调用方式
非静态成员函数 c++的设计准则之一就是:非静态成员函数至少和一般的非成员函数有相同的效率.编译器内部已将member函数实体转换为对等的nonmember函数实体. 转化步骤: 1.改写函数原型以安 ...
- CentOS 7.0安装Zimbra 8.6邮件服务器
Zimbra的核心产品是Zimbra协作套件(Zimbra Collaboration Suite,简称ZCS). 系统:Centos7 ip地址:192.168.127.131 安装前准备 1.关闭 ...
- [RxJS] Implement RxJS `mergeMap` through inner Observables to Subscribe and Pass Values Through
Understanding sources and subscribers makes it much easier to understand what's going on with mergeM ...
- Visual Studio VS2010 如何修改默认的编辑语言
1 比如我要把默认是C++的配置改成C#,在工具-导入和导出设置中,重置所有设置 2 这里改成新的语言 3 重置完成
- SQL - 创建一个学生表,要求有主键约束和非空约束
CREATE TABLE [dbo].[Student] ( [ID] [int] NOT NULL, [Name] [nchar](10) NOT NULL, [Age] [int] NOT NUL ...
- Linux学习笔记:系统启动引导过程
Linux系统启动引导过程 近期发现自己在仅仅是掌握上有几个比較硬的伤: 一.知识体系碎片,比方Linux,这学点那学点,结果没有成体系,串不起来: 二.记忆时间短暂,非常多的内容学了就忘,最后的结果 ...
- C项目实践--网络协议和套接字编程
1.TCP/IP协议 TCP/IP协议是一组包括TCP协议和IP协议,UDP(User Datagram Protocol)协议,ICMP(Internet Control Message Proto ...
- Koa2学习(六)使用koa-router
Koa2学习(六)使用koa-router 配置简单路由 引入中间件 配置需要的路由 通过app.use注册路由 const Koa = require('koa') const app = new ...