传送门

树上前缀和。

在树上找一条权值和为 s 的链,其中这个链上的点按深度递增(递减)(不同)

dfs

每搜到一个点求它的前缀和 sum[x],放入 set 中。

在 set 中找 sum[x] - s 的点,如果有,ans++

退出 dfs 的时候再把 sum[x] 从 set 中删除

因为每个点权都是正整数,所以 set 中没有重复元素。

同时也是单调递增,所以简单些不用 set,开个数组再 lower_bound 也行。

——代码

 #include <set>
#include <cstdio>
#include <cstring>
#include <iostream> const int MAXN = ;
int n, s, cnt, ans;
int a[MAXN], head[MAXN], to[MAXN << ], next[MAXN << ], f[MAXN], sum[MAXN];
std::set <int> S; inline int read()
{
int x = , f = ;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -;
for(; isdigit(ch); ch = getchar()) x = (x << ) + (x << ) + ch - '';
return x * f;
} inline void add(int x, int y)
{
to[cnt] = y;
next[cnt] = head[x];
head[x] = cnt++;
} inline void dfs(int u)
{
sum[u] = sum[f[u]] + a[u];
S.insert(sum[u]);
if(S.count(sum[u] - s)) ans++;
for(int i = head[u]; i ^ -; i = next[i]) dfs(to[i]);
S.erase(sum[u]);
} int main()
{
int i, x, y;
n = read();
s = read();
memset(head, -, sizeof(head));
for(i = ; i <= n; i++) a[i] = read();
for(i = ; i < n; i++)
{
x = read();
y = read();
f[y] = x;
add(x, y);
}
S.insert();
dfs();
printf("%d\n", ans);
return ;
}

[luoguP3252] [JLOI2012]树(DP)的更多相关文章

  1. CF456D A Lot of Games (字典树+DP)

    D - A Lot of Games CF#260 Div2 D题 CF#260 Div1 B题 Codeforces Round #260 CF455B D. A Lot of Games time ...

  2. BZOJ2783: [JLOI2012]树 dfs+set

    2783: [JLOI2012]树 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 588  Solved: 347 Description 数列 提交文 ...

  3. HDU4916 Count on the path(树dp??)

    这道题的题意其实有点略晦涩,定义f(a,b)为 minimum of vertices not on the path between vertices a and b. 其实它加一个minimum ...

  4. Codeforces 219D. Choosing Capital for Treeland (树dp)

    题目链接:http://codeforces.com/contest/219/problem/D 树dp //#pragma comment(linker, "/STACK:10240000 ...

  5. HDU4276 The Ghost Blows Light SPFA&&树dp

    题目的介绍以及思路完全参考了下面的博客:http://blog.csdn.net/acm_cxlove/article/details/7964739 做这道题主要是为了加强自己对SPFA的代码的训练 ...

  6. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  7. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. 2783: [JLOI2012]树( dfs + BST )

    直接DFS, 然后用set维护一下就好了.... O(nlogn) ------------------------------------------------------------------ ...

  9. bzoj 3572世界树 虚树+dp

    题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...

随机推荐

  1. SSH协议、HTTPS中SSL协议的完整交互过程

    1.(SSH)公私钥认证原理 服务器建立公钥:每一次启动sshd服务时,该服务会主动去找/etc/ssh/ssh_host*的文件 客户端通过ssh工具进行连接,如Xshell,SecureCRT 服 ...

  2. 384 Shuffle an Array 打乱数组

    打乱一个没有重复元素的数组.示例:// 以数字集合 1, 2 和 3 初始化数组.int[] nums = {1,2,3};Solution solution = new Solution(nums) ...

  3. 306 Additive Number 加法数

    Additive number is a string whose digits can form additive sequence.A valid additive sequence should ...

  4. sublime 3 最新注册码

    http://9iphp.com/web/html/sublime-text-3-license-key.html

  5. [转]ASP.NET MVC中实现多个按钮提交的几种方法

    本文转自:http://www.cnblogs.com/wuchang/archive/2010/01/29/1658916.html 有时候会遇到这种情况:在一个表单上需要多个按钮来完成不同的功能, ...

  6. [转]从数据到代码——基于T4的代码生成方式

    本文转自:http://www.cnblogs.com/artech/archive/2010/10/23/1859529.html 在之前写一篇文章<从数据到代码>(上篇.下篇)中,我通 ...

  7. datagrid 选中某行,翻页再翻回来,发现选中的行没有选中

    不管有没有设置复选框,其实都是一样的,都是idField属性没有设置,加上去即可. $(function(){ $('#dg').datagrid({ url:'ContactServlet', to ...

  8. 第2章 JavaScript语法

    1.最好的做法是把<script>标签放到html文档的最后,</body>标签之前. 举例: ...... <script src="file.js" ...

  9. get、post、put、delete、head请求方式

    对资源的增,删,改,查操作,其实都可以通过GET/POST完成,不一定要用PUT和DELETE. 一:Jersey框架,实现了restful风格,常用的注解@GET.@POST.@PUT.@DELET ...

  10. PHP开发心得四

    1,php返回给html页面的Json数据不能含有回车符 某次用php编写查询数据库数据,以json格式返回给前端页面js文件,js文件以angularJS的函数调用处理的方式进行数据显示,但数据返回 ...