http://codeforces.com/contest/743/problem/D

如果我们知道mx[1]表示以1为根节点的子树中,点权值的最大和是多少(可能是整颗树,就是包括了自己)。那么,就可以O(n)扫一次各个点,对于每个点的儿子。

选出最大的两个mx[son],更新答案即可。(注意这个节点只有1个或者没有儿子,就要是-inf)

那么怎么得到这个mx[]呢?

可以知道mx[father] = max(所有的mx[son])

最后还要看看是否能选择整个树,所以用dp[cur]表示以cur为根的树的所有点券之和。

然后就能dfs算出mx[]和dp[]。然后O(n)扫一次就好。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
typedef long long int LL;
const LL inf = 1e16L; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 2e5 + ;
int a[maxn];
struct node {
int u, v, w;
int tonext;
}e[maxn << ];
int first[maxn];
int num;
void add(int u, int v) {
++num;
e[num].u = u;
e[num].v = v;
e[num].tonext = first[u];
first[u] = num;
}
bool vis[maxn];
LL dp[maxn];
LL mx[maxn];
void init(int cur) {
dp[cur] = a[cur];
mx[cur] = -inf;
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (vis[v]) continue;
vis[v] = true;
init(v);
dp[cur] += dp[v];
mx[cur] = max(mx[cur], mx[v]);
// mx[cur] = max(mx[cur], dp[v]);
}
mx[cur] = max(dp[cur], mx[cur]);
}
LL ans = -inf;
void dfs(int cur, int fa) {
LL mx1 = -inf, mx2 = -inf;
int t = ;
for (int i = first[cur]; i; i = e[i].tonext) {
int v = e[i].v;
if (fa == v) continue;
dfs(v, cur);
t++;
if (mx1 <= mx[v]) {
mx2 = mx1;
mx1 = mx[v];
} else if (mx2 <= mx[v]) {
mx2 = mx[v];
}
}
if (t == || t == ) return;
ans = max(ans, mx1 + mx2);
}
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
for (int i = ; i <= n - ; ++i) {
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
vis[] = true;
init();
// printf("%d\n", dp[6]);
// cout << mx[5] << endl;
memset(vis, , sizeof vis);
dfs(, );
if (ans <= -inf) {
cout << "Impossible" << endl;
} else cout << ans << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}

D. Chloe and pleasant prizes 树上dp + dfs的更多相关文章

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

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

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

                                                                D. Chloe and pleasant prizes             ...

  4. CodeForces - 743D Chloe and pleasant prizes

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

  5. D. Chloe and pleasant prizes

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

  6. Chloe and pleasant prizes

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

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

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

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

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

  9. codeforces 743D. Chloe and pleasant prizes(树形dp)

    题目链接:http://codeforces.com/contest/743/problem/D 大致思路挺简单的就是找到一个父节点然后再找到其两个字节点总值的最大值. 可以设一个dp[x]表示x节点 ...

随机推荐

  1. Python - 多次检查后缀名(endwith)

    在通过后缀名查找类型文件的时候, 多次使用endwith, 使用元组(tuple), 简化操作. 此类方式, 也能够应用于if语句多次类似检測. 代码 # 列出目录内全部代码 def list_dic ...

  2. 深入浅出Redis(二)高级特性:事务

    第一篇中介绍了Redis是一个强大的键-值仓储,支持五种灵活的数据结构.其实,Redis还支持其他的一些高级特性:事务.公布与订阅.管道.脚本等,本篇我们来看一下事务. 前一篇中我们提到,在Redis ...

  3. 【转】实现LoadRunner多个场景的顺序执行

    应用场景假设有3个不同的测试场景,分别为并发登录.核心业务.可靠性测试,3个场景有先后执行顺序.由于白天测试机器另有用处,只能在晚上进行性能测试,这时我们的期望是能否把测试场景都设定好之后晚上自动运行 ...

  4. APUE 线程 - 程序清单

    APUE 线程 - 程序清单 程序清单11-1 打印线程ID #include "util.h" #include<pthread.h> pthread_t ntid; ...

  5. Android经常使用设计模式(二)

    继上一篇 Android经常使用设计模式(一)里认识了观察者.适配器.代理等三种模式,这一篇将会解说下面三种模式: 工厂模式 单例模式 命令模式 1.工厂模式(Factory Pattern) 工厂模 ...

  6. mt7620 uboot

    我本机装的是64位Ubuntu, SDK 里提供的 buildroot-gcc342 是32位的,无法直接运行,需要先安装 gcc-multilib. sudo apt-get install gcc ...

  7. vsCode 常用快捷键(mac 版)

    光标多行显示: commond+Alt+topArrow/downArrow 查找:commond+F 查找并按顺序切换下一个:commond+G 跳转到某一行: ctrl+G 输入行号跳转 跳转到某 ...

  8. 浅谈JavaScript的字符串的replace方法

    JavaScript字符串提供了一个replace方法.replace方法可以接受两个参数:第一个参数可以使RegExp对象或者一个字符串,第二个参数可以是一个字符串或者一个函数.如果第一个参数是字符 ...

  9. ArrayAdapter requires the resource ID to be a TextView

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABDUAAADFCAIAAADLz168AAAgAElEQVR4nO2d368kxZXnQ37dn9qdWa

  10. asp.net 实现搜索站内搜索功能

    首先有index和search 两个页面 index页面中有textbox1和button1两个控件 双击button1控件添加代码: protected void Button1_Click(obj ...