Codeforces Round #514 (Div. 2) E. Split the Tree(倍增+贪心)
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(倍增+贪心)的更多相关文章
- Codeforces Round #514 (Div. 2)
目录 Codeforces 1059 A.Cashier B.Forgery C.Sequence Transformation D.Nature Reserve(二分) E.Split the Tr ...
- 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 ...
- 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/ ...
- 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 ...
- Codeforces Round #514 (Div. 2):D. Nature Reserve(二分+数学)
D. Nature Reserve 题目链接:https://codeforces.com/contest/1059/problem/D 题意: 在二维坐标平面上给出n个数的点,现在要求一个圆,能够容 ...
- Codeforces Round #514 (Div. 2) C. Sequence Transformation(递归)
C. Sequence Transformation 题目链接:https://codeforces.com/contest/1059/problem/C 题意: 现在有1~n共n个数,然后执行下面操 ...
- 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 ...
- 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 ...
- Codeforces Round #514 (Div. 2) C. Sequence Transformation 思维构造
题意 给出一个1-n的集合 gcd 集合里面的所有数 得到的 一个 数 然后自己选择删去一个数 要使得到的数 构成的数列 的字典序最大 思路: gcd所有数 那gcd得到的数肯定要小于数 ...
随机推荐
- js中json知识点
首先,json是一种数据格式,而不能说是一种对象(object).这一点是非常重要的. 起源是不同的语言中数据对象的形式是不一样的,我们为了在不同的语言中传递数据,发明了一种json格式用于消除这种差 ...
- os & sys
os os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell下cd os.c ...
- http协议的学习
TPC/IP协议是传输层协议,主要解决数据如何在网络中传输,而HTTP是应用层协议,主要解决如何包装数据.
- 编程,计算data段中的第一组数据的3次方,结果保存在后面一组dword单元中
assume cs:code data segment dw ,,,,,,, dd ,,,,,,, data ends code segment start: mov ax,data mov ds,a ...
- CSS-解决苹果点击高亮、安卓select灰色背景(select下拉框在IOS中背景变黑、出现阴影问题)
1.在苹果手机上,用点击事件后会出现一个高亮的阴影: 面对click事件的阴影,解决办法: *{ -webkit-tap-highlight-color: rgba(0,0,0,0); -webkit ...
- 完全卸载jdk
完全卸载jdk 如(卸载jdk1.7.0_80), 当卸载jdk时出现删除信息不全,导致无法安装,可通过此方法实现完全卸载. 1.打开系统运行输入regedit(注册表)分别查找以下路径 (1) ...
- 从django的序列化到rest-framework 序列化
1.利用Django的view实现返回json数据 from django.views.generic import View from goods.models import Goods class ...
- Informatica_(3)组件
一.Informatica介绍Informatica PowerCenter 是Informatica公司开发的世界级的企业数据集成平台,也是业界领先的ETL工具.Informatica PowerC ...
- PAT 1009 说反话 (20)(代码)
1009 说反话 (20)(20 分) 给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出. 输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串.字符串由若干单词和若干空 ...
- andorid UI事件 监听器
gridlayout.xml <?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns ...