树形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. [RxJS] Throttling vs Debouncing

    DebounceTime: It's like delay, but passes only the most recent value from each burst of emissions. T ...

  2. swift2.0学习之拓展

    拓展:和oc的拓展方法功能差点儿相同.就是给已经存在的类,结构体.枚举,协议类型添加新的方法 拓展语法: 用extensionkeyword声明: extension SomeType { // ne ...

  3. ctags的基本操作总结

    ctags用法 说明: a.       ctags能够分析程序生成tags文件: b.         生成的tags文件,能够用 vi -t 查找结构体,数据类型,函数名所在位置.非常方便: ct ...

  4. 基于QT的多线程server

    // thread.cpp #include "thread.h" Thread::Thread(int socketDescriptor, QObject *parent) : ...

  5. mongodb 3.2配置内存缓存大小为MB/MongoDB 3.x内存限制配置

    mongodb 3.2配置内存缓存大小为MB/MongoDB 3.x内存限制配置 转载自勤奋的小青蛙 mongodb占用内存非常高,这是因为官方为了提升存储的效率,设计就这么设计的. 但是大部分的个人 ...

  6. JavaScript表格搜索高亮功能模拟

    在网页表格中模拟excle的搜索高亮显示功能.当在搜索框中输入需要的姓名时,若表格中存在对应的数据,则该表格背景色变为黄色. 下面为表的HTML源码: <!doctype html> &l ...

  7. El表达式日期处理

    第1步:引入指令  <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt " %&g ...

  8. Spring Data 自动生成

    之前一直用的mybatis逆向自动生成,由于最近学习springdata,所以看了一下springdata的自动生成,基本与mybatis一致,不同的也许就是逆向生成代码(实体类,mapper等)和正 ...

  9. ThinkPHP __PUBLIC__的定义 __ROOT__等 常用 常量的定义

    '__TMPL__' => APP_TMPL_PATH, // 项目模板目录 '__ROOT__' => __ROOT__, // 当前网站地址 '__APP__' => __APP ...

  10. 题解报告:hdu 1847 Good Luck in CET-4 Everybody!(入门SG值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1847 Problem Description 大学英语四级考试就要来临了,你是不是在紧张的复习?也许紧 ...