传送门

树上前缀和。

在树上找一条权值和为 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. 乐搏讲自动化测试-python语言特点及优缺点(5)

    相信小伙伴们都知道,随着软件测试行业的发展和进步自动化测试已经成为必然.在竞争日益激烈的市场环境中也是你升职加薪的利器. 所以,小编决定从今天起!将要系统.连续.高质量的持续更新「整套自动化测试」文章 ...

  2. 【BZOJ4009_洛谷3242】[HNOI2015] 接水果(整体二分)

    题目: 洛谷 3242 分析: 明确题意:在一棵树上给定若干权值为 \(w\) 的路径 \((u,v)\) (盘子),每次给定 \((a,b)\) (水果),询问所有满足 \((u,v)\) 被 \( ...

  3. ACM_蛇形矩阵

    蛇行矩阵 Time Limit: 4000/2000ms (Java/Others) Problem Description: 蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形. Input: ...

  4. vue+elementUI table篇

    1.table内容展示 <el-table stripe :key='tableKey' header-cell-class-name="bindonce" :data=&q ...

  5. 10046 trace and sql

    1.   SQLT 下载 从metalink上下载SQLT工具,参考文档 (以下大部分(SQL可以在sqlt\utl 目录下找到)) 1.1 SQLT 安装 SQLT安装在自己的schema SQLT ...

  6. Knockout应用开发指南(完整版) 目录索引(转)

    使用Knockout有一段时间了(确切的说从MIX11大会宣传该JavaScript类库以来,我们就在使用,目前已经在正式的asp.net MVC项目中使用),Knockout使用js代码达到双向绑定 ...

  7. js重写alert()弹窗

    //重写alertwindow.alert = function(str){ var alertFram = document.getElementById('alertFram'); var shi ...

  8. Ubuntu 16.04安装Kate文本编辑工具

    Kate支持很多语言,比如NASM,比SBL3低那么一点,但是比Gedit好. 安装: sudo apt-get install kate 启动: 额外配置: 1.安装Kwrite sudo apt- ...

  9. html5 页面音频

    1. html5 样式 <audio class="audioleft download" id="audVoice" type="audio/ ...

  10. servlet——web应用中路径问题

    target.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html ...