很容易想到树形 dp。

考虑在有根树内,每个点都有两种状态:

  1. 不选自己和父亲的边;
  2. 要选自己和父亲的边。

那么单独对于子树内部而言,就要分两种情况:

  1. 最多可以向 \(d_i\) 个孩子连边,对应上述第一种情况,我们称之为 \(f_i\);
  2. 最多可以向 \(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 题解的更多相关文章

  1. LeetCode Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  2. R画网络图

    R  画网络图 目的:用R做生信分析,画基因样本的网络图,从中观察样本的致病性情况. 一.所用到的包 library(tidyr) library(ggplot2) library(reshape2) ...

  3. LeetCode 323. Number of Connected Components in an Undirected Graph

    原题链接在这里:https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph/ 题目: Giv ...

  4. POJ 3241 曼哈顿距离最小生成树 Object Clustering

    先上几个资料: 百度文库有详细的分析和证明 cxlove的博客 TopCoder Algorithm Tutorials #include <cstdio> #include <cs ...

  5. ArcMap所有Command GUID

    The information in this topic is useful if you're trying to programmatically find a built-in command ...

  6. Open Cascade:如何从AIS_Shape导出TopoDS_Shape?

    Open Cascade:如何从AIS_Shape导出TopoDS_Shape? 实现代码如下: if( !myAISContext->HasOpenedContext()) { wxMessa ...

  7. AGC033 D~F——[ 值放到角标的DP ][ 思路+DP ][ 思路 ]

    地址:https://atcoder.jp/contests/agc033/ D Complexity dp[ i ][ j ][ k ][ l ] 表示左上角是 ( i , j ) .右下角是 ( ...

  8. 最全的ORACLE-SQL笔记

    -- 首先,以超级管理员的身份登录oracle sqlplus sys/bjsxt as sysdba --然后,解除对scott用户的锁 alter user scott account unloc ...

  9. Matplotlib数据可视化(6):饼图与箱线图

    In [1]: from matplotlib import pyplot as plt import numpy as np import matplotlib as mpl mpl.rcParam ...

  10. 2021.07.17 题解 CF1385E Directing Edges(拓扑排序)

    2021.07.17 题解 CF1385E Directing Edges(拓扑排序) CF1385E Directing Edges - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) ...

随机推荐

  1. Vue.js 事件绑定

    1.事件监听 v-on:eventName可以简写成@eventName 事件对象:在HTML中,事件参数为$event,但是即使不传递,在回调函数中也可以直接使用event读取 <div id ...

  2. oracle中id自增长(转)

    首先,你要有一张表! CREATE TABLE example( ID Number(4) NOT NULL PRIMARY KEY, NAME VARCHAR(25), PHONE VARCHAR( ...

  3. idea中yaml文件中文乱码问题解决

    idea打开yaml,或是properties文件,出现中文乱码. 解决步骤: 打开notepad++ ,新建iso-8859-1编码的空文件 将乱码文件通过notepad++直接打开,把正常显示的代 ...

  4. vscode+evernote印象笔记+markdown写在线笔记

    1. vscode安装evermonkey插件 2. vscode快捷键:Ctrl+Shift+P ,输入ever 按提示进行操作 Ever New: 创建新evernote笔记: 愉快地玩耍 点击下 ...

  5. Android 12 适配之 "Android:exported"

    Android 12 适配之 "Android:exported" 将 build.gradle 中的 targetSDKVersion 和 compileSdkVersion 改 ...

  6. ASP.NET Core EventStream (SSE) 使用以及 WebSocket 比较

    在开发环境中,对于实时数据流的需求非常常见,最常用的技术包括 Server-Sent Events (SSE) 和 WebSocket. 什么是 Server-Sent Events (SSE)? S ...

  7. 【转载】Apache Doris、DorisDB傻傻分不清。。。

    https://www.sohu.com/a/488816742_827544   相信这两天很多社区小伙伴都看到 StarRocks 所谓"开源"的动态了,开源用户群里有很多小伙 ...

  8. FISCO BCOS 控制台 部署合约 调用 查看已部署合约的地址

    deploy 部署合约.(默认提供HelloWorld合约和TableTest.sol进行示例使用) 参数: 合约路径:合约文件的路径,支持相对路径.绝对路径和默认路径三种方式.用户输入为文件名时,从 ...

  9. Qt编写物联网管理平台35-实时曲线

    一.前言 设备采集到的数据,通过曲线展示也是一种非常好的方式,尽管之前已经有了表格数据展示.设备面板展示.设备地图展示等,实时曲线也是一种不错的方式,这个用户自由选择,反正通过端口已经拿到了所有要采集 ...

  10. 跟着源码学IM(十一):一套基于Netty的分布式高可用IM详细设计与实现(有源码)

    本文由will分享,个人博客zhangyaoo.github.io,原题"基于Netty的IM系统设计与实现",有修订和重新排版. 1.引言 本文将要分享的是如何从零实现一套基于N ...