Codeforces 743D:Chloe and pleasant prizes(树形DP)
http://codeforces.com/problemset/problem/743/D
题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible。
思路:之前好像也做过这种类型的题目啊,知道是树形DP,但是不知道怎么保证两个不相交。看别人代码之后,
在DFS回溯的时候,
void dfs(int u, int fa) {
sum[u] = w[u];
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
if(dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
}
先执行 if 语句的话,可以保证只有以 u 结点为根的时候,它的子树有两个或两个以上,否则就不会更新 ans 了。并且这个时候dp【u】还只是目前扫过的一个子树的最大权值和,再加上一个次大的dp【v】,这样就可以保证是最大的两个不相交子树的权值和了。遍历完后dp【u】一定是以 u 为根的所有子树的最大权值和,再和 sum【u】更新是否要包含u这个节点的权值。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
using namespace std;
#define N 200010
typedef long long LL;
const LL INF = 1LL<<;
struct node
{
int v, nxt;
}edge[N*];
LL dp[N], sum[N], w[N], head[N], tot, ans; void add(int u, int v) {
edge[tot].v = v; edge[tot].nxt = head[u]; head[u] = tot++;
} void dfs(int u, int fa) {
sum[u] = w[u];
for(int i = head[u]; ~i; i = edge[i].nxt) {
int v = edge[i].v;
if(v == fa) continue;
dfs(v, u);
sum[u] += sum[v];
if(dp[u] > -INF) ans = max(ans, dp[u] + dp[v]);
dp[u] = max(dp[u], dp[v]);
}
dp[u] = max(dp[u], sum[u]);
} int main()
{
int n;
cin >> n;
memset(head, -, sizeof(head));
for(int i = ; i <= n; i++) cin >> w[i];
for(int i = ; i < n; i++) {
int u, v;
cin >> u >> v;
add(u, v); add(v, u);
}
ans = -INF;
for(int i = ; i <= n; i++) dp[i] = -INF;
dfs(, -);
if(ans <= -INF) puts("Impossible");
else cout << ans << endl;
return ;
}
Codeforces 743D:Chloe and pleasant prizes(树形DP)的更多相关文章
- Codeforces Round #384 (Div. 2)D - Chloe and pleasant prizes 树形dp
D - Chloe and pleasant prizes 链接 http://codeforces.com/contest/743/problem/D 题面 Generous sponsors of ...
- Codeforces 743D Chloe and pleasant prizes(树型DP)
D. Chloe and pleasant prizes ...
- CodeForces - 743D Chloe and pleasant prizes
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- codeforces 743D. Chloe and pleasant prizes(树形dp)
题目链接:http://codeforces.com/contest/743/problem/D 大致思路挺简单的就是找到一个父节点然后再找到其两个字节点总值的最大值. 可以设一个dp[x]表示x节点 ...
- coderforces #384 D Chloe and pleasant prizes(DP)
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- D. Chloe and pleasant prizes 树上dp + dfs
http://codeforces.com/contest/743/problem/D 如果我们知道mx[1]表示以1为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己).那么,就可 ...
- D. Chloe and pleasant prizes
D. Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Chloe and pleasant prizes
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- [Codeforces743D][luogu CF743D]Chloe and pleasant prizes[树状DP入门][毒瘤数据]
这个题的数据真的很毒瘤,身为一个交了8遍的蒟蒻的呐喊(嘤嘤嘤) 个人认为作为一个树状DP的入门题十分合适,同时建议做完这个题之后再去做一下这个题 选课 同时在这里挂一个选取节点型树形DP的状态转移方程 ...
随机推荐
- Android --Android Stuido 导入jar包
参考博客:Android Studio导入第三方类库的方法 1.将第三方开源jar包代码拷入工程文件夹 2.在settings.gradle添加 include ':app' include ':ap ...
- HashMap put,get操作
HashMap中的put方法 public V put(K key, V value) { //当key为null,调用putForNullKey方法,保存null与table第一个位置中,这是Has ...
- MetInfo标签函数及参数
参数标签直接在页面中调用标签代码即可: 函数标签需要在页面PHP嵌入代码中通过参数定义转换方可使用,如$metlang=methtml_lang('-'),点击函数标签代码可查看函数标签详细使用方法: ...
- 利用selenium Server实现IE firefox 和 chrome兼容性测试
本文的主题是基于Selenium Server,使用 Java 语言编写网页交互流程, 实现多浏览器(IE Firefox Chrome)兼容性测试,为使用纪要. Selenium Selenium是 ...
- Linux C进程内存布局
当程序文件运行为进程时,进程在内存中获得空间.这个空间是进程自己的内存空间.每个进程空间按照如下方式分为不同区域: 进程内存空间布局图 text:代码段.存放的是程序的全部代码(指令),来源于二进制可 ...
- FileInputStream and FileOutputStream
Java FileOutputStream class Java FileOutputStream is an output stream for writing data to a file. If ...
- csu oj 1339: 最后一滴血
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1339 1339: 最后一滴血 Time Limit: 1 Sec Memory Limit: 1 ...
- [原创]java WEB学习笔记72:Struts2 学习之路-- 文件的上传下载,及上传下载相关问题
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- struts2校验の实现
1.JSP <%@ page contentType="text/html; charset=utf-8" language="java" errorPa ...
- 夺命雷公狗---DEDECMS----24dedecms让网站头部分离
我们这里来做一个让网站头部分离的工作,我们先看下index.htm的模版, 这里很明显就是用了一个div包围着而已,那么我们在模版目录下创建一个head.htm,如下所示: 然后我们再将刚才div里面 ...