CodeForces - 743D Chloe and pleasant prizes 树形DP
2016-12-15 14:54
447 查看
题意:给你一棵树,每个节点有个值,同时选择两棵子树,不能有重叠,求两颗子树所能得到的最大值。
在树上DP就好了 dp[i]表示i节点为根的子树中能选取到的最大值
在树上DP就好了 dp[i]表示i节点为根的子树中能选取到的最大值
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> #define maxn 200005 #define oo 2000000005 using namespace std; typedef long long ll; struct node{ int to,nxt; }ed[maxn<<1]; int head[maxn],cnt; void addedge(int u,int v){ ed[cnt].to=v,ed[cnt].nxt=head[u],head[u]=cnt++; } ll vl[maxn],dp[maxn]; bool vis[maxn]; ll ans; void dfs(int u){ dp[u]=-oo; vis[u]=1; for(int i=head[u];~i;i=ed[i].nxt){ int v=ed[i].to; if(vis[v])continue; dfs(v); vl[u]+=vl[v]; if(dp[u]>-oo)ans=max(ans,dp[u]+dp[v]); dp[u]=max(dp[u],dp[v]); } dp[u]=max(dp[u],vl[u]); } int main() { ans=-oo; memset(head,-1,sizeof(head)); int n; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%lld",&vl[i]); for(int i=1;i<n;i++){ int a,b; scanf("%d%d",&a,&b); addedge(a,b); addedge(b,a); } dfs(1); if(ans==-oo)printf("Impossible\n"); else printf("%lld\n",ans); return 0; }
相关文章推荐
- mybatis出现org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
- iOS模拟器慢网速测试工具之Hardware IO Tools
- 将时间戳转换为Date类型
- php-fpm优化方法 pm.min_spare_servers、pm.max_spare_servers 的真实意义
- TotalCommander 之 快捷键
- soot基础 -- 从头开始创建一个类
- Android 搜索关键字高亮显示及开发中遇到的坑
- jsp注释快捷键
- Spring bean 通过实现 InitializingBean ,DisposableBean 接口实现初始化方法和销毁前操作
- GPU Monitor
- NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel
- JS中call、apply、bind大概区别
- 【BZOJ1014】[JSOI2008]火星人prefix
- JavaScript变量、作用域及内存
- Js入门学习总结
- tomcat 配置https
- Codeforces Round #384 (Div. 2) C. Vladik and fractions
- TotalCommander 之 配置
- MyISAM与InnoDB区别
- 326. Power of Three