博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
No Link, Cut Tree! Gym - 101484F(dp)
阅读量:4700 次
发布时间:2019-06-09

本文共 1021 字,大约阅读时间需要 3 分钟。

dp[i][j]代表在i为根 深度为j时的价值河

所以 dp[i][j]=dp[i*2][j-1]+dp[i*2+1][j-1]
重新标一下号 开始跑转移方程即可

#include
#define inf 0x3f3f3f3fusing namespace std;const int maxn=5e5+10;int dp[maxn][35],D[maxn];int has[maxn],v[maxn];struct node{ int l,r;}tree[maxn];int d=0,tot=0;void dfs(int now,int u,int dep){ if(u==-1) return ; d=max(d,dep); tot=max(tot,now); D[now]=dep; has[u]=now; dp[now][1]=v[u]; dfs(now*2,tree[u].l,dep+1); dfs(now*2+1,tree[u].r,dep+1);}int main(){ int n,m; cin>>n>>m>>v[1]; for(int i=1;i<=n;i++) tree[i].l=tree[i].r=-1; for(int i=1;i
>son>>f;cin>>v[son]; if(tree[f].l==-1) tree[f].l=son; else tree[f].r=son; } dfs(1,1,1); for(int i=tot;i>=1;i--) { for(int j=d;j>=2;j--) { dp[i][j]=dp[i*2][j-1]+dp[i*2+1][j-1]; } } while(m--) { int now,ans=0; cin>>now; now=has[now]; for(int i=1;i<=d;i++) { if(i

转载于:https://www.cnblogs.com/minun/p/11600470.html

你可能感兴趣的文章
python学习0day
查看>>
课堂练习之检测水军
查看>>
显示和隐藏选项卡
查看>>
函数指针的使用
查看>>
自动化测试的准备
查看>>
E07【餐厅】What would you recommend?
查看>>
HTML基础标签
查看>>
位图数据结构的操作
查看>>
python之路--day23--面向对象高级
查看>>
azkaban用户管理及权限配置
查看>>
GCD学习笔记
查看>>
[转]Asp.net MVC中的ViewData与ViewBag
查看>>
PHP......会话控制SESSION与COOKIE
查看>>
[转]AchartEngineActivity引擎绘制柱状图、曲线图
查看>>
[转]javascript实现限制上传文件的大小
查看>>
2-SAT问题的算法
查看>>
2012年,将是HTML5决胜的一年
查看>>
DOM的事件操作
查看>>
利用powerdesigner创建数据库
查看>>
js 中数组的遍历
查看>>