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得到的数肯定要小于数 ...
随机推荐
- [剑指Offer]6-从尾到头打印链表
典型的后进先出,可以借助栈,也可以使用递归. 考虑到若链表过长递归可能造成函数调用栈溢出,所以使用栈更好. 注意stack无遍历操作,全部用push(),pop(),top()完成. 以下创建列表胡乱 ...
- Python: PySide(PyQt)QMessageBox按钮显示中文
习惯了Delphi.c#调用系统MessageBox本地化显示,待用PySide调用时,Qt原生提示对话框,默认以英文显示. 如何本地化呢? 参考些资料,加以摸索,实现所需效果.并可根据此思路,设计自 ...
- Django的rest_framework的视图之Mixin类编写视图源码解析
Mixin类编写视图 我们这里用auther表来做演示,先为auther和autherdetail写2个url url(r'^autherdetail/(?P<id>\d+)', view ...
- tableView与导航栏的偏移问题
//方法1.关闭自动布局 self.automaticallyAdjustsScrollViewInsets = NO; //方法2.改变相对于scrollview的位置 //contentInset ...
- golang语言中os包的学习与使用(文件,目录,进程的操作)
os中一些常用函数的使用: package main; import ( "os" "fmt" "time" "strings&q ...
- Solidity语言基础 和 Etherum ERC20合约基础
1. 类型只能从第一次赋值中推断出来,因此以下代码中的循环是无限的, 小. for (var i = 0; i < 2000; i++) { ... } --- Solidity Types ...
- [Z]sql优化
前言:平常写的SQL可能主要以实现查询出结果为主,但如果数据量一大,就会突出SQL查询语句优化的性能独特之处.一般的数据库设计都会建索引查询,这样较全盘扫描查询的确快了不少.下面总结下SQL查询语句的 ...
- socket错误代码
Socket error 0 - Directly send error Socket error 10004 - Interrupted function call一个封锁操作被对 WSACance ...
- tomcat用虚拟目录方式发布项目
conf/Catalina/localhost:指定项目的配置信息 1.添加:ROOT.xml 听见Context节点: <Context docBase="/usr/local/to ...
- 第一个android ijkplayer播放器
创建一个ijkplayer的播放器项目,需要三步设置: 一.在activity_main.xml中添加播放器标签 <com.smallart.myapplication.media.IjkVid ...