题意:

  一棵 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的更多相关文章

  1. hdu 5735 Born Slippy 暴力

    Born Slippy 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5735 Description Professor Zhang has a r ...

  2. HDU 5735 Born Slippy(拆值DP+位运算)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5735 [题目大意] 给出一棵树,树上每个节点都有一个权值w,w不超过216,树的根为1,从一个点往 ...

  3. HDU5735 : Born Slippy

    考虑DP,设$f[x]$表示最后一个是$x$时的最优解,则$f[x]=\max(f[y]+w[x]\ opt\ w[y])$,其中$y$是$x$的祖先. 注意到$w[i]<2^{16}$,那么将 ...

  4. Born Slippy (超大背包问题 + 树形DP)

    首先是需要我们知道的是假设又一条链给你让你求最大值,你会求吗?当然会,就是时间有点爆炸O(n2).那不行,要是如果我把到达每个点的最大值以及他对后面的贡献情况都求出来后放在数组里面,然后到了新的节点直 ...

  5. 2016 Multi-University Training Contest 2

    8/13 2016 Multi-University Training Contest 2官方题解 数学 A Acperience(CYD)题意: 给定一个向量,求他减去一个  α(>=0)乘以 ...

  6. 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 ...

  7. HDU 5768:Lucky7(中国剩余定理 + 容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description   When ?? was born, seven ...

  8. HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)

    Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...

  9. HDU 2454 Degree Sequence of Graph G(Havel定理 推断一个简单图的存在)

    主题链接:pid=2454">http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiya ...

随机推荐

  1. 在mac平台运行debug.exe

    最近准备学习操作系统,想先复习一下汇编语言.因为用的是mac,而看的汇编教材(<汇编语言>王爽)使用到DOS下的debug,在网上搜了一圈发现,mac 也可以模拟运行debug. 先到网上 ...

  2. apache 反向代理配置(ubuntu)

    1.配置apache2的站点文件 cd /etc/apache2/site-avaliable sudo vim edy.conf 具体配置如下: # 反向代理配置 # 监听所有80端口的访问 < ...

  3. 范围for语句 && 列表初始值&& 标准库函数begin和end

    范围for语句: 引入的意义:简化传统for的编写,主要用于遍历给定序列中的每个元素并对序列中的每个值执行某种操作,其语法形式是: for( 声明: 给定序列) { 执行的操作. } 其中,“给定序列 ...

  4. JQuery实现倒计时效果

    首先:引入jquery文件 <script type="text/javascript" src="http://www.cnblogs.com/Content/P ...

  5. 图文-水平垂直居中兼容ie6+

    图文-水平垂直居中兼容ie6+ 具体代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8&q ...

  6. python学习第四天 --字符编码 与格式化及其字符串切片

    字符编码 与格式化 第三天已经知道了字符串也是一种数据类型,但是,字符串比较特殊的是还有一个编码问题. 因为计算机只能处理数字,如果要处理文本,就必须先把文本转换为数字才能处理.最早的计算机在设计时采 ...

  7. Double Strings Solved Problem code: DOUBLE

    # Fuking silly, OTZ.... import sys def main(): n = int(raw_input()) for num in sys.stdin: if int(num ...

  8. python文件_批量改名

    #! /usr/bin/env python #coding=gbk #文件操作实例--将文件夹下所有图片名称加上'_test' import re,os,time #str.split(path) ...

  9. 在子jsp页面中调用父jsp中的function或父jsp调用子页面中的function

    项目场景: A.jsp中有一个window,window里嵌入了一个<iframe>,通过<iframe>引入了另一个页面B.jsp.在B.jsp中的一个function中需要 ...

  10. nyoj-366-D的小L(求全排列)

    D的小L 时间限制:4000 ms  |  内存限制:65535 KB 难度:2 描述       一天TC的匡匡找ACM的小L玩三国杀,但是这会小L忙着哩,不想和匡匡玩但又怕匡匡生气,这时小L给匡匡 ...