题意翻译

现有n个点组成一棵以1为根的有根树,第i个点的点权为wi,需将其分成若干条垂直路径使得每一个点当且仅当被一条垂直路径覆盖,同时,每条垂直路径长度不能超过L,点权和不能超过S,求最少需要几条垂直路径才能满足要求。特别地,无解输出-1。

一条垂直路径是一条包含v1, v2...vk的路径,使得vi(i>=2)是vi-1的父亲。

题解

话说莫不是树上的题目全都可以用倍增艹过去……

首先,这个东西是可以贪心的,我们每一次一定是选取子树里能向上拓展最多的点与自己连成一条链

然后每一个节点最多能向上拓展多少可以用倍增预处理出来

然后直接在dfs的时候判断即可

一开始也想到要贪心了……然而最后倍增那一步没想到233

 //minamoto
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=;
int head[N],Next[N],ver[N],tot;
inline void add(int u,int v){
ver[++tot]=v,Next[tot]=head[u],head[u]=tot;
}
int pa[N][],top[N],son[N],dep[N],ans,n,L;ll sum[N],w[N],S;
inline void init(int u){
for(int i=;i<=;++i)
pa[u][i]=pa[pa[u][i-]][i-];
int dis=L,tmp=u;
for(int i=;i>=;--i){
int fa=pa[tmp][i];
if(!fa||(<<i)>=dis) continue;
if(sum[u]-sum[fa]+w[fa]>S) continue;
dis-=(<<i),top[u]=fa,tmp=fa;
}
}
void dfs1(int u,int fa){
sum[u]=sum[fa]+w[u],top[u]=u,dep[u]=dep[fa]+,init(u);
for(int i=head[u];i;i=Next[i])
dfs1(ver[i],u);
}
void dfs2(int u){
int mx=-;
for(int i=head[u];i;i=Next[i]){
int v=ver[i];dfs2(v);
if(son[v]==v) continue;
if(mx==-||dep[mx]>dep[son[v]]) mx=son[v];
}
if(mx==-) ++ans,mx=top[u];
son[u]=mx;
}
int main(){
// freopen("testdata.in","r",stdin);
scanf("%d%d%lld",&n,&L,&S);
for(int i=;i<=n;++i){
scanf("%lld",&w[i]);
if(w[i]>S) return puts("-1"),;
}
for(int i=,f;i<=n;++i)
scanf("%d",&f),add(f,i),pa[i][]=f;
dfs1(,),dfs2();
printf("%d\n",ans);
return ;
}

CF1059E Split the Tree(倍增)的更多相关文章

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

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

  2. [CodeForces1059E] Split the Tree

    树形DP. 用倍增处理出来每个点往上能延伸出去的最远路径,nlogn 对于每个节点,如果它能被后代使用过的点覆盖,就直接覆盖,这个点就不使用,否则就ans++,让传的Max改成dp[x] #inclu ...

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

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

  4. Split The Tree

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

  5. 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 ...

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

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

  7. 2018 ICPC青岛网络赛 B. Red Black Tree(倍增lca好题)

    BaoBao has just found a rooted tree with n vertices and (n-1) weighted edges in his backyard. Among ...

  8. CF 208E. Blood Cousins [dsu on tree 倍增]

    题意:给出一个森林,求和一个点有相同k级祖先的点有多少 倍增求父亲然后和上题一样还不用哈希了... #include <iostream> #include <cstdio> ...

  9. Codeforces 1140G Double Tree 倍增 + dp

    刚开始, 我以为两个点肯定是通过树上最短路径过去的, 无非是在两棵树之间来回切换, 这个可以用倍增 + dp 去维护它. 但是后来又发现, 它可以不通过树上最短路径过去, 我们考虑这样一种情况, 起点 ...

随机推荐

  1. leetcode题解||Container With Most Water问题

    problem: Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...

  2. 走入asp.net mvc不归路:[3]创建控制器

    实际上,控制器就是一个类,一个继承自Controller的类.正常创建一个Controller即可,而问题在于asp.net mvc提供了一套便捷的方法,在创建一个Controller时,可以自动创建 ...

  3. poj1703 Find them,Catch them 【并查集】

    做过一些的带权并查集,再来做所谓的"种类并查集",发现好像就顿悟了. 种类并查集与带权并查集实质上的区别并不大. 关键的区别就是种类并查集仅仅是带权并查集再弄个%取余操作而已.然后 ...

  4. openwrt: sysupgrade

    sysupgrade 用法: sysupgrade [...] sysupgrade [-q] [-i] 升级选项: -d 重启前等待 delay 秒 -f 从 .tar.gz (文件或链接) 中恢复 ...

  5. Android — 长按ListView 利用上下文菜单(ActionMode) 进行批量事件处理

    好久没写博客拉``````` 近期最终略微闲一点了``````` 无聊拿手机清理短信.发现批量事件的处理还是挺管用的`````` 那么自己也来山寨一记看看效果吧````` 闲话少说,首先,我们来看下手 ...

  6. object-c iOS 教程 git for mac

    本文转载至 http://blog.csdn.net/u011728347/article/details/10035191   http://rypress.com/tutorials/object ...

  7. log Configuration

    Log4j – Configuring Log4j 2 - Apache Log4j 2 https://logging.apache.org/log4j/2.x/manual/configurati ...

  8. [rabbitmq-discuss] Exactly Once Delivery

    [rabbitmq-discuss] Exactly Once Delivery http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/2010-A ...

  9. Kafka在Windows安装运行

    摘要:本文主要说明了如何在Windows安装运行Kafka 一.安装JDK 过程比较简单,这里不做说明. 最后打开cmd输入如下内容,表示安装成功 二.安装zooeleeper 下载安装包:http: ...

  10. Unity5.6打包问题

    将unity切换到5.6版本后打Android包时,提示android sdk tools version低于compile version,于是更新了android-sdk(下载了Android-S ...