[CodeForces1059E] Split the Tree
树形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的更多相关文章
- [Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...
- Split The Tree
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB 题目描述 You are given a tree with n vertices, numbered from 1 ...
- 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 ...
- Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)
https://codeforces.com/contest/1059/problem/E 题意 给出一棵树,每个点都有一个权值,要求你找出最少条链,保证每个点都属于一条链,而且每条链不超过L个点 和 ...
- [CF1059E]Split the Tree[贪心+树上倍增]
题意 给定 \(n\) 个节点的树,点有点权 \(w\) ,划分成多条儿子到祖先的链,要求每条链点数不超过 \(L\) ,和不超过 \(S\),求最少划分成几条链. \(n\leq 10^5\) . ...
- Codeforces 1059E. Split the Tree
题目:http://codeforces.com/problemset/problem/1059/E 用倍增可以在nlog内求出每个节点占用一个sequence 时最远可以向父节点延伸到的节点,对每个 ...
- CF1059E Split the Tree(倍增)
题意翻译 现有n个点组成一棵以1为根的有根树,第i个点的点权为wi,需将其分成若干条垂直路径使得每一个点当且仅当被一条垂直路径覆盖,同时,每条垂直路径长度不能超过L,点权和不能超过S,求最少需要几条垂 ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CF461B Appleman and Tree (树DP)
CF462D Codeforces Round #263 (Div. 2) D Codeforces Round #263 (Div. 1) B B. Appleman and Tree time l ...
随机推荐
- Linux网络编程——浅谈 TCP 三次握手和四次挥手
一.tcp协议格式 二.三次握手 在 TCP/IP 协议中.TCP 协议提供可靠的连接服务,採用三次握手建立一个连接. 第一次握手:建立连接时,client发送 syn 包(tcp协议中syn位置1. ...
- UVA 12683 Odd and Even Zeroes(数学—找规律)
Time Limit: 1000 MS In mathematics, the factorial of a positive integer number n is written as n! an ...
- mongoDB学习笔记——在C#中查询
1.下载安装 想要在C#中使用MongoDB,首先得要有个MongoDB支持的C#版的驱动.C#版的驱动貌似有很多种,如官方提供的samus. 实现思路大都类似.这里我们用官方提供的mongo-csh ...
- JS容易犯错的this和作用域
var someuser = { name: 'byvoid', func: function() { console.log(this.name); } }; var foo = { name: ' ...
- FileZilla Client 免费又好用的ftp工具
设置文件同步关联(这个功能很好用) 很好用,很方便! 防止掉线 编辑->设置
- 2017 Multi-University Training Contest - Team 2 &&hdu 6050 Funny Function
Funny Function Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- [SDOI2007]游戏
https://zybuluo.com/ysner/note/1184420 题面 题意简单,但不太好概括. 戳我 解析 不成熟想法 据题意可知,字符串字符的顺序无影响. 并且判断两个字符串能否接龙可 ...
- Webpack 2.0 的文档
Webpack 2.0 的文档 https://webpack.js.org/get-started/
- Python 44 前端概述 、三剑客 、常用标签与分类
1.前端三剑客是哪三位?文件的后缀内容?在前端开发中的功能是什么? HTML: .htm .html 内容 CSS: .css 效果 JS: .js 行为 2.简述三剑客的主要 ...
- php 图片生成器
一.需求 最近公司由于有大量的海报要做,而且海报的布局规模都是一样的,只是内容不同,所以老板想我开发一个图片的生成器.可以根据你输入的内容生成海报图片. 具体有需求有以下的需求 1.可以根据将每条数据 ...