2017 日照夏令营 day5 t2 tree

题目大意:
给定一棵 个节点的树,树上的每个节点 有一个权值 ,每次操作中你可以选择一个包含1号节点的连通子树,将这个连
通子树上所有节点的权值加上(或减去)一个相同的非负整数,要求将所有节点变为0,且最小化所有加上(或减去)的
非负整数之和

思路:
以1为根,我们应该先把深度大的点变成0,再把深度小的点变成0
换句话来说,我们想让当前点x变为0,应该先把它的儿子都变为0,这是一个递归定义的过程
树形DP
f[i]表示把以i为根的子树都变为0时在i上的最少累加值
g[i]表示把以i为根的子树都变为0的在i上的最少累减值
f[i]=max(f[i],f[v]),g[i]=max(g[i],g[v])
v是i的儿子
设点i的权值为a[i], t=f[i]-g[i]+a[i]
如果t等于0,那么非常好,不用再在i上加减了
如果t大于0,那么我们就要在这个点减去t,即g[i]+=t
如果t小于0 ,那么我们就要在这个点加上t,即f[i]+=t
复杂度O(n)

#include<iostream>
#include<cstdio>
#include<algorithm>
#define M 300005
using namespace std;
int x,y,n,s;
int a[M],head[M];
long long f[M],g[M];//f[]累加,g[]累减 
struct node{
    int from,to;
}list[M*2];
void add(int x,int y){
    list[++s].from=head[x];
    list[s].to=y;
    head[x]=s;
}
void dfs(int x,int fa){
    for (int i=head[x];i;i=list[i].from){
        int v=list[i].to;
        if (v==fa) continue;
        dfs(v,x);
        g[x]=max(g[x],g[v]);
        f[x]=max(f[x],f[v]);
    }
    long long t=f[x]-g[x]+a[x];
    if (t>0) g[x]+=t;
    else f[x]-=t;
}
int main(){
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        add(x,y);add(y,x);
    }
    dfs(1,0);
    cout<<f[1]+g[1];
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Summary of Notable New Features in 15.7 •We added support to change installation locations. •You can Save All your pending changes before you start your update. •The update dialog provides you even more details about your update during installation. •C# 7.3 is included in Visual Studio version 15.7. •We improved solution load time for C# and VB projects. •We made numerous updates to F# and its tools, with a focus on performance. •We reduced the time to enable IntelliSense for large .NET Core projects by 25%. •We made Quick Info improvements and new .NET refactorings like convert for-to-foreach and make private fields readonly. •We added the ability to publish ASP.NET Core applications to App Service Linux without containers. •Live Unit Testing works with embedded pdbs and supports projects that use reference assemblies. •The Test Explorer has more responsive icons during test runs. •C++ developers can use CodeLens for unit testing. •We added new rules enforcing items from the C++ Core Guidelines. •Debugging large solutions with /Debug:fastlink PDBs is more robust. •CMake integration supports CMake 3.11 and static analysis. •Python projects support type hints in IntelliSense, and a Run MyPy command has been added to look for typing errors in your code. •Conda environments are supported in Python projects. •We added a next version of our Python debugger based on the popular open source pydevd debugger. •TypeScript 2.8 is included in Visual Studio version 15.7. •We improved Kestrel HTTPs support during debugging. •We added support for JavaScript debugging with Microsoft Edge. •The Debugger supports VSTS and GitHub Authentication for Source Link. •IntelliTrace's step-back debugging feature is supported for debugging .NET Core projects. •We added IntelliTrace support for taking snapshots on exceptions. •We removed the blocking modal dialog from branch checkouts in Git when a solution or project reload is not required. •You have the option to choose between OpenSSL and SChannel in Git. •You can create and associate Azure Key Vaults from within the Visual Studio IDE. •Visual Studio Tools for Xamarin can automatically install missing Android API levels required by Xamarin.Android projects. •The Xamarin.Forms XAML editor provides IntelliSense and quick fixes for conditional XAML. •Visual Studio Build Tools now supports installing into a container, and we added support for building Azure, UWP, and additional project types. •You can create build servers without installing all of Visual Studio. •The Windows 10 April 2018 Update SDK - Build 17134 is the default required SDK for the Universal Windows Platform development workload. •We added support for Visual State Management for all UWP apps and more. •We enabled automatic updates for sideloaded APPX packages. •You have new tools for migrating to NuGet PackageReference. •We added support for NuGet package signatures. •We added Service Fabric Tooling for the 6.2 Service Fabric release. •We updated Entity Framework Tools to work with the EF 6.2 runtime and to improve reverse engineering of existing databases.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值