【树形dp】ZOJ-3326-Tree of Tree
来源:互联网 发布:离线地图标注软件 编辑:程序博客网 时间:2024/05/17 04:45
这道题和昨晚在CF上做的一道题很像,都是树形dp。刚开始时,我写了个很烂的代码水过了,时间复杂度为O(n^3).不过一看觉得不对了,人家的都是0ms的,于是去网上找题解优化,时间就降为O(n^2)了,终究还是自己太水了……
题目
优化前,300ms:
#include<iostream>#include<cstring>#include<cstdio>#include<vector>using namespace std;template<class T> T Max(T x,T y){return x>y?x:y;}template<class T> T Min(T x,T y){return x<y?x:y;}#define N 105vector<int> v[N];int m,a[N],dp[N][N];void dfs(int x,int y){int i,j,k,z,len;dp[x][1]=a[x];len=v[x].size();for(i=0;i<len;i++){z=v[x][i];if(y==z)continue;dfs(z,x);for(j=m;j>0;j--)for(k=1;k<=j;k++)dp[x][j]=Max(dp[x][j],dp[x][k]+dp[z][j-k]); //状态转移}}int main(){//freopen("a.txt","r",stdin);int i,n,x,y,ans;while(scanf("%d%d",&n,&m)!=EOF){for(i=0;i<n;i++){scanf("%d",a+i);v[i].clear();}for(i=1;i<n;i++){scanf("%d%d",&x,&y);v[x].push_back(y);v[y].push_back(x);}ans=0;for(i=0;i<n;i++) //这个循环浪费了时间{memset(dp,0,sizeof(dp));dfs(i,-1);ans=Max(ans,dp[i][m]);}printf("%d\n",ans);}return 0;}
优化后,0ms:
#include<iostream>#include<cstring>#include<cstdio>#include<vector>using namespace std;template<class T> T Max(T x,T y){return x>y?x:y;}template<class T> T Min(T x,T y){return x<y?x:y;}#define N 105vector<int> v[N];int m,a[N],dp[N][N];void dfs(int x,int y){int i,j,k,z,len;dp[x][1]=a[x];len=v[x].size();for(i=0;i<len;i++){z=v[x][i];if(y==z)continue;dfs(z,x);for(j=m;j>0;j--)for(k=j;k>0;k--) //这里是优化最关键的地方,由大到小就可以防止覆盖dp[x][j]=Max(dp[x][j],dp[x][k]+dp[z][j-k]);}}int main(){//freopen("a.txt","r",stdin);int i,n,x,y,ans;while(scanf("%d%d",&n,&m)!=EOF){for(i=0;i<n;i++){scanf("%d",a+i);v[i].clear();}for(i=1;i<n;i++){scanf("%d%d",&x,&y);v[x].push_back(y);v[y].push_back(x);}memset(dp,0,sizeof(dp));dfs(0,-1);ans=0;for(i=0;i<n;i++)ans=Max(ans,dp[i][m]);printf("%d\n",ans);}return 0;}
- 【树形dp】ZOJ-3326-Tree of Tree
- ZOJ 3201 Tree of Tree 树形DP
- ZOJ - 3201 Tree of Tree 树形DP
- zoj 3201 简单树形dp Tree of Tree
- ZOJ 3201 Tree of Tree(树形背包DP)
- ZOJ 3201 Tree of Tree(树形DP)
- zoj 3201 Tree of Tree(树形背包dp)
- TOJ 2676 ZOJ 3201 Tree of Tree / 树形DP
- ZOJ 3201 Tree of Tree(树形dp + 分组背包)
- ZOJ 3201 Tree of Tree(树形DP+背包)
- ZOJ Monthly, July 2011 - G Tree of Three(树形DP)
- zoj 3201 Tree of Tree 树形dp基础题——树形背包
- 树形DP专辑-ZOJ3201(Tree of Tree)
- ZOJ3201 Tree of Tree 【树形dp】
- (树形DP)tree of tree (zju)
- Zoj3201 Tree of Tree 树形DP
- zoj 3506 Cut the Tree (树形dp)
- ZOJ 3201 Tree of Tree 大水树形背包
- getchar()和EOF总结
- C/C++中的函数参数传递机制(zz)
- http://blog.csdn.net/hell2pradise/article/details/5901745
- linux下TCP socket编程初步(1)
- JAVA实现JNDI连接数据库
- 【树形dp】ZOJ-3326-Tree of Tree
- Hdu-1215 七夕节【算术基本定理应用】
- maven3常用命令及eclipse插件地址
- android onResume is called before you unlock the phone
- WebPage应用深入探讨
- 记本使用串口全攻略(转载)
- ZOJ1409 POJ1018 Communication System
- 3.12
- CentOS6.2下JAVAEE开发环配置(1)---Java+Tomcat环境配置