树形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. [Cypress] Test Variations of a Feature in Cypress with a data-driven Test

    Many applications have features that can be used with slight variations. Instead of maintaining mult ...

  2. 【原创】TCP超时重传机制探索

    TCP超时重传机制探索 作者:tll (360电商技术) 1)通信模型 TCP(Transmission Control Protocol)是一种可靠传输协议.在传输过程中当发送方(sender)向接 ...

  3. 学习笔记——node.js

    node.js的作用在于,号称可以让服务器支持更多的连接.比如说,php + apche可以让服务器支持4000个并发连接,那么node.js + apche可以让服务器支持并发几万个. 为什么这么牛 ...

  4. https://www.threatminer.org/domain.php?q=blackschickens.xyz ——域名的信誉查询站点 还可以查IP

    https://www.threatminer.org/domain.php?q=blackschickens.xyz https://www.threatminer.org/host.php?q=6 ...

  5. [SDOI 2013] 直径

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=3124 [算法] 树的直径 [代码] #include<bits/stdc++. ...

  6. insufficient space

  7. linux随便贴贴

    在bin目录下进入mysql: ./mysql -uroot -p123456 update mysql.user set password=password('root') where user=' ...

  8. Cracking the Coding Interview 10.7

    Design an algorithm to find the kth number such that the only prime factors are 3,5 and 7 方法一: a[i]= ...

  9. POJ 2976 裸的01分数规划

    题意:给你n个数对(认为是a数组和b数组吧),从中取n-m个数对,如果选第i个数对,定义x[i]=1,求R=∑(a[i]*x[i])/∑(b[i]*x[i])取得最大值时R的值.输出R*100(保留到 ...

  10. ACM_写数字

    写数字 Time Limit: 2000/1000ms (Java/Others) Problem Description: 把由1开始的自然数依次写下来:123456789101112……,重新分组 ...