Codeforces 1240C. Paint the Tree
首先每个点 $u$ 只能选择不超过 $k$ 个相连的边
并且设边为 $(u,v)$ ,那么此时 $v$ 也必须选择这条边
因为图是一颗树,显然考虑一下树形 $dp$
设 $f[x][0/1]$ 表示考虑完 $x$ 的子树,当前节点有没有留一个选择给和父亲相连的边($0$ 表示没有)
那么对于 $f[x][0]$
考虑所有 $x$ 的儿子 $v$,我们要选出不超过 $k$ 个儿子的 $f[v][1]+val(x,v)$ ,然后剩下的儿子全部选 $f[v][0]$ ,求最大价值
(其中 $val(x,v)$ 是边 $(x,v)$ 的价值)
考虑一开始所有的儿子都先选 $f[v][0]$,对于某个儿子 $v$ 如果我们之后要选 $f[v][1]$ ,那么增加的贡献 $delta$ 为 $-f[v][0]+f[v][1]+val(x,v)$
显然 $-f[v][0]$ 是因为之前已经加入了 $f[v][0]$ 的贡献
那么此时每个儿子的选择互不影响,直接按 $delta$ 排序取前 $k$ 大即可(注意如果还没到 $k$ 个但是 $delta$ 已经小于 $0$ 了就不用选)
对于 $f[x][1]$ 也是一样的道理,但是我们这时候取的就是前 $k-1$ 的的 $delta$ 了
不妨设 $1$ 为根,那么答案即为 $f[1][0]$
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=5e5+;
int Q,n,K;
int fir[N],from[N<<],to[N<<],val[N<<],cntt;
inline void add(int a,int b,int c) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; val[cntt]=c; }
ll f[N][];
void dfs(int x,int fa)
{
vector <ll> tmp;
for(int i=fir[x];i;i=from[i])
{
int v=to[i]; if(v==fa) continue;
dfs(v,x);
f[x][]+=f[v][]; f[x][]+=f[v][];
tmp.push_back(f[v][]-f[v][]+val[i]);
}
sort(tmp.begin(),tmp.end()); reverse(tmp.begin(),tmp.end());
int sz=tmp.size();
for(int i=;i<K&&i<sz;i++)
{
if(tmp[i]<=) break;
f[x][]+=tmp[i];
if(i<K-) f[x][]+=tmp[i];
}
}
int main()
{
Q=read();
while(Q--)
{
n=read(),K=read(); int a,b,c;
for(int i=;i<=n;i++) f[i][]=f[i][]=;
for(int i=;i<=n;i++) fir[i]=; cntt=;
for(int i=;i<n;i++)
{
a=read(),b=read(),c=read();
add(a,b,c); add(b,a,c);
}
dfs(,);
printf("%lld\n",f[][]);
}
return ;
}
Codeforces 1240C. Paint the Tree的更多相关文章
- Codeforces 1244D. Paint the Tree
传送门 首先如果某个点的度数大于 $2$ 那么显然无解 然后考虑点的度数小于等于 $2$ 的情况 发现其实是一条链 一旦确定了链开头的两个点,后面的点的颜色都可以通过之前的点推出 所以直接枚举即可 # ...
- Codeforces 461B Appleman and Tree(木dp)
题目链接:Codeforces 461B Appleman and Tree 题目大意:一棵树,以0节点为根节点,给定每一个节点的父亲节点,以及每一个点的颜色(0表示白色,1表示黑色),切断这棵树的k ...
- Paint the Tree
Paint the Tree 题目来源: Moscow Pre-Finals Workshop 2018 Day 5 C 题目大意: 一棵\(n(n\le2000)\)个点的树,有\(m(2<m ...
- Codeforces 1129 E.Legendary Tree
Codeforces 1129 E.Legendary Tree 解题思路: 这题好厉害,我来复读一下官方题解,顺便补充几句. 首先,可以通过询问 \(n-1\) 次 \((S=\{1\},T=\{ ...
- Codeforces 280C Game on tree【概率DP】
Codeforces 280C Game on tree LINK 题目大意:给你一棵树,1号节点是根,每次等概率选择没有被染黑的一个节点染黑其所有子树中的节点,问染黑所有节点的期望次数 #inclu ...
- Codeforces A. Game on Tree(期望dfs)
题目描述: Game on Tree time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- E. Paint the Tree 树形dp
E. Paint the Tree 题目大意:给你一棵树,每一个点都可以染k种颜色,你拥有无数种颜色,每一种颜色最多使用2次,如果一条边的两个节点拥有同一种颜色,那么就说 这条边是饱和的,一个树的价值 ...
- Codeforces Round #781(C. Tree Infection)
Codeforces Round #781 C. Tree Infection time limit per test 1 second memory limit per test 256 megab ...
- Codeforces 196C Paint Tree(贪心+极角排序)
题目链接 Paint Tree 给你一棵n个点的树和n个直角坐标系上的点,现在要把树上的n个点映射到直角坐标系的n个点中,要求是除了在顶点处不能有线段的相交. 我们先选一个在直角坐标系中的最左下角的点 ...
随机推荐
- 预处理、const、static与sizeof-#pragma pack的作用
1:有如下代码: #include <iostream.h> #pragma pack(1) struct test{ char c; short s1; short s2; int i; ...
- jquery中mouseover和mouseenter的区别
jquery中mouseover和mouseenter的区别 一.总结 一句话总结: 见名知意:enter(进入)和over(在上方)的意思好好思考一下 mouseover就是从子元素回到自己的时候也 ...
- NProgress的使用 及 路由 token 定向的使用
主要配合路由生命周期使用 实现一个进度条的效果 使用方法: 1. 下载:npm install --save nprogress 使用:NProgress.start(); 开始 NProgress ...
- leetcode-hard-ListNode-148. Sort List
mycode 97.37% 如果用上一个题目中”参考“的方法,res中放节点而不是val,就会超时 # Definition for singly-linked list. # class Li ...
- MediaPlayer 播放视频的方法
MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.reset();//重置为初始状态 mediaPlayer.setAudioStrea ...
- Spring配置文件里加载路径中的通配符
?代表匹配任意一个字符 *代表匹配0个或多个任意字符 **/匹配任意多个目录 classpath:app-Beans.xml 查找app-Beans.xm ...
- 使用IntelliJ IDEA和Maven管理搭建+Web+Tomcat开发环境
使用IntelliJ IDEA和Maven管理搭建+Web+Tomcat开发环境 前言:原来一直使用Eclipse,换工作后使用IDEA,初识IDEA发现,哇,它的快捷键可真多啊,但是一路用下来,觉得 ...
- router-link跳转页面传递参数及页面刷新方法
使用router-link传参: 第一种: 路径:http://localhost:8080/goodListP?id=2 跳转的页面获取参数: this.$route.query.id 第二种: 路 ...
- (转载)详解Javascript中prototype属性(推荐)
在典型的面向对象的语言中,如java,都存在类(class)的概念,类就是对象的模板,对象就是类的实例.但是在Javascript语言体系中,是不存在类(Class)的概念的,javascript中不 ...
- Android studio无法发现nexus 5x解决方法
原因: 没有安装adb interface驱动. 解决方法: 1. 使用Android device manager下载google usb driver. 2. 打开设备管理器,右键带黄色感叹号 ...