https://codeforces.com/contest/1059/problem/E

题意

给出一棵树,每个点都有一个权值,要求你找出最少条链,保证每个点都属于一条链,而且每条链不超过L个点 和 每条链的权值和不超过S

题解

  • 对于儿子来说,父亲节点只有一个,所以没有决策点。可以从下往上处理出,每个节点最远能爬到那个节点(过程就是倍增)
  • 然后从下往上贪 (选择往上走的远的子节点)
#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define N 100005
using namespace std;
vector<int>g[N];
int n,l,i,v,u,ans,f[N][25],d[N],top[N],pre[N];
ll s,w[N],sum[N]; void dfs(int u,int fa){
sum[u]=sum[fa]+w[u];
d[u]=d[fa]+1;
f[u][0]=fa;
for(i=1;i<=20;i++)f[u][i]=f[f[u][i-1]][i-1];
top[u]=u; int D=l;
for(i=20;i>=0;i--){
int v=f[top[u]][i];
if(v==0||(1<<i)>=D||sum[u]-sum[f[v][0]]>s)continue;
D-=(1<<i);
top[u]=v;
}
for(int i=0;i<g[u].size();i++){
int v=g[u][i];dfs(v,u);
}
} void dfs2(int u){
int bt=0;
for(int i=0;i<g[u].size();i++){
int v=g[u][i];
dfs2(v);
if(pre[v]==v)continue;
if(!bt||d[bt]>d[pre[v]])bt=pre[v];
}
if(!bt){ans++;bt=top[u];}
pre[u]=bt;
}
int main(){
cin>>n>>l>>s;
for(i=1;i<=n;i++){
scanf("%lld",&w[i]);
if(w[i]>s){cout<<-1;return 0;}
}
for(i=2;i<=n;i++){
scanf("%d",&v);
g[v].pb(i);
}
ans=0;
dfs(1,0);
dfs2(1);
cout<<ans<<endl;
}

Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)的更多相关文章

  1. Codeforces Round #514 (Div. 2)

    目录 Codeforces 1059 A.Cashier B.Forgery C.Sequence Transformation D.Nature Reserve(二分) E.Split the Tr ...

  2. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  3. Codeforces Round #319 (Div. 1) B. Invariance of Tree 构造

    B. Invariance of Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/576/ ...

  4. Codeforces Round #514 (Div. 2) D. Nature Reserve

    http://codeforces.com/contest/1059/problem/D 最大值: 最左下方和最右下方分别有一个点 r^2 - (r-1)^2 = (10^7)^2 maxr<0 ...

  5. Codeforces Round #514 (Div. 2):D. Nature Reserve(二分+数学)

    D. Nature Reserve 题目链接:https://codeforces.com/contest/1059/problem/D 题意: 在二维坐标平面上给出n个数的点,现在要求一个圆,能够容 ...

  6. Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)

    C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...

  7. Codeforces Round #567 (Div. 2)B. Split a Number (字符串,贪心)

    B. Split a Number time limit per test2 seconds memory limit per test512 megabytes inputstandard inpu ...

  8. Codeforces Round #567 (Div. 2) B. Split a Number

    Split a Number time limit per test 2 seconds memory limit per test 512 megabytes input standard inpu ...

  9. Codeforces Round #514 (Div. 2) C. Sequence Transformation 思维构造

    题意 给出一个1-n的集合   gcd 集合里面的所有数  得到的 一个 数   然后自己选择删去一个数   要使得到的数 构成的数列 的字典序最大 思路: gcd所有数 那gcd得到的数肯定要小于数 ...

随机推荐

  1. P3375 【模板】KMP字符串匹配

    P3375 [模板]KMP字符串匹配 https://www.luogu.org/problemnew/show/P3375 题目描述 如题,给出两个字符串s1和s2,其中s2为s1的子串,求出s2在 ...

  2. 在IDEA中使用MyBatis Generator逆向工程生成代码

    本文介绍一下用Maven工具如何生成Mybatis的代码及映射的文件. 一.配置Maven pom.xml 文件 在pom.xml增加以下插件: <build> <finalName ...

  3. Django具体操作(五)

    一.中间件的概念 中间件顾名思义,是介于request与response处理之间的一道处理过程,相对比较轻量级,并且在全局上改变django的输入与输出.因为改变的是全局,所以需要谨慎实用,用不好会影 ...

  4. 导出可运行jar包

    @参考文档 选中项目右击export 可运行jar文件 Extract required libraries into generated JAR:将所需库导出到导出的jar包根目录下,效果如下 Pa ...

  5. 交叉编译libudev

    一.交叉编译libudev下载udev-182.tar.xz 下载网址:https://mirrors.edge.kernel.org/pub/linux/utils/kernel/hotplug/ ...

  6. go语言中的反射reflect

    package main; import ( "fmt" "reflect" ) //反射refection //反射使用TypeOf和ValueOf函数从接口 ...

  7. Linux下实时查看GPU状态

    参考链接: http://blog.csdn.net/yao_yao_2015/article/details/53404389 1. 显示当前GPU使用情况 Nvidia自带了一个nvidia-sm ...

  8. Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools"

    https://blog.csdn.net/saucyj/article/details/79043443

  9. tomcat用虚拟目录方式发布项目

    conf/Catalina/localhost:指定项目的配置信息 1.添加:ROOT.xml 听见Context节点: <Context docBase="/usr/local/to ...

  10. android 线性布局

    activity_main.xml线性布局 <?xml version="1.0" encoding="utf-8"?> <LinearLay ...