bzoj 4033 树上染色 - 树形动态规划
Input
Output
Sample Input
5 2
1 2 3
1 5 1
2 3 1
2 4 2
Sample Output
17
【样例解释】
将点1,2染黑就能获得最大收益。
动态规划的第一步——设计状态,f[i][j]表示以i节点为根的子树中染了j个黑点的"收益"。
不过这样没有黑点的位置,这么多个点,总不可能用N进制来表示点的位置。所以只能换个思路。
对于当前考虑的这棵子树,我知道染了j个节点,那么我知道在这棵子树内的白点数和子树外的白点数和黑点数。因此我可以计算出节点i到它的父节点的那条边的对答案的贡献,对于子节点转移到父节点就是一个用dp合并的过程,因此解决了状态转移的问题,时间复杂度为O(nk)。
注意dp时不合法的状态一定不能转移(看代码吧,或者自己想想也可以,状态转移前有个if)
(现在觉得以前的树归写得好丑)
Code
/**
* bzoj
* Problem#4033
* Accepted
* Time:630ms
* Memory:17092k
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<ctime>
#include<map>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#ifndef WIN32
#define AUTO "%lld"
#else
#define AUTO "%I64d"
#endif
using namespace std;
typedef bool boolean;
#define inf 0xfffffff
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline boolean readInteger(T& u) {
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-') {
aFlag = -;
x = getchar();
}
for(u = x - ''; isdigit((x = getchar())); u = u * + x - '');
u *= aFlag;
ungetc(x, stdin);
return true;
} ///map template starts
typedef class Edge{
public:
int end;
int next;
int w;
Edge(const int end = , const int next = , const int w = ):end(end), next(next), w(w){}
}Edge; typedef class MapManager{
public:
int ce;
int *h;
Edge *edge;
MapManager(){}
MapManager(int points, int limit):ce(){
h = new int[(const int)(points + )];
edge = new Edge[(const int)(limit + )];
memset(h, , sizeof(int) * (points + ));
}
inline void addEdge(int from, int end, int w){
edge[++ce] = Edge(end, h[from], w);
h[from] = ce;
}
inline void addDoubleEdge(int from, int end, int w){
addEdge(from, end, w);
addEdge(end, from, w);
}
Edge& operator [] (int pos) {
return edge[pos];
}
}MapManager;
#define m_begin(g, i) (g).h[(i)]
///map template ends template<typename T>class Matrix{
public:
T *p;
int lines;
int rows;
Matrix():p(NULL){ }
Matrix(int rows, int lines):lines(lines), rows(rows){
p = new T[(lines * rows)];
}
T* operator [](int pos){
return (p + pos * lines);
}
};
#define matset(m, i, s) memset((m).p, (i), (s) * (m).lines * (m).rows) int n, k;
MapManager g;
Matrix<long long> f;
int* size; inline void init() {
readInteger(n);
readInteger(k);
g = MapManager(n, * n);
f = Matrix<long long>(n + , k + );
size = new int[(const int)(n + )];
matset(f, , sizeof(long long));
for(int i = , a, b, c; i < n; i++) {
readInteger(a);
readInteger(b);
readInteger(c);
g.addDoubleEdge(a, b, c);
}
} void treedp(int node, int fa, int len) {
size[node] = ;
for(int i = m_begin(g, node); i != ; i = g[i].next) {
int& e = g[i].end;
if(e == fa) continue;
treedp(e, node, g[i].w);
size[node] += size[e];
for(int j = min(size[node], k); j >= ; j--) {
for(int s = ; s <= size[e] && s <= j; s++) {
if(j - s <= size[node] - size[e])
smax(f[node][j], f[node][j - s] + f[e][s]);
}
}
}
for(int i = ; i <= min(size[node], k); i++)
f[node][i] += (i * 1LL * (k - i) + (size[node] - i) * 1LL * (n - k - size[node] + i)) * len;
} inline void solve() {
treedp(, , );
printf(AUTO"\n", f[][k]);
} int main() {
init();
solve();
return ;
}
bzoj 4033 树上染色 - 树形动态规划的更多相关文章
- [BZOJ 4033] 树上染色
Link: BZOJ 4033 传送门 Solution: 此题用到了计算贡献的方法, 将 多条路径的路径和 $->$ $\sum_{i=1}^{n-1} w[i]*cnt[i]$ 这样我们由 ...
- [HAOI2015][bzoj 4033]树上染色(树dp+复杂度分析)
[题目描述]有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色.将所有点染色后,你会获得黑点两两之间的距离加上白点两两 ...
- 洛谷 P3177 [HAOI2015]树上染色 树形DP
洛谷 P3177 [HAOI2015]树上染色 树形DP 题目描述 有一棵点数为 \(n\) 的树,树边有边权.给你一个在 \(0 \sim n\)之内的正整数 \(k\) ,你要在这棵树中选择 \( ...
- bzoj 4033: [HAOI2015]树上染色 [树形DP]
4033: [HAOI2015]树上染色 我写的可是\(O(n^2)\)的树形背包! 注意j倒着枚举,而k要正着枚举,因为k可能从0开始,会使用自己更新一次 #include <iostream ...
- [BZOJ4033][HAOI2015]树上染色(树形DP)
4033: [HAOI2015]树上染色 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2437 Solved: 1034[Submit][Stat ...
- 【BZOJ4033】[HAOI2015]树上染色 树形DP
[BZOJ4033][HAOI2015]树上染色 Description 有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染 ...
- 【HAOI2015】树上染色—树形dp
[HAOI2015]树上染色 [题目描述]有一棵点数为N的树,树边有边权.给你一个在0~N之内的正整数K,你要在这棵树中选择K个点,将其染成黑色,并将其他的N-K个点染成白色.将所有点染色后,你会获得 ...
- BZOJ 4033 [HAOI2015]树上染色 ——树形DP
可以去UOJ看出题人的题解. 这样的合并,每一个点对只在lca处被考虑到,复杂度$O(n^2)$ #include <map> #include <ctime> #includ ...
- bzoj4033 [HAOI2015]树上染色——树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4033 树形DP,状态中加入 x 与父亲之间的边的贡献: 边权竟然是long long... ...
随机推荐
- linux 启动过程关键点
Freeing init memory: 4568K init... Freeing init memory 后,就是开始init进程
- 分析 mongodb admin local 修改ip 热修改
分析 mongodb admin local 更改ip前 [root@e ~]# mongo mongodb://admin:adminpwd123@10.144.114.152 MongoDB ...
- Time Profiler Instrument分析卡顿
https://www.jianshu.com/p/080108c969e8 启动Time Profile:Xcode ——> Product ——> Profile ——> Tim ...
- Redis、Mongo - 目录
redis redis字典取数据.列表取数据(数据量大) redis 实现栈 - python mongodb - 可视化工具 / pymongo - 使用方法
- 2018/04/03 PHP 中的 进制计算问题
还是先抛出一个问题 017 + 1 = ? -- 如果你知道的话,那也就不用看下面了,哈哈. 答案是 // 15 -- 如果你想的答案和这个不对的话,说明你也有这个问题,也应该学习一下啦. -- 首先 ...
- Roadblocks--poj3255(次短路)
题目链接 求次短路的问题: dist[i][0]和dist[i][1]表示从起点1到i的距离和从起点n到i的距离: 次短路要比最短路大但小于其他路: 每条路1--n的距离都可以用dist[i][0] ...
- Frame报文
链路层帧常用的帧格式有两种:Ethernet II 与 IEEE802.3 Ethernet II 格式多用于终端设备的通信 IEEE802.3 格式多用于网络设备的通信 如何区分这两种报文 ...
- Python描述器引导(转)
原文:http://pyzh.readthedocs.io/en/latest/Descriptor-HOW-TO-Guide.html 1. Python描述器引导(翻译) 作者: Raymond ...
- 代码这样写更优雅(Python版)
要写出 Pythonic(优雅的.地道的.整洁的)代码,还要平时多观察那些大牛代码,Github 上有很多非常优秀的源代码值得阅读,比如:requests.flask.tornado,笔者列举一些常见 ...
- javaScript设计模式(一)观察者模式
哈哈..写了一个钟,一点一点加功能. 1 function Publisher(){ this.subscribers = []; //存储订阅者 this.news = []; //存储要发布的消息 ...