Description

在这个问题中,给定一个值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。

题解

遍历一遍,将根到当前节点路径上的所有的点的树上前缀和压入栈中。

查找时二分栈即可。

 //It is made by Awson on 2017.9.29
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = ;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
} int n, s, u, v;
int val[N+];
int sum[N+], top;
struct tt {
int to, next;
}edge[N+];
int path[N+], tot;
int ans; void add(int u, int v) {
edge[++tot].to = v;
edge[tot].next = path[u];
path[u] = tot;
}
bool erge(int l, int r) {
while (l <= r) {
int mid = (l+r)>>;
if (sum[top]-sum[mid] == s) return true;
if (sum[top]-sum[mid] > s) l = mid+;
else r = mid-;
}
return false;
}
void dfs(int u) {
sum[++top] = sum[top-]+val[u];
ans += erge(, top);
for (int i = path[u]; i; i = edge[i].next)
dfs(edge[i].to);
top--;
}
void work() {
read(n), read(s);
for (int i = ; i <= n; i++) read(val[i]);
for (int i = ; i < n; i++)
read(u), read(v), add(u, v);
dfs();
printf("%d\n", ans);
}
int main() {
work();
return ;
}

最新文章

  1. spark统计
  2. Python学习笔记——Day2
  3. Android中asset文件夹和raw文件夹区别
  4. alpha阶段总结 说明
  5. SQLITE 时间字段操作函数
  6. Linux下查看文件和文件夹大小(转)
  7. 【英语】Bingo口语笔记(4) - Pick系列
  8. bzoj1997: [Hnoi2010]Planar
  9. 【Android Developers Training】 65. 应用投影和相机视图
  10. Dynamic web module 版本之间的区别
  11. C++ 头文件系列(ostream)
  12. bzoj 4916: 神犇和蒟蒻 (杜教筛+莫比乌斯反演)
  13. Java NIO 概览
  14. CSS white-space属性详解
  15. Viterbi algorithm
  16. C# zip -ICSharpCode.SharpZipLib
  17. Golang 的 协程调度机制 与 GOMAXPROCS 性能调优
  18. RowFilter遇上特殊字符*%&#39;[]\
  19. MongoDB ,cursor not found异常
  20. Redis debugging guide---官方

热门文章

  1. Alpha冲刺No.1
  2. 项目Beta预备
  3. 树莓派3启动wifi并且配置wifi
  4. 使用SecureCRTP 连接生产环境的web服务器和数据库服务器
  5. Flask Markup 上下文,request
  6. VS系列控制台闪退解决
  7. Hibernate之HQL
  8. 织梦cms网上复制图片不可用的解决方法
  9. python django的ManyToMany简述
  10. Python内置函数(46)——format