HDU 5735 Born Slippy

看官方题解很详细了:

总结一下:递推式不难想到,但是每次求dp[x]需要枚举祖先,复杂度太高,需要优化。

题解的方法,可以使得复杂度降低到1<<24.

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
    char c = getchar();  while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar(); }
    return x;
}

const LL mod=1e9+7;
const int maxn=(1<<16)+10;
int T,n;
char op[5];
LL w[maxn],dp[maxn],g[300][300],t[maxn][258];
int h[maxn],tot;
struct Edge {int v,nx;}e[maxn];
int f[300];

LL get(LL a,LL b)
{
    if(op[0]==‘A‘) return a&b;
    else if(op[0]==‘O‘) return a|b;
    else return a^b;
}

void dfs(int x)
{
    LL a=w[x]>>8, b=w[x]-(a<<8);

    for(int i=0;i<256;i++)
        if(f[i]) dp[x]=max(dp[x],g[i][b]+(get((LL)i,a)<<8));

    for(int i=0;i<256;i++) t[x][i]=g[a][i]; f[a]++;
    for(int i=0;i<256;i++) g[a][i]=max(g[a][i],dp[x]+get(b,(LL)i));
    for(int i=h[x];i!=-1;i=e[i].nx) dfs(e[i].v);
    for(int i=0;i<256;i++) g[a][i]=t[x][i]; f[a]--;
}

int main()
{
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n); scanf("%s",op);
        for(int i=1;i<=n;i++) scanf("%lld",&w[i]);
        tot=0; memset(h,-1,sizeof h);
        for(int i=2;i<=n;i++)
        {
            int fa;scanf("%d",&fa);
            e[tot].v=i,e[tot].nx=h[fa],h[fa]=tot++;
        }
        memset(f,0,sizeof f);
        memset(dp,0,sizeof dp);
        memset(g,0,sizeof g); dfs(1);
        LL ans=0;
        for(int i=1;i<=n;i++)
            ans=(ans+(i*(w[i]+dp[i])%mod)%mod)%mod;
        printf("%lld\n",ans);
    }
    return 0;
}
时间: 2024-10-17 10:06:19

HDU 5735 Born Slippy的相关文章

HDU - 5735 Born Slippy 思维 + dp(看题解)

HDU - 5735 感觉这个思路相当巧妙啊.. 考虑最普通的 dp[ i ] = max(dp[ j ] + w[ i ] opt w[ j ]), j 是 i 的祖先. 把(2 << 16) 分成前八位和后八位去优化最朴素的dp. 修改遍历后八位, 查询遍历前八位. #pragma GCC optimize(2) #pragma GCC optimize(3) #include<bits/stdc++.h> #define LL long long #define LD lon

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

[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5735 [题目大意] 给出一棵树,树上每个节点都有一个权值w,w不超过216,树的根为1,从一个点往根的方向走,可以得到他的祖先序列,现在需要从v1点的祖先序列中挑选出一定数量的点,组成数列v1,v2,v3……vm,要求vi是vi-1的祖先,求dp[v1]=max(dp[vi]+(w[v1] opt w[vi])),opt是一种运算,在题目中可为xor,or或者and,最后求出ans=sum_{i

HDU5735 : Born Slippy

考虑DP,设$f[x]$表示最后一个是$x$时的最优解,则$f[x]=\max(f[y]+w[x]\ opt\ w[y])$,其中$y$是$x$的祖先. 注意到$w[i]<2^{16}$,那么将数字划分成前$8$位和后$8$位,额外维护一个数组$g[a][b]$表示某个$w[y]$的前$8$位为$a$时,后$8$位$opt\ b+f[y]$的最大值,那么维护$g$只需要枚举$b$,计算$f$只需要枚举$a$,单次复杂度都是$O(2^8)$. 对于树的情况只需要保存每次修改,然后在回溯的时候还原即

HDU 5880 Family View (AC自动机)

Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Description Steam is a digital distribution platform developed by Valve Corporation offering digital rights management (DRM), multiplayer gaming and socia

HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法

地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 754    Accepted Submission(s): 279 Problem Description When ?? was born, seven crows flew

HDU 2454 Degree Sequence of Graph G(Havel定理 判断简单图的存在)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2454 Problem Description Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep love and yearns for the boundless oceans.

hdu 5880 AC自动机

Family View Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 20 Problem Description Steam is a digital distribution platform developed by Valve Corporation offering

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

http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body

HDU 2103 Family planning

http://acm.hdu.edu.cn/showproblem.php?pid=2103 Problem Description As far as we known,there are so many people in this world,expecially in china.But many people like LJ always insist on that more people more power.And he often says he will burn as mu