【HDU 5735】Born Slippy(状压dp)
来源:互联网 发布:字符串数组结束标志 编辑:程序博客网 时间:2024/06/08 19:05
Born Slippy
Total Submission(s): 833 Accepted Submission(s): 252
Problem Description
Professor Zhang has a rooted tree, whose vertices are conveniently labeled by
For each
v1=s and vi is the ancestor ofvi−1(1<i≤m) .- the value
f(s)=wv1+∑i=2mwvi opt wvi−1 is maximum. Operationx opt y denotes bitwise AND, OR or XOR operation of two numbers.
Input
There are multiple test cases. The first line of input contains an integer
The first line contains an integer
There are about
Output
For each test case, output an integer
Sample Input
3
5 AND
5 4 3 2 1
1 2 2 4
5 XOR
5 4 3 2 1
1 2 2 4
5 OR
5 4 3 2 1
1 2 2 4
Sample Output
91
139
195
Author
zimpha
Source
2016 Multi-University Training Contest 2
题目大意:
给出一棵树,每个点有点权。
定义
最直白的想法:
这样是
考虑优化,将遍历一个节点时,内部更新的
因
这样考虑一个中间数组
这样在计算
枚举祖先二进制前八位,因为确定当前节点i后,其实
然后取遍历过程中
这也是
这样通过
(不计后八位运算)
代码如下:
#include <iostream>#include <cmath>#include <vector>#include <cstdlib>#include <cstdio>#include <cstring>#include <queue>#include <stack>#include <list>#include <algorithm>#include <map>#include <set>#define LL long long#define Pr pair<int,int>#define fread(ch) freopen(ch,"r",stdin)#define fwrite(ch) freopen(ch,"w",stdout)using namespace std;const int INF = 0x3f3f3f3f;const int msz = 1<<8;const int mod = 1e9+7;const double eps = 1e-8;struct Edge{ int v,next;};LL ds[256][256];LL dp[66666];bool vis[66666];Edge eg[66666];int head[66666];int cf[66666][2];LL val[66666];LL mp[65537][256];LL ans;int tp;void Add(int u,int v){ eg[tp].v = v; eg[tp].next = head[u]; head[u] = tp++;}void dfs(int u,int opt){ int a,b; a = cf[val[u]][0]; b = cf[val[u]][1]; dp[u] = 0; for(int i = 0; i < msz; ++i) { if(ds[i][b] == -1) continue; if(opt == 0) dp[u] = max(dp[u],1LL*(ds[i][b]+((i&a)<<8))); else if(opt == 1) dp[u] = max(dp[u],1LL*(ds[i][b]+((i|a)<<8))); else if(opt == 2) dp[u] = max(dp[u],1LL*(ds[i][b]+((i^a)<<8))); } ans = (ans+(1LL*dp[u]*u)%mod)%mod; //printf("dp[%d] %lld\n",u,dp[u]); int v; memcpy(mp[u],ds[a],sizeof(ds[a])); int k; for(int i = 0; i < msz; ++i) { if(opt == 0) k = i&b; else if(opt == 1) k = i|b; else if(opt == 2) k = i^b; if(ds[a][i] == -1) ds[a][i] = dp[u]+k; else ds[a][i] = max(1LL*ds[a][i],dp[u]+k); } for(int i = head[u]; i != -1; i = eg[i].next) { v = eg[i].v; // printf("%d-%d\n",u,v); dfs(v,opt); } memcpy(ds[a],mp[u],sizeof(mp[u]));}int main(){ int a,b,tmp; a = 0; b = 0; while(a+b < 66666) { cf[a+b][0] = a>>8; cf[a+b][1] = b; // if(a+b >= 4350 && a+b <= 4360) // printf("%d %d %d\n",a+b,cf[a+b][0],cf[a+b][1]); b++; tmp = b>>8; tmp <<= 8; a += tmp; b -= tmp; } int t,u,n; scanf("%d",&t); char opt[5]; while(t--) { scanf("%d%s",&n,opt); ans = 0; for(int i = 1; i <= n; ++i) { scanf("%lld",&val[i]); ans = (ans+1LL*val[i]*i)%mod; } memset(head,-1,sizeof(head)); tp = 0; for(int i = 2; i <= n; ++i) { scanf("%d",&u); Add(u,i); } memset(ds,-1,sizeof(ds)); if(opt[0] == 'A') dfs(1,0); else if(opt[0] == 'O') dfs(1,1); else dfs(1,2); printf("%lld\n",ans); } return 0;}
- 【HDU 5735】Born Slippy(状压dp)
- HDU 5735 Born Slippy(树形DP)
- HDU 5735 Born Slippy(dp+优化)
- HDU 5735 Born Slippy 【DP + 小技巧】
- HDU 5735 Born Slippy
- HDU 5735 Born Slippy
- HDU 5735 Born Slippy
- [HDU 5735] Born Slippy (机智暴力)
- hdu 5735 Born Slippy
- HDU 5735 Born Slippy(树形dp+可持久化)
- HDU 5735 Born Slippy【巧妙暴力枚举】
- hdu 5735 Born Slippy 折半枚举
- hdu 5735 Born Slippy(2016多校第二场1002) dp
- HDU 5735 Born Slippy (分块+树上可持久化)
- hud oj 5735 Born Slippy (暴力)
- HDU 5735 Born Slippy ( from:2016 Multi-University Training Contest 2 )
- HDU5735 Born Slippy
- HDU5735 Born Slippy
- centos之lnmp
- CRM开发要点(七)
- WINCE 6.0 RDP相关的Terminal server Client License界面修改
- Demo-jQuery下拉列表内容联动
- Struts2与SpingMVC的开发对比
- 【HDU 5735】Born Slippy(状压dp)
- lf4j
- 生产验证码
- mac 海马0.8.3 模拟器adb调试
- Fragment全解析
- 判断二叉树是否二叉查找树
- 简单漂亮的”加载中“控件
- 服务高可用性及容灾的几个衡量指标
- 工作中对于提交BUG或需求的看法