树形DP.

用倍增处理出来每个点往上能延伸出去的最远路径,nlogn

对于每个节点,如果它能被后代使用过的点覆盖,就直接覆盖,这个点就不使用,否则就ans++,让传的Max改成dp[x]

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int N=;
int n,a[N],fa[N][],f[N],head[N],to[N<<],nxt[N<<],ecnt,ans,L;
inline void add(int bg,int ed) {
nxt[++ecnt]=head[bg];to[ecnt]=ed;head[bg]=ecnt;
}
ll S,v[N][];
void dfs(int x) {
for(int i=head[x];i;i=nxt[i])
if(to[i]!=fa[x][]) fa[to[i]][]=x,v[to[i]][]=a[x],dfs(to[i]);
}
int Dfs(int x) {
int Max=;
for(int i=head[x];i;i=nxt[i]) if(to[i]!=fa[x][]) Max=max(Max,Dfs(to[i]));
if(!Max) {
ans++;
return f[x]-;
}
return Max-;
}
int main() {
cin>>n>>L>>S;
for(int i=;i<=n;i++) {scanf("%d",&a[i]);if(a[i]>S) return puts("-1"),;}
for(int i=,x;i<n;i++) scanf("%d",&x),add(x,i+),add(i+,x);
dfs(); for(int i=;(<<i)<=L;i++) {
for(int j=;j<=n;j++) {
fa[j][i]=fa[fa[j][i-]][i-];
v[j][i]=v[fa[j][i-]][i-]+v[j][i-];
}
}
for(int i=;i<=n;i++) {
ll sum=a[i];int now=i;f[i]=;
for(int j=;~j&&sum<=S;j--) {
if(fa[now][j]&&sum+v[now][j]<=S&&f[i]+(<<j)<=L)
sum+=v[now][j],f[i]+=<<j,now=fa[now][j];
}
}
Dfs();
cout<<ans;
return ;
}

Split the Tree

[CodeForces1059E] Split the Tree的更多相关文章

  1. [Split The Tree][dfs序+树状数组求区间数的种数]

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB提交: 46  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...

  2. Split The Tree

    Split The Tree 时间限制: 1 Sec  内存限制: 128 MB 题目描述 You are given a tree with n vertices, numbered from 1 ...

  3. HDU6504 Problem E. Split The Tree【dsu on tree】

    Problem E. Split The Tree Problem Description You are given a tree with n vertices, numbered from 1 ...

  4. Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)

    https://codeforces.com/contest/1059/problem/E 题意 给出一棵树,每个点都有一个权值,要求你找出最少条链,保证每个点都属于一条链,而且每条链不超过L个点 和 ...

  5. [CF1059E]Split the Tree[贪心+树上倍增]

    题意 给定 \(n\) 个节点的树,点有点权 \(w\) ,划分成多条儿子到祖先的链,要求每条链点数不超过 \(L\) ,和不超过 \(S\),求最少划分成几条链. \(n\leq 10^5\) . ...

  6. Codeforces 1059E. Split the Tree

    题目:http://codeforces.com/problemset/problem/1059/E 用倍增可以在nlog内求出每个节点占用一个sequence 时最远可以向父节点延伸到的节点,对每个 ...

  7. CF1059E Split the Tree(倍增)

    题意翻译 现有n个点组成一棵以1为根的有根树,第i个点的点权为wi,需将其分成若干条垂直路径使得每一个点当且仅当被一条垂直路径覆盖,同时,每条垂直路径长度不能超过L,点权和不能超过S,求最少需要几条垂 ...

  8. Codeforces 461B. Appleman and Tree[树形DP 方案数]

    B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  9. CF461B Appleman and Tree (树DP)

    CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. Appleman and Tree time l ...

随机推荐

  1. [Angular] Using ngTemplateOutlet to create dynamic template

    I can use <tamplete> syntax and a entry component as a container to create a dynamic component ...

  2. mysql学习之四:sql语句学习2

    创建数据库: CREATE DATABASE stefan; 删除数据库: DROP DATABASE stefan; 重命名数据库: 重命名数据库没有直接的办法. 已经不再使用的方法: RENAME ...

  3. 《编程导论(Java)&#183;3.3.2 按值传递语义》

    不要受<Java编程思想>的影响,计算机科学中的术语--按引用传递(pass-by-reference).不要搞成自说自话的个人用语. 这些术语也不是专门针对Java的,你不应该从某一本J ...

  4. Wikioi 1081 线段树成段更新单点查询

    线段树练习飘逸的写法,自从自己改成这样的写法之后,线段树就没再练过,如今最终练得上了. 由于这里查询仅仅是查询了叶子结点,所以pushUp函数就用不上了,只是我没去掉之前是3ms.去掉之后反而变成4m ...

  5. DBI(i80)/DPI(RGB)/DSI【转】

    本文转载自:http://blog.csdn.net/liuxd3000/article/details/17437317 (1)DBI接口 A,也就是通常所讲的MCU借口,俗称80 system接口 ...

  6. LINQ Query Expressions

    https://msdn.microsoft.com/en-us/library/bb397676(v=vs.100).aspx Language-Integrated Query (LINQ) is ...

  7. 无损压缩算法历史——熵编码是最早出现的,后来才有Lzx这些压缩算法

    Lossless   Entropy type Unary Arithmetic Asymmetric Numeral Systems Golomb Huffman  Adaptive Canonic ...

  8. 0502 php-变量、常量

    变 量 1.变量必须以$开头,后面紧跟变量名. 2.注意以下情况: echo  “人民币符合¥像个羊,美元符号$象个钱”;        //“$象个钱”会被识别为变量 3.与js的不同: 不支持“只 ...

  9. C# 标准命名规范

    笔者从事开发多年,有这样一种感觉,查看一些开源项目,如Spring.Apache Common等源码是一件赏心悦目的事情,究其原因,无外两点:1)代码质量非常高:2)命名特别规范(这可能跟老外的英语水 ...

  10. IO流遍历文件夹下所有文件问题

    import java.io.File; /** * @author 王恒 * @datetime 2017年4月20日 下午2:24:32 * @description 递归调用 * */ publ ...