[ABC259F] Select Edges 题解
很容易想到树形 dp。
考虑在有根树内,每个点都有两种状态:
- 不选自己和父亲的边;
- 要选自己和父亲的边。
那么单独对于子树内部而言,就要分两种情况:
- 最多可以向 \(d_i\) 个孩子连边,对应上述第一种情况,我们称之为 \(f_i\);
- 最多可以向 \(d_i-1\) 个孩子连边,对应上述第二种情况,我们称之为 \(dp_i\)。
最基本的状态是不选自己和子树的连边,答案即为 \(\sum\limits_{j\in ison} f_j\)。
然后发现每次连 \((i,j)\) 这条边,答案会加上 \(mx_j=dp_j+w_{(i,j)}-f_j\)。
那么对于 \(f_i\),就可以挑选前 \(d_i\) 大的 \(mx_j\),答案加上所有 \(>0\) 的 \(mx\) 值。\(dp_i\) 同理。
时间复杂度 \(O(n\log n)\)。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5+5;
int n,k,u[N],v[N],d[N],r[N];
int m,h[N],to[N*2],nxt[N*2];
ll w[N*2],mx[N],f[N],dp[N];
int cmp(ll x,ll y){return x>y;}
void add(int x,int y,ll z){
to[++m]=y;
w[m]=z;
nxt[m]=h[x];
h[x]=m;
}void dfs(int x,int fa){
for(int i=h[x];i;i=nxt[i]){
int y=to[i];
if(y==fa) continue;
dfs(y,x);
dp[x]+=f[y];
}int k=0;
for(int i=h[x];i;i=nxt[i])
if(to[i]!=fa)
mx[++k]=dp[to[i]]+w[i]-f[to[i]];
sort(mx+1,mx+k+1,cmp);
for(int i=1;i<d[x];i++){
if(mx[i]<=0) break;
dp[x]+=mx[i];
}f[x]=dp[x];
if(mx[d[x]]>0) f[x]+=mx[d[x]];
if(!d[x]) dp[x]=-1e9;
for(int i=1;i<=k;i++) mx[i]=0;
}int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++) cin>>d[i];
for(int i=1;i<n;i++){
int x,y,z;
cin>>x>>y>>z;
add(x,y,z);
add(y,x,z);
}dfs(1,0);
cout<<f[1];
return 0;
}
[ABC259F] Select Edges 题解的更多相关文章
- LeetCode Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- R画网络图
R 画网络图 目的:用R做生信分析,画基因样本的网络图,从中观察样本的致病性情况. 一.所用到的包 library(tidyr) library(ggplot2) library(reshape2) ...
- LeetCode 323. Number of Connected Components in an Undirected Graph
原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...
- POJ 3241 曼哈顿距离最小生成树 Object Clustering
先上几个资料: 百度文库有详细的分析和证明 cxlove的博客 TopCoder Algorithm Tutorials #include <cstdio> #include <cs ...
- ArcMap所有Command GUID
The information in this topic is useful if you're trying to programmatically find a built-in command ...
- Open Cascade:如何从AIS_Shape导出TopoDS_Shape?
Open Cascade:如何从AIS_Shape导出TopoDS_Shape? 实现代码如下: if( !myAISContext->HasOpenedContext()) { wxMessa ...
- AGC033 D~F——[ 值放到角标的DP ][ 思路+DP ][ 思路 ]
地址:https://atcoder.jp/contests/agc033/ D Complexity dp[ i ][ j ][ k ][ l ] 表示左上角是 ( i , j ) .右下角是 ( ...
- 最全的ORACLE-SQL笔记
-- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...
- Matplotlib数据可视化(6):饼图与箱线图
In [1]: from matplotlib import pyplot as plt import numpy as np import matplotlib as mpl mpl.rcParam ...
- 2021.07.17 题解 CF1385E Directing Edges(拓扑排序)
2021.07.17 题解 CF1385E Directing Edges(拓扑排序) CF1385E Directing Edges - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) ...
随机推荐
- Astro v5 x DevNow
先介绍下 DevNow DevNow Github 体验网站 DevNow 是一个精简的开源技术博客项目模版,支持 Vercel 一键部署,支持评论.搜索等功能,欢迎大家体验.同时也支持 Follow ...
- DataGridView频繁更新PLC报警信息数据源
1.问题描述 当DataGridView频繁更新数据源时,可能会导致界面闪烁.性能下降等问题.这是因为每次更新数据源时,DataGridView都需要重新绘制和绑定数据,这是一个相对耗时的过程. 2. ...
- StarBlog博客Vue前端开发笔记:(2)页面路由
前言 Vue.js 使用虚拟 DOM 处理单页面,然后使用 Webpack 打包.通过上一篇文章的例子,读者也许已经发现,无论语法和写法如何不同,Vue.js 程序打包后都是一个单一的 HTML 文件 ...
- docker save与docker export实现docker镜像与容器的备份
本来想写一篇关于docker save/export/commit/load/import之间的关系的文章,后来看了看,已经有很多人写过了,我就不做重复工作了. 参见: docker save与doc ...
- helm values reference other values
https://helm.sh/docs/chart_template_guide/yaml_techniques/#yaml-anchors https://helm.sh/zh/docs/char ...
- git Already up-to-date解决办法,强制覆盖本地代码
1.拉取最新云端代码强制覆盖本地代码 git fetch --all git reset --hard origin/master git pull 2.git将分支合并到主master,出现这个结果 ...
- Python中导入模块的import命令的语法
- [转]在MyBatis中使用pageHelper5.1.9分页插件实现物理分页
pagehelper的GIT地址:https://github.com/pagehelper/Mybatis-PageHelper/ 废话少说,直接给出中文官方链接: 1.如何使用分页插件 2.HOW ...
- 即时通讯技术文集(第44期):微信、QQ技术精华合集(Part1) [共14篇]
为了更好地分类阅读 52im.net 总计1000多篇精编文章,我将在每周三推送新的一期技术文集,本次是第44 期. [-1-] 微信朋友圈千亿访问量背后的技术挑战和实践总结 [链接] http:/ ...
- blip2代码解析
请你作为一个代码翻译解释生成器,下面我会发送一个github链接给你,请你详细解析与介绍这个链接下代码 好的,请发送链接. https://github.com/huggingface/transfo ...