Codeforces1223E. Paint the Tree(树形dp)
题目链接:传送门
题目大意:
给出节点数为n的一棵带权树,和每个点的最大染色数k。一条边的权重w能产生价值w的条件是,这条边的两端的点至少有一个颜色相同。颜色种类数无限,但每种只能使用两次,问能产生的最大总价值。
思路:
(这两天刷dp专题ing,一上来就会朝dp方面想。看到题目中给出的还是个树,直接盲猜树形dp开始搓状态。结果还tm都搓出来了,以后再看到树会不会巴甫洛夫效应了呀!)
这题考虑树形dp。树形dp的话一般都是考虑一棵以u为根的树的状态,能不能很好地从以u的儿子v为根的子树的状态转移过来。
这题中想从v转移到u,只用考虑u和v有没有染相同颜色即可。
所以用f[u][0/1]表示以u为根的子树,u和u的父亲染不染相同颜色的条件下(0表示不染同色,1反之),产生的最大价值。
在f[u][0]中u最多能和k个儿子染相同的颜色,这些点产生的贡献是$\sum_{u是v的父亲} f[v][1] + edge_{u, v}$,此外还要加上剩余的所有没考虑过的v的f[v][0]。
同理在f[u][1]中u最多能和k-1个儿子染相同的颜色。
考虑如何选择这k/k-1个儿子v:
他们如果连父亲,产生的贡献为f[v][1] + $edge_{u, v}$,如果不连父亲,产生的贡献是f[v][0]。连上父亲对总答案的贡献的增量为f[v][1] + $edge_{u,v}$ - f[v][0],根据这个增量排序从大到小取k/k-1个就可以了(注意一下可能有增量小于0的情况)
代码:O(nlogn)
#include <bits/stdc++.h>
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define N 500005
#define M 500005
#define INF 0x3f3f3f3f
#define mk(x) (1<<x) // be conscious if mask x exceeds int
#define sz(x) ((int)x.size())
#define upperdiv(a,b) (a/b + (a%b>0))
#define mp(a,b) make_pair(a, b)
#define endl '\n'
#define lowbit(x) (x&-x) using namespace std;
typedef long long ll;
typedef double db; /** fast read **/
template <typename T>
inline void read(T &x) {
x = ; T fg = ; char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') fg = -;
ch = getchar();
}
while (isdigit(ch)) x = x*+ch-'', ch = getchar();
x = fg * x;
}
template <typename T, typename... Args>
inline void read(T &x, Args &... args) { read(x), read(args...); }
template <typename T>
inline void write(T x) {
int len = ; char c[]; if (x < ) putchar('-'), x = -x;
do{++len; c[len] = x% + '';} while (x /= );
for (int i = len; i >= ; i--) putchar(c[i]);
}
template <typename T, typename... Args>
inline void write(T x, Args ... args) { write(x), write(args...); } int n, k;
struct Node{
int u;
ll val;
bool operator < (const Node& x) const {
return val > x.val;
}
}; int tot = ;
int head[N], nxt[M<<], ver[M<<];
ll wei[M<<];
void addEdge(int u, int v, ll w) { nxt[++tot] = head[u], ver[tot] = v, wei[tot] = w, head[u] = tot; } bool added[N][];
ll f[N][];
void dfs(int u, int p) {
vector <Node> nodes;
for (int i = head[u]; i != -; i = nxt[i]) {
int v = ver[i]; ll w = wei[i];
if (v == p) continue;
dfs(v, u);
nodes.push_back(Node{v, f[v][]+w - f[v][]});
}
sort(nodes.begin(), nodes.end());
for (int i = ; i < sz(nodes) && i < k; i++) {
Node tmp = nodes[i];
if (tmp.val <= )
break;
if (i < k-) {
f[u][] += tmp.val + f[tmp.u][];
added[tmp.u][] = true;
}
f[u][] += tmp.val + f[tmp.u][];
added[tmp.u][] = true;
}
for (int i = head[u]; i != -; i = nxt[i]) {
int v = ver[i];
if (v == p) continue;
if (!added[v][])
f[u][] += f[v][];
if (!added[v][])
f[u][] += f[v][];
}
}
int main()
{
int q; read(q);
while (q--) {
read(n, k);
tot = ;
for (int i = ; i <= n; i++) {
head[i] = -;
added[i][] = added[i][] = false;
f[i][] = f[i][] = ;
}
for (int i = ; i <= n-; i++) {
int u, v; ll w; read(u, v, w);
addEdge(u, v, w);
addEdge(v, u, w);
}
dfs(, -);
ll ans = f[][];
cout << ans << endl;
}
return ;
}
Codeforces1223E. Paint the Tree(树形dp)的更多相关文章
- E. Paint the Tree 树形dp
E. Paint the Tree 题目大意:给你一棵树,每一个点都可以染k种颜色,你拥有无数种颜色,每一种颜色最多使用2次,如果一条边的两个节点拥有同一种颜色,那么就说 这条边是饱和的,一个树的价值 ...
- 熟练剖分(tree) 树形DP
熟练剖分(tree) 树形DP 题目描述 题目传送门 分析 我们设\(f[i][j]\)为以\(i\)为根节点的子树中最坏时间复杂度小于等于\(j\)的概率 设\(g[i][j]\)为当前扫到的以\( ...
- hdu-5834 Magic boy Bi Luo with his excited tree(树形dp)
题目链接: Magic boy Bi Luo with his excited tree Time Limit: 8000/4000 MS (Java/Others) Memory Limit: ...
- CF 461B Appleman and Tree 树形DP
Appleman has a tree with n vertices. Some of the vertices (at least one) are colored black and other ...
- codeforces 161D Distance in Tree 树形dp
题目链接: http://codeforces.com/contest/161/problem/D D. Distance in Tree time limit per test 3 secondsm ...
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- 5.10 省选模拟赛 tree 树形dp 逆元
LINK:tree 整场比赛看起来最不可做 确是最简单的题目. 感觉很难写 不过单独考虑某个点 容易想到树形dp的状态. 设f[x]表示以x为根的子树内有黑边的方案数. 白边方案只有一种所以不用记录. ...
- Codeforces Round #263 Div.1 B Appleman and Tree --树形DP【转】
题意:给了一棵树以及每个节点的颜色,1代表黑,0代表白,求将这棵树拆成k棵树,使得每棵树恰好有一个黑色节点的方法数 解法:树形DP问题.定义: dp[u][0]表示以u为根的子树对父亲的贡献为0 dp ...
- codeforces Round #263(div2) D. Appleman and Tree 树形dp
题意: 给出一棵树,每个节点都被标记了黑或白色,要求把这棵树的其中k条变切换,划分成k+1棵子树,每颗子树必须有1个黑色节点,求有多少种划分方法. 题解: 树形dp dp[x][0]表示是以x为根的树 ...
随机推荐
- spring cloud microservice provider and consumer
MicroService Provider:https://files.cnblogs.com/files/xiandedanteng/empCloud190824.rarMicroService C ...
- QBXT七月D1
今天是lyd神仙讲课的第一天,可以感觉到的是这位神仙有着不同于他人的气质,比如他的表情包(雾) 好了来讲正经的) 今天讲的比较多的是模拟算法和一些比赛中的好习惯 high-level 这个名词的大体意 ...
- rocketMQ 消息的 tag
tag 的使用场景:不同的消费组,订阅同一 topic 不同的 tag,拉取不同的消息并消费.在 topic 内部对消息进行隔离. producer 发送消息,指定 tag Message msg = ...
- vue中关于checkbox数据绑定v-model指令的个人理解
vue.js为开发者提供了很多便利的指令,其中v-model用于表单的数据绑定很常见, 下面是最常见的例子: <div id='myApp'> <input type=&qu ...
- fastcgi_params 与 fastcgi.conf的区别
参照文档: http://blog.51cto.com/noican/1766676
- LeetCode.1002-寻找共有字符(Find Common Characters)
这是悦乐书的第375次更新,第402篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第236题(顺位题号是1002).给定仅由小写字母组成的字符串A,返回列表中所有字符串都 ...
- P2814 家谱
我真没什么创意了woc.. so,为什么一道水题是蓝色的???哦哦哦,水好像就是蓝色的,emmm那就不是恶意评分了嘤嘤嘤 ... 好吧实际上可能是非c党对于字符串的处理需要进行编号和结构体,会麻烦一点 ...
- python2.X与3.X比较及Python编译器选择
- ajax的post请求crsftoken提交
- linux 更改文件权限命令 chmod
chmod -change file mode bits :更改文件权限 chmod是用来改变文件或者目录权限的命令,但只有文件的属主和超级用户(root)才有这种权限. 更改文件权限的2种方式: 一 ...