【BZOJ 1051】【HAOI 2006】受欢迎的牛
tarjan缩点模板
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 10003;
const int M = 50003;
void read(int &k) {
k = 0; int fh = 1; char c = getchar();
for(; c < '0' || c > '9'; c = getchar())
if (c == '-') fh = -1;
for(; c >= '0' && c <= '9'; c = getchar())
k = (k << 1) + (k << 3) + c - '0';
k = k * fh;
} struct node {
int from, nxt, to;
}E[M];
bool vis[N], inst[N];
int point[N], ans = 0, color = 0, col[N], in[N], out[N], sum[N], cnt = 0, n, m, DFN[N], low[N], st[N], top = 0;
void ins(int x, int y) {E[++cnt] = (node) {x, point[x], y}; point[x] = cnt;}
void tarjan(int x) {
DFN[x] = low[x] = ++cnt;
st[++top] = x; inst[x] = vis[x] = 1;
for(int tmp = point[x]; tmp; tmp = E[tmp].nxt)
if (!vis[E[tmp].to])
tarjan(E[tmp].to), low[x] = min(low[x], low[E[tmp].to]);
else if (inst[E[tmp].to]) low[x] = min(low[x], DFN[E[tmp].to]);
if (DFN[x] == low[x]) {
++color;
int u = 0;
while (u != x) {
u = st[top--];
col[u] = color;
inst[u] = 0;
++sum[color];
}
}
} int main() {
read(n); read(m);
int u, v;
for(int i = 1; i <= m; ++i) {read(u); read(v); ins(u, v);}
cnt = 0;
tarjan(1);
for(int i = 1; i <= m; ++i) {
u = col[E[i].from]; v = col[E[i].to];
if (u != v) ++out[u];
}
for(int i = 1; i <= color; ++i)
if (out[i] == 0) {
if (ans == 0) ans = sum[i];
else {ans = 0; break;}
}
printf("%d\n", ans);
return 0;
}
现在才学是不是太晚了= =
【BZOJ 1051】【HAOI 2006】受欢迎的牛的更多相关文章
- BZOJ 1051 HAOI 2006 受欢迎的牛
[题解] 先用tarjan缩点,然后如果某个强联通分量的出度为0,则该强联通分量内的点数为答案,否则无解.因为若其他所有的强联通分量都有边连向Ai,则Ai必定没有出边,否则Ai连向的点所属的强联通分量 ...
- [HAOI 2006]受欢迎的牛
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认 ...
- BZOJ 1051:[HAOI2006]受欢迎的牛(强连通分量)
受欢迎的牛Description每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么 ...
- HAOI 2006 受欢迎的牛 (洛谷2341)
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的"喜欢"是可以传递的--如果A喜 欢B,B喜 ...
- 【BZOJ 1051】[HAOI2006]受欢迎的牛
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] Tarjan算法强连通缩点 . 最后出度为0的点. 如果只有一个. 那么这个"大点"所包含的点的个数就是答案了. ...
- bzoj 1050 [ HAOI 2006 ] 旅行comf —— 并查集
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1050 没思路的话想想暴力就好了... 首先,比值最小就是确定最小值后最大值最小: 怎样确定最 ...
- BZOJ 1051 受欢迎的牛(Tarjan缩点)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4573 Solved: 2428 [Submit][S ...
- BZOJ 1051 最受欢迎的牛 解题报告
题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4438 Solved: 2353[S ...
- 【BZOJ】1051: [HAOI2006]受欢迎的牛(tarjan)
http://www.lydsy.com/JudgeOnline/problem.php?id=1051 这题还好-1A了..但是前提还是看了题解的 囧.....一开始认为是并查集,oh,不行,,无法 ...
- 【tarjan】BZOJ 1051:受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3134 Solved: 1642[Submit][Sta ...
随机推荐
- apache poi导出excel报表
Apache POI 是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能.POI为"P ...
- 使用 React.js 的渐进式 Web 应用程序:第 1 部分 - 介绍
使用 React.js 的渐进式 Web 应用程序:第 1 部分 - 介绍 使用 React.js 的渐进式 Web 应用程序:第 1 部分 - 介绍 来自译者 markzhai:大家也知道最近 ...
- HOLOTOOLKIT的使用
Using HoloToolkit-Unity in Your Project Open or create your project in Unity. Assets -> Import Pa ...
- jmeter jdbc request使用详解
1.在使用 jdbc request之前需要加载一个jar包 在test plan中将jar包引入到classpath中 2.创建一个JDBC Connection Configuration Var ...
- 实用js函数收集
1. 全选复选框: //复选框全选函数 function SelectAll() { var checkAll = document.getElementsByName("checkAll& ...
- cookie操作(jquery的cookie插件源码)
cookie : function (key, value, options) { var days, time, result, decode; // A key and value were gi ...
- OmniPlan文档链接
https://support.omnigroup.com/documentation/omniplan/mac/3.0/zh/introduction/#introduction
- Castle ActiveRecord 二级缓存使用 异常记录
在 本公司的 IBeamMDAA 框架下,如果是配置为本机调试时,AR的查询缓存工作正常,但如果部署到服务器上,工作不正常,二级缓存配置为使用 MemoryCahcheD 服务器,二级缓存没有能够根据 ...
- mediaplayer与surfaceView,无法播放问题
mediaplayer需要在surfaceView创建之后才能创建,不然会导致错误. surfaceholder = msurface.getHolder(); surfaceholder.setKe ...
- 纯html的table打印注意事项
1. 在firefox下,每页均会打印重复thead(表头),tfoot(表尾)的内容:IE8下无效(其它IE版本未测试) 2. 分页的处理 @media print { .page-brea ...