[HAOI 2006]受欢迎的牛
Description
Input
Output
一个数,即有多少头牛被所有的牛认为是受欢迎的。
Sample Input
1 2
2 1
2 3
Sample Output
HINT
题解
好久没打$tarjan$了,码在这当模板存着。
//It is made by Awson on 2017.9.24
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
using namespace std;
const int N = ;
const int M = ;
int Read() {
char ch = getchar();
int sum = ;
while (ch < '' || ch > '') ch = getchar();
while (ch >= '' && ch <= '') sum = (sum<<)+(sum<<)+ch-, ch = getchar();
return sum;
} int n, m, u, v;
struct tt {
int from, to, next;
}edge[M+];
int path[N+], top;
int dfn[N+], low[N+], t;
bool vis[N+];
int S[N+], topS;
int sccno[N+], sccnum;
int in[N+], tot[N+]; void add(int u, int v) {
edge[++top].to = v;
edge[top].from = u;
edge[top].next = path[u];
path[u] = top;
}
void tarjan(int r) {
dfn[r] = low[r] = ++t;
vis[r] = ;
S[topS++] = r;
for (int i = path[r]; i; i = edge[i].next) {
if (!dfn[edge[i].to]) {
tarjan(edge[i].to);
low[r] = Min(low[edge[i].to], low[r]);
}
else if (vis[edge[i].to])
low[r] = Min(low[r], dfn[edge[i].to]);
}
if (dfn[r] == low[r]) {
sccnum++;
while (topS > && S[topS] != r) {
vis[S[--topS]] = ;
sccno[S[topS]] = sccnum;
tot[sccnum]++;
}
}
}
void work() {
n = Read(), m = Read();
for (int i = ; i <= m; i++) {
u = Read(), v = Read();
add(u, v);
}
for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i);
for (int i = ; i <= m; i++)
if (sccno[edge[i].to] != sccno[edge[i].from])
in[sccno[edge[i].from]]++;
int ans = , cntt = ;
for (int i = ; i <= sccnum; i++)
if (!in[i]) cntt++, ans=i;
if (cntt == ) printf("%d\n", tot[ans]);
else printf("0\n");
}
int main() {
work();
return ;
}
[HAOI 2006]受欢迎的牛的更多相关文章
- HAOI 2006 受欢迎的牛 (洛谷2341)
题目描述 每头奶牛都梦想成为牛棚里的明星.被所有奶牛喜欢的奶牛就是一头明星奶牛.所有奶 牛都是自恋狂,每头奶牛总是喜欢自己的.奶牛之间的"喜欢"是可以传递的--如果A喜 欢B,B喜 ...
- BZOJ 1051 HAOI 2006 受欢迎的牛
[题解] 先用tarjan缩点,然后如果某个强联通分量的出度为0,则该强联通分量内的点数为答案,否则无解.因为若其他所有的强联通分量都有边连向Ai,则Ai必定没有出边,否则Ai连向的点所属的强联通分量 ...
- bzoj1051 [HAOI2006]受欢迎的牛
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4773 Solved: 2541[Submit][Sta ...
- bzoj 1051 (强连通) 受欢迎的牛
题目:这里 题意: Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这 种关系是具有传递性的,如果A认为B受欢迎,B认为 ...
- BZOJ 1051 最受欢迎的牛 解题报告
题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4438 Solved: 2353[S ...
- 【BZOJ1051】1051: [HAOI2006]受欢迎的牛 tarjan求强连通分量+缩点
Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B受欢迎. 这种关系是具有传递性的,如果A认为B受欢迎,B认为C受欢迎,那么牛A也认 ...
- 【bzoj1051】 [HAOI2006]受欢迎的牛 tarjan缩点判出度算点数
[bzoj1051] [HAOI2006]受欢迎的牛 2014年1月8日7450 Description 每一头牛的愿望就是变成一头最受欢迎的牛.现在有N头牛,给你M对整数(A,B),表示牛A认为牛B ...
- 【BZOJ】1051: [HAOI2006]受欢迎的牛(tarjan)
http://www.lydsy.com/JudgeOnline/problem.php?id=1051 这题还好-1A了..但是前提还是看了题解的 囧.....一开始认为是并查集,oh,不行,,无法 ...
- BZOJ 1051 受欢迎的牛(Tarjan缩点)
1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 4573 Solved: 2428 [Submit][S ...
随机推荐
- Java基础学习笔记十三 常用API之正则表达式、Date、DateFormat、Calendar
正则表达式 正则表达式(英语:Regular Expression,在代码中常简写为regex).正则表达式是一个字符串,使用单个字符串来描述.用来定义匹配规则,匹配一系列符合某个句法规则的字符串.在 ...
- 04_Python的数据类型1数值和字符串_Python编程之路
上一节我们通过一个helloworld程序学习python的一些简单操作,还有输入与输出 这节我们来讲Python的数据类型与变量的操作 Python的交互器 在讲这个之前,我要先讲一下python的 ...
- C语言第一次博客作业——输入输出格式
一.PTA实验作业 注意:本次PTA实验共有8个题目,在博客上只要贴:4个题目就可以,分别为: 题目1:7-3 温度转换 (1分) 题目2:7-4 将x的平方赋值给y (2分) 题目3:7-6 是不是 ...
- c语言第五次作业--函数
一.PTA实验作业 题目1.使用函数输出一个整数的逆序数 1.本题PTA提交列表 2.设计思路 1.int mod,rever:分别表示余数和返回的数 2.while(number%10 || num ...
- 201621123057 《Java程序设计》第11周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序BounceThread 1.1 BallR ...
- 团队作业4——第一次项目冲刺(Alpha版本)11.16
a. 提供当天站立式会议照片一张 举行站立式会议,讨论项目安排: 整理各自的任务汇报: 全分享遇到的困难一起讨论: 讨论接下来的计划: b. 每个人的工作 (有work item 的ID) 1.前两天 ...
- SWFUpload文件上传详解
SWFUpload是一个flash和js相结合而成的文件上传插件,其功能非常强大. SWFUpload的特点: 1.用flash进行上传,页面无刷新,且可自定义Flash按钮的样式; 2.可以在浏览器 ...
- LeetCode题型分类及索引
目录 这是一个对LeetCode题目归类的索引,分类标准参考了July大神的<编程之法>以及LeetCode的tag项.分类可能还不太合理,逐步完善,请见谅~ 题主本人也在一点一点的刷题, ...
- HttpClient 上传多个文件
using (System.Net.Http.HttpClient client = new System.Net.Http.HttpClient()) { client.BaseAddress = ...
- Web系统Login拦截器
所需要导入的包类:import org.springframework.web.servlet.HandleInterceptor;(拦截器要继承该类) public class loginInter ...