题目链接:http://codeforces.com/contest/743/problem/D

大致思路挺简单的就是找到一个父节点然后再找到其两个字节点总值的最大值。

可以设一个dp[x]表示x节点及以下节点能得到的最大值,由于dfs的顺序我们

可以边dfs边求解ans=max(dp[x]+dp[v],ans)(由于dfs是先查询完dp[v]的

左边的树,所以得到的dp[x]是在v节点左边的最大值),顺便记录一下每个节

点的sum值因为一个节点要么整个子树包括自己都要算或者不算。

#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
const int M = 2e5 + 10 , inf = 2e14;
typedef long long ll;
ll a[M] , dp[M] , sum[M] , ans;
vector<int>vc[M];
void dfs(int pos , int pre) {
int len = vc[pos].size();
ll temp = a[pos];
for(int i = 0 ; i < len ; i++) {
int gg = vc[pos][i];
if(gg != pre) {
dfs(gg , pos);
temp += sum[gg];
if(dp[pos] != -inf) {
ans = max(ans , dp[pos] + dp[gg]);
}
dp[pos] = max(dp[pos] , dp[gg]);
}
}
sum[pos] = temp;;
dp[pos] = max(dp[pos] , sum[pos]);
}
int main() {
int n;
cin >> n;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
}
for(int i = 1 ; i < n ; i++) {
int x , y;
cin >> x >> y;
vc[x].push_back(y);
vc[y].push_back(x);
}
ans = -inf;
for(int i = 1 ; i <= n ; i++) {
dp[i] = -inf;
}
dfs(1 , -1);
if(ans == -inf)
cout << "Impossible" << endl;
else
cout << ans << endl;
return 0;
}

codeforces 743D. Chloe and pleasant prizes(树形dp)的更多相关文章

  1. 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 ...

  2. Codeforces 743D Chloe and pleasant prizes(树型DP)

                                                                D. Chloe and pleasant prizes             ...

  3. CodeForces - 743D Chloe and pleasant prizes

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  4. 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 ...

  5. D. Chloe and pleasant prizes 树上dp + dfs

    http://codeforces.com/contest/743/problem/D 如果我们知道mx[1]表示以1为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己).那么,就可 ...

  6. D. Chloe and pleasant prizes

    D. Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. Chloe and pleasant prizes

    Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  8. Codeforces 743D:Chloe and pleasant prizes(树形DP)

    http://codeforces.com/problemset/problem/743/D 题意:求最大两个的不相交子树的点权和,如果没有两个不相交子树,那么输出Impossible. 思路:之前好 ...

  9. [Codeforces743D][luogu CF743D]Chloe and pleasant prizes[树状DP入门][毒瘤数据]

    这个题的数据真的很毒瘤,身为一个交了8遍的蒟蒻的呐喊(嘤嘤嘤) 个人认为作为一个树状DP的入门题十分合适,同时建议做完这个题之后再去做一下这个题 选课 同时在这里挂一个选取节点型树形DP的状态转移方程 ...

随机推荐

  1. Netty+WebSocket 获取火币交易所数据项目

    Netty+WebSocket 获取火币交易所时时数据项目 先附上项目项目GitHub地址 spring-boot-netty-websocket-huobi 项目简介 本项目使用 SpringBoo ...

  2. ssm 搭建项目各项配置

    首先配置 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  3. Jquery 实现添加删除,checkbok 的全选,反全选,但是批量删除没有实现

    <!DOCTYPE html><html> <head>  <meta charset="utf-8" />  <title& ...

  4. A solution to the never shortened to-do list

    I once told my younger sister my learning system, and the basic five doctrines of my methodology. Bu ...

  5. 高性能MySQL之事物

    一.概念 事务到底是什么东西呢?想必大家学习的时候也是对事务的概念很模糊的.接下来通过一个经典例子讲解事务. 银行在两个账户之间转账,从A账户转入B账户1000元,系统先减少A账户的1000元,然后再 ...

  6. RE最全面的正则表达式----字符验证

    二.校验字符的表达式汉字:^[一-彪]{0,}$英文和数字:^[A-Za-z0-9]+$ 或 ^[A-Za-z0-9]{4,40}$长度为3-20的所有字符:^.{3,20}$由26个英文字母组成的字 ...

  7. springboot整合websocket高级版

    目录 sockjs介绍 产生的原因 环境搭建 springboot整合sockjs 使用场景 聊天室开发 点对点通信 群聊 效果 总结 加入战队 微信公众号 上一章节我们说了websocket的优缺点 ...

  8. Unity进阶之:Shader渲染

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  9. java中String,StringBuffer,StringBuilder的区别

    String: 1,是字符串常量,一旦创建就不能修改.对于已经存在了的String对象的修改都是重新创建一个新的对象,然后把新的值保存进去. 2,String也是final类,不能被继承. 3,而且S ...

  10. I firmly believe

    I firmly believe, what you plant now, you will harvest later.