bzoj 2783树
来源:互联网 发布:kmp算法next数组怎么求 编辑:程序博客网 时间:2024/05/16 14:01
在这个问题中,给定一个值S和一棵树。在树的每个节点有一个正整数,问有多少条路径的节点总和达到S。路径中节点的深度必须是升序的。假设节点1是根节点,根的深度是0,它的儿子节点的深度为1。路径不必一定从根节点开始。
Input
第一行是两个整数N和S,其中N是树的节点数。 第二行是N个正整数,第i个整数表示节点i的正整数。 接下来的N-1行每行是2个整数x和y,表示y是x的儿子。
Output
输出路径节点总和为S的路径数量。
Sample Input
3 3
1 2 3
1 2
1 3
Sample Output
2
HINT
对于100%数据,N≤100000,所有权值以及S都不超过1000。
分析
因为求的路径都是从在包含根的一条路径上。用dfs。记录元素前缀和就好了。
代码:
#define N 100005#include <algorithm>#include <iostream>#include <cstdio>#include <queue>#include <cmath>using namespace std;struct KK{ int to,next;}e[N*2];int head[N];int st[N],Q[N],sum[N];int top,ans;int s,n;int cnt=0;void add(int x,int y){ e[++cnt].to=y;e[cnt].next=head[x];head[x]=cnt;}void dfs(int dep,int now,int root){ st[++top]=now; int need=lower_bound(st+1,st+top+1,now-s)-st; if(st[need]+s==now && need>0 && need<=top) ans++; for(int i=head[dep];i;i=e[i].next) { if(e[i].to==root) continue; dfs(e[i].to,now+sum[e[i].to],dep); } top--;}int main(){ scanf("%d%d",&n,&s); for(int i=1;i<=n;i++) scanf("%d",&sum[i]); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); add(x,y); } st[++top]=0; dfs(1,sum[1],0); printf("%d\n",ans);}
1 0
- BZOJ 2783 [JLOI2012]树
- 【BZOJ 2783】 [JLOI2012]树
- BZOJ 2783: [JLOI2012]树
- bzoj 2783树
- bzoj 2783: [JLOI2012]树
- BZOJ 2783: [JLOI2012]树
- bzoj 2783 [JLOI2012] 树 题解
- BZOJ 2783 JLOI2012 树 DFS
- BZOJ 2783 JLOI 2012 树 倍增+二分
- BZOJ 2783 JLOI 2012 树 搜索
- bzoj 2783: [JLOI2012]树 dfs+二分
- BZOJ 2783 JLOI2012 树 二分 + 倍增
- BZOJ 3674/BZOJ 3673 主席树
- bzoj 线段树专刊
- bzoj 1798 线段树
- BZOJ 3306: 树
- BZOJ 3697 树分治
- bzoj 3589 动态树
- make: *** [.tmp_vmlinux1] 错误 1
- linux内核链表学习出错
- 安卓系统:在SD卡上新建一个文件file.txt,并将经纬度和时间数据写入
- linux内核链表函数学习
- warning: function declaration isn't a prototype原因分析
- bzoj 2783树
- _IO, _IOR, _IOW, _IOWR 宏的用法与解析
- 海明校验码
- 小甲鱼PE详解之IMAGE_DOS_HEADER结构定义即各个属性的作用(PE详解01)
- JAVA基础【3.8】《Java核心技术1》Java的基本程序设计结构-控制流程
- 机器学习中距离和相似性度量方法
- 小甲鱼PE详解之IMAGE_NT_HEADERS结构定义即各个属性的作用(PE详解02)
- 小甲鱼PE详解之区块表(节表)和区块(节)(PE详解04)
- 小甲鱼PE详解之区块表(节表)和区块(节)续(PE详解05)