HDU 5735 - Born Slippy
题意:
一棵 n 个节点的根树,i 节点权重 wi
对每一个节点s,找到这样一个长 m 的标号序列 v :
1. vi是vi-1 的祖先
2. f[s] = w[vi] + ∑(i=2, m) (w[vi] opt w[vi-1]) 最大
要求输出:S = ∑(i=1, n) (i * f[i]) (mod 1e9 + 7)
opt给出,为 & , ^ , | 的任意一种
数据范围: 2<= n <= 2^16 , 2 <= wi <= 2^16
分析:
普通转移方程: DP[i] = max(DP[j] + w[i] opt w[j] ), j 为 i 的祖先。
折半:
将权值的高低八位拆分: w[i] = (ai<<8) + bi
所以转移方程变式: DP[i] = max(DP[j] + (ai opt aj)*(1<<8) + (bi opt bj) ), j 为 i 的祖先。
再拆 tmp[ajbi] = max(DP[j] + (bi opt bj) ) bi 为 j 点的某个子孙节点的低八位
DP[i] = max( tmp[ajbi]+ ( (ai opt aj)<<8) ) ) aj 为 i 点的某个祖父节点的高八位
枚举:
辅助数组 F[a][b] 表示某点权值(不管哪个点)低八位为 b 时的所有权值高八位为 a 的祖先 j 中 DP[j] + (bi opt bj) 的最大值
即 F[a][b] = max (DP[j] + (bi opt bj) ) , w[j]>>8 = a .
对于每个 i , w[i] = (ai<<8) + bi , 枚举 F[x][bi] , DP[i] = max(F[x][bi] + (x opt ai) ) , 0 <= x <= 2^8 - 1 且 x为祖先高八位.
之后再更新 F[ai][x] = max (DP[i] + (x opt bi) ) ,0 <= x <= 2^8 - 1 .
用DFS就可以套在树上。
注意要还原
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define LL long long
const int MOD = 1e9+;
const int MAXN = <<+;
LL f[<<+][<<+],w[MAXN],tmp[MAXN][<<+];//tmp:回溯
LL ans;
int n,fa[<<+];//fa:是否为祖先
char op[];
vector<int> g[MAXN];
LL opt(LL a,LL b)
{
if (op[]=='A') return a&b;
else if (op[]=='X') return a^b;
else return a|b;
}
void DFS(int x)
{
int a = w[x] >> ,b = w[x] & ;
LL DPx=;
for (int i = ;i <= ; i++) tmp[x][b] = f[a][b];
for (int i = ;i <= ; i++) if(fa[i]) DPx = max(DPx, f[i][b] + ( opt(a,i)<< ) );
fa[a]++;
ans = (ans + x * ( w[x] + DPx ) ) % MOD;
for (int i=;i<g[x].size();i++) DFS(g[x][i]);
for (int i = ;i <= ; i++) f[a][b] = tmp[x][b];//回溯
}
int main()
{
int t;
scanf("%d",&t);
while (t--)
{
scanf("%d%s",&n,op);
for (int i=;i<=n;i++) g[i].clear();
for (int i=;i<=n;i++) scanf("%lld",&w[i]);
memset(f,,sizeof(f));
memset(fa,,sizeof(fa));
for (int i=;i<=n;i++)
{
int x;
scanf("%d",&x);
g[x].push_back(i);
}
DFS();
printf("%lld\n",ans);
}
}
HDU 5735 - Born Slippy的更多相关文章
- hdu 5735 Born Slippy 暴力
Born Slippy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5735 Description Professor Zhang has a r ...
- HDU 5735 Born Slippy(拆值DP+位运算)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5735 [题目大意] 给出一棵树,树上每个节点都有一个权值w,w不超过216,树的根为1,从一个点往 ...
- HDU5735 : Born Slippy
考虑DP,设$f[x]$表示最后一个是$x$时的最优解,则$f[x]=\max(f[y]+w[x]\ opt\ w[y])$,其中$y$是$x$的祖先. 注意到$w[i]<2^{16}$,那么将 ...
- Born Slippy (超大背包问题 + 树形DP)
首先是需要我们知道的是假设又一条链给你让你求最大值,你会求吗?当然会,就是时间有点爆炸O(n2).那不行,要是如果我把到达每个点的最大值以及他对后面的贡献情况都求出来后放在数组里面,然后到了新的节点直 ...
- 2016 Multi-University Training Contest 2
8/13 2016 Multi-University Training Contest 2官方题解 数学 A Acperience(CYD)题意: 给定一个向量,求他减去一个 α(>=0)乘以 ...
- 2016 Multi-University Training Contest 2 solutions BY zimpha
Acperience 展开式子, \(\left\| W-\alpha B \right\|^2=\displaystyle\alpha^2\sum_{i=1}^{n}b_i^2-2\alpha\su ...
- HDU 5768:Lucky7(中国剩余定理 + 容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description When ?? was born, seven ...
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)
主题链接:pid=2454">http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiya ...
随机推荐
- CI框架uri去掉index.php
CI框架的入口是index.php,所以url实际上要多出一个index.php,非常不美观.我使用的是apache服务器,要开启mod_rewrite服务才可以. sudo a2enmod rewr ...
- BasicExcel说明文档
BasicExcel说明文档 BasicExcel原始链接:http://www.codeproject.com/Articles/13852/BasicExcel-A-Class-to-Read-a ...
- linq学习三个实例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- IOS响应式编程框架ReactiveCocoa(RAC)使用示例-备
ReactiveCocoa是响应式编程(FRP)在IOS中的一个实现框架,它的开源地址为:https://github.com/ReactiveCocoa/ReactiveCocoa# :在网上看了几 ...
- #include<iostream>与#include<iostream.h>的区别
转载于祝长洋的BLOG:http://blog.sina.com.cn/s/blog_514b5f600100ayks.h ...
- Effective Java实作hashCode() - 就是爱Java
hashCode()这个方法,也是定义在Object class中,这个是所有class的base class,因此所有的class也都继承这个方法,预设是传回这个对象储存的内存地址编号,因为Mix覆 ...
- windows下编译firefox
可以自己定制下.估计很简单..... 官方文档扫一遍: https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Build_I ...
- MS Build参考
以下链接很详细的讲述了Build方面相关的知识,顺带连Link的参数都解释了,以后不知道就来这里查一查了. http://msdn.microsoft.com/zh-CN/library/ee8624 ...
- haproxy 负载elasticsearch 切换
Attempted to send a bulk request to Elasticsearch configured at '["http://192.168.32.152:9200&q ...
- centos 6.5 hadoop 2.3 初配置
为了安装hadoop废了好大的劲才把esxi5.5给装好. 同时装了centos6.5,由于hadoop里面有个免密码登陆所以这里讲的就是免密码登陆. 看了大家的博客文章发现转发的一部分,写ubunt ...