传送门

树上前缀和。

在树上找一条权值和为 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. bzoj 1691: [Usaco2007 Dec]挑剔的美食家【贪心+splay】

    高端贪心,好久没写splay调了好久-- 以下v为价格,w为鲜嫩度 把牛和草都按v排升序,扫草,首先把v小于等于当前草的牛都丢进splay,这样一来splay里全是可选的牛了,按w排序,然后贪心的为当 ...

  2. bzoj1076: [SCOI2008]奖励关(期望dp+状压dp)

    1076: [SCOI2008]奖励关 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2989  Solved: 1557[Submit][Statu ...

  3. Hadoop回收站及fs.trash参数详解

    前言: Linux系统里,个人觉得最大的不方便之一就是没有回收站的概念.rm -rf很容易造成极大的损失.而在Hadoop或者说HDFS里面,有trash(回收站)的概念,可以使得数据被误删以后,还可 ...

  4. spring controller接口中,用pojo对象接收页面传递的参数,发现spring在对pojo对象赋值时,有一定顺序的问题

    1.我的项目中的实体类都继承了基类entityBase,里面封装了分页的一些属性,pageindex.pagesize.pagerownum等. 2.思路是页面可以灵活的传递分页参数,比如当前页pag ...

  5. 每天学点Linux命令:倒叙打印文件第二行的前100个大写字母

    sed -n | rev 处理第二行             grep:提取大写字母   o: 不显示非结果  tr:删除换行   Cut:截取1-100个字符  rev:逆序 断断续续搞了好长时间. ...

  6. Windows 2008中部署dll到GAC

    两种方法: 1  gacutil.exe 2 直接拖动DLL到GAC (此种方式要关闭UAC,否则提示"Access is Denied")

  7. 树莓派+百度api实现人脸识别

    title: 树莓派+百度api实现人脸识别 tags: 树莓派 date: 2018-5-31 20:06:00 --- 树莓派对接百度api 我以前玩安卓的时候一直用的讯飞的平台和api,对于百度 ...

  8. opencv边缘滤波

    2018-03-0422:16:11 import cv2 as cv import numpy as np def bi_demo (image): print ("ceshi" ...

  9. mongo 3.4分片集群系列之四:搭建分片集群--哈希分片 + 安全 + 区域

    这个系列大致想跟大家分享以下篇章: 1.mongo 3.4分片集群系列之一:浅谈分片集群 2.mongo 3.4分片集群系列之二:搭建分片集群--哈希分片 3.mongo 3.4分片集群系列之三:搭建 ...

  10. 用rownum先排序后分页

    今天突然想到rownum可以解决分页问题,于是做了各种实验,找个几个文章,最后有了一定成果. 现有表tablename,含有字段showorder,要求提取showorder的第11行到20行数据. ...