题意:

  一棵 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. 1236 hdu排名

    Problem Description 今天的上机考试虽然有实时的Ranklist,但上面的排名只是根据完成的题数排序,没有考虑 每题的分值,所以并不是最后的排名.给定录取分数线,请你写程序找出最后通 ...

  2. C++中string类的基本用法

    #include <iostream> #include <set> using namespace std; int main() { string line; getlin ...

  3. 【劳动节江南白衣Calvin 】我的后端开发书架2015

    自从技术书的书架设定为”床底下“之后,又多了很多买书的空间.中国什么都贵,就是书便宜. 不定期更新,在碎片化的阅读下难免错评. 书架主要针对Java后端开发,书单更偏爱那些能用简短流畅的话,把少壮不努 ...

  4. js学习(一)

    在javascript中,哪些值能作为if的条件呢? 1.布尔变量true/false 2.数字 非0数值为true, 0 或NaN为false. 3.对象 对象为null或undefined为fal ...

  5. 帝国cms7.0整合百度编辑器ueditor教程

    帝国cms7.0整合百度编辑器ueditor教程开始 1.根据自己使用的帝国cms版本编码下载对应的ueditor版本 下载地址 http://ueditor.baidu.com/website/do ...

  6. ORA-19502: write error on file "", blockno (blocksize=)/linux下磁盘空间满了解决办法--Virtualbox

    今天,在测试环境启动数据库时,报错: SQL> startup; ORACLE instance started. Total System Global Area  285212672 byt ...

  7. 伪造队形(FFT)

    题目描述Tukkun带着他的合唱队去环形音乐厅参加演出.上场前,Tukkun发现了严重的问题:音乐厅的工作人员把他们的合唱队形搞错了.具体来说,Tukkun的合唱队有N个人围成一圈,身高按照顺时针顺序 ...

  8. LeetCode_Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...

  9. Delphi中ShellExecute使用详解(详细解释10种显示状态)

    有三个API函数可以运行可执行文件WinExec.ShellExecute和CreateProcess.1.CreateProcess因为使用复杂,比较少用.2.WinExec主要运行EXE文件.如: ...

  10. visual studio 使用正则查找或替换示例

    visual studio 使用正则查找或替换示例 注意哟:使用之前应做好备份 visual studio 2015: 多行替换 (.*)point\ =(.*);\r\n.+this.([A-Za- ...