BZOJ4562: [Haoi2016]食物链
Description
Input
Output
一个整数即食物网中的食物链条数
Sample Input
1 2
1 4
1 10
2 3
2 5
4 3
4 5
4 8
6 5
7 6
7 9
8 5
9 8
10 6
10 7
10 9
Sample Output
#include<cstdio>
#include<cctype>
#include<queue>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i;i=next[i])
using namespace std;
const int BufferSize=1<<16;
char buffer[BufferSize],*head,*tail;
inline char Getchar() {
if(head==tail) {
int l=fread(buffer,1,BufferSize,stdin);
tail=(head=buffer)+l;
}
return *head++;
}
inline int read() {
int x=0,f=1;char c=Getchar();
for(;!isdigit(c);c=Getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=Getchar()) x=x*10+c-'0';
return x*f;
}
const int maxn=100010;
const int maxm=200010;
typedef long long ll;
int n,m,first[maxn],deg[maxn],in[maxn],is[maxn],next[maxm],to[maxm],e;
void AddEdge(int u,int v) {
in[v]++;deg[v]++;is[u]=1;to[++e]=v;next[e]=first[u];first[u]=e;
}
int Q[maxn],f[maxn],g[maxn];
int main() {
n=read();m=read();
rep(i,1,m) AddEdge(read(),read());
int l=1,r=0;
rep(i,1,n) if(!in[i]) Q[++r]=i,f[i]=1;
while(l<=r) {
int x=Q[l++];
ren {
f[to[i]]+=f[x];
if(!(--in[to[i]])) Q[++r]=to[i];
}
}
int ans=0;
rep(i,1,n) if(!is[i]&°[i]) ans+=f[i];
printf("%d\n",ans);
return 0;
}
BZOJ4562: [Haoi2016]食物链的更多相关文章
- [bzoj4562][Haoi2016]食物链_记忆化搜索_动态规划
食物链 bzoj-4562 Haoi-2016 题目大意:给你n个点,m条边的DAG,求所有的满足条件的链,使得每条链的起点是一个入度为0的点,中点是一条出度为0的点. 注释:$1\le n\le 1 ...
- bzoj4562: [Haoi2016]食物链--记忆化搜索
这道题其实比较水,半个小时AC= =对于我这样的渣渣来说真是极大的鼓舞 题目大意:给出一个有向图,求入度为0的点到出度为0的点一共有多少条路 从入读为零的点进行记忆化搜索,搜到出度为零的点返回1 所有 ...
- 【BZOJ-4562】食物链 记忆化搜索(拓扑序 + DP)
4562: [Haoi2016]食物链 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 133 Solved: 112[Submit][Status] ...
- bzoj 4562 [Haoi2016]食物链
4562: [Haoi2016]食物链 Time Limit: 10 Sec Memory Limit: 128 MB Description 如图所示为某生态系统的食物网示意图,据图回答第1小题 ...
- 洛谷——P3183 [HAOI2016]食物链
P3183 [HAOI2016]食物链 题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题现在给你n个物种和m条能量流动关系,求其中的食物链条数.物种的名称为从1到n编号M条能量流动关系形 ...
- 洛谷 P3183 [HAOI2016]食物链 题解
P3183 [HAOI2016]食物链 题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题现在给你n个物种和m条能量流动关系,求其中的食物链条数.物种的名称为从1到n编号M条能量流动关系形 ...
- 【bzoj4562】[Haoi2016]食物链 拓扑排序+dp
原文地址:http://www.cnblogs.com/GXZlegend/p/6832118.html 题目描述 如图所示为某生态系统的食物网示意图,据图回答第1小题 现在给你n个物种和m条能量流动 ...
- 【bzoj4562】[Haoi2016]食物链
*题目描述: 如图所示为某生态系统的食物网示意图,据图回答第1小题 现在给你n个物种和m条能量流动关系,求其中的食物链条数. 物种的名称为从1到n编号 M条能量流动关系形如 a1 b1 a2 b2 a ...
- 【bzoj4562】HAOI2016食物链
记忆化搜索水过去了…… QwQ #include<bits/stdc++.h> #define N 400010 typedef long long ll; using namespace ...
随机推荐
- Endnote专题之--output style相关问题
Endnote专题之--output style相关问题 1. 打开output style, Edit--->Output Styles--->选择要编辑的某个style模板,如下面的E ...
- 如何处理 在html中 li 的高度自适应(且li里面的内容有浮动的情况下)
废话不多说,我们写贴出代码 这个是 Html 代码 <div class="main"> <ul> <li> <div class=&qu ...
- nyoj 71 独木舟上的旅行(贪心专题)
独木舟上的旅行 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 进行一次独木舟的旅行活动,独木舟可以在港口租到,并且之间没有区别.一条独木舟最多只能乘坐两个人,且乘客 ...
- PHP简单 对象(object) 与 数组(array) 的转换
数组是PHP的灵魂,非常强大,但有时候面向对象编程也是挺方便的,数组 与 对象 之间切换也是常有的事: /** * 数组 转 对象 * * @param array $arr 数组 * @return ...
- Linux进程间通信(一): 信号 signal()、sigaction()
一.什么是信号 用过Windows的我们都知道,当我们无法正常结束一个程序时,可以用任务管理器强制结束这个进程,但这其实是怎么实现的呢?同样的功能在Linux上是通过生成信号和捕获信号来实现的,运行中 ...
- 【Go入门教程7】并发(goroutine,channels,Buffered Channels,Range和Close,Select,超时,runtime goroutine)
有人把Go比作21世纪的C语言,第一是因为Go语言设计简单,第二,21世纪最重要的就是并行程序设计,而Go从语言层面就支持了并行. goroutine goroutine是Go并行设计的核心.goro ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- embed标签loop=true背景音乐无法循环
在html网页中加入背景音乐并设置为循环播放.一开始用<embed>标签,设置loop="true", 但是结果发现在IE浏览器可以,但是在chrome浏览器却无法实现 ...
- composer 安装提示 PHP Warning: readfile(): SSL operation failed with code 1
这是php设置openssl 没有指定cacert.pem (证书) 第一步:下载 cacert.pem 文件下载地址 https://curl.haxx.se/docs/caextract.html ...
- linux常用命令积累
1.jps jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/u ...