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. Atomic Builtins - Using the GNU Compiler Collection (GCC) GCC 提供的原子操作

    http://gcc.gnu.org/onlinedocs/gcc-4.4.3/gcc/Atomic-Builtins.html gcc从4.1.2提供了__sync_*系列的built-in函数,用 ...

  2. AWK教程

    1. IBM:GAWK入门:AWK语言基础 2. Unix AWK使用手册 3. 台湾中研院计算中心ASPAC计划之AWK程序介绍 4. Study-area之AWK 5. AWK学习笔记——酷勤 持 ...

  3. JQuery实现表格行的上移、下移、删除、增加

    <%@ page language="java" import="java.util.*" pageEncoding="GBK"%&g ...

  4. nhibernate实体类主键ID赋值问题

    有个同事忽然来找我,说他遇到了一个问题,在调用nhibernate 进行update数据的时候报错,说是有数据行锁定. 看代码,没啥问题. 直接在PL/SQL developer里对数据库进行插入,也 ...

  5. C++ 函数部分(1)

    1.编写一个求X的n次方的函数 .csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas ...

  6. form之action的绝对路径与相对路径(转载)

    1.当你的form要提交到你自己的站点之外的URL的时候,就采取绝对路径: <form action="http://www.xxx.yyy:zzzz/mmm/nn/kkk.jsp&q ...

  7. id 查询

    Ids Query | Elasticsearch Reference [6.2] | Elastic http://www.elastic.co/guide/en/elasticsearch/ref ...

  8. Responsive Nav

    引入文件 <!-- 引入这些文件至 <head> 中 --> <link rel="stylesheet" href="responsive ...

  9. bacth参数说明 cmd parameter

    http://www.robvanderwoude.com/parameters.php Windows NT 4 introduced a set of new features for comma ...

  10. [Usaco2015DEC] Breed Counting

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4397 [算法] 树状数组 时间复杂度 : O(QlogN) [代码] #includ ...