题目描述

In a village called Byteville, there are houses connected with N-1 roads. For each pair of houses, there is a unique way to get from one to another. The houses are numbered from 1 to . The house no. 1 belongs to the village administrator Byteasar. As part of enabling modern technologies for rural areas framework, computers have been delivered to Byteasar's house. Every house is to be supplied with a computer, and it is Byteasar's task to distribute them. The citizens of Byteville have already agreed to play the most recent version of FarmCraft (the game) as soon as they have their computers. Byteasar has loaded all the computers on his pickup truck and is about to set out to deliver the goods. He has just the right amount of gasoline to drive each road twice. In each house, Byteasar leaves one computer, and immediately continues on his route. In each house, as soon as house dwellers get their computer, they turn it on and install FarmCraft. The time it takes to install and set up the game very much depends on one's tech savviness, which is fortunately known for each household. After he delivers all the computers, Byteasar will come back to his house and install the game on his computer. The travel time along each road linking two houses is exactly 1 minute, and (due to citizens' eagerness to play) the time to unload a computer is negligible. Help Byteasar in determining a delivery order that allows all Byteville's citizens (including Byteasar) to start playing together as soon as possible. In other words, find an order that minimizes the time when everyone has FarmCraft installed.

mhy住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子。

mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒软件,第i个妹子安装时间为

树上的每条边mhy能且仅能走两次,每次耗费1单位时间。mhy送完所有电脑后会回自己家里然后开始装zhx牌杀毒软件。

卸货和装电脑是不需要时间的。

求所有妹子和mhy都装好zhx牌杀毒软件的最短时间。

输入格式

The first line of the standard input contains a single integer N(2<=N<=5 00 000) that gives the number of houses in Byteville.

The second line contains N integers

, separated by single spaces;

is the installation time (in minutes) for the dwellers of house no. i.

The next N-1 lines specify the roads linking the houses. Each such line contains two positive integers a and b(1<=a<b<=N) , separated by a single space. These indicate that there is a direct road between the houses no. a and b.

输出格式

The first and only line of the standard output should contain a single integer: the (minimum) number of minutes after which all citizens will be able to play FarmCraft together.

样例

样例输入

6
1 8 9 6 3 2
1 3
2 3
3 4
4 5
4 6

样例输出

11

数据范围与提示

Explanation: Byteasar should deliver the computers to the houses in the following order: 3, 2, 4, 5, 6, and 1. The game will be installed after 11, 10, 10, 10, 8, and 9 minutes respectively, in the house number order. Thus everyone can play after 11 minutes.

If Byteasar delivered the game in the following order: 3, 4, 5, 6, 2, and 1, then the game would be installed after: 11, 16, 10, 8, 6, and 7 minutes respectively. Hence, everyone could play only after 16 minutes,

题解:设f[x]为x的子树中全部安完软件的时间,对于x节点的a,b儿子,设他们的子树大小为siz[a],siz[b],显然遍历一遍子树a的时间就是2*siz[a],然后分类讨论

若先走a,后走b,那么f[x]=max(f[a]+1,f[b]+2*siz[a]+1)
若先走b,后走a,那么f[x]=max(f[a]+2*siz[b]+1,f[b]+1)

对于f[a]+1和f[b]+1我们可以不用考虑,那么如果先走a更优,当且仅当:f[b]+2*siz[a]+1<f[a]+2*siz[b]+1

移项后:

f[a]-2*siz[a]>f[b]-2*siz[b]

那么直接将x的儿子按照f[i]-2*siz[i]排序,先走f[i]-2*siz[i]的就好了

注意根节点要特判,ans=max(2*(n-1)+1+time[1],f[1])

代码:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 500500
using namespace std;
struct abcd{
int to,next;
}table[M<<];
int head[M],tot;
int n;
int a[M],f[M],g[M];
//f表示遍历所需时间,g表示遍历并装机完毕所需时间
void Add(int x,int y)
{
table[++tot].to=y;
table[tot].next=head[x];
head[x]=tot;
}
bool Compare(int x,int y)
{
return g[x]-f[x] > g[y]-f[y];
}
void Tree_DP(int x,int from)
{
int i;
for(i=head[x];i;i=table[i].next)
if(table[i].to!=from)
Tree_DP(table[i].to,x);
static int stack[M];int top=;
for(i=head[x];i;i=table[i].next)
if(table[i].to!=from)
{
f[table[i].to]+=;
g[table[i].to]=max(g[table[i].to]+,f[table[i].to]);
stack[++top]=table[i].to;
}
sort(stack+,stack+top+,Compare);
g[x]=a[x];
for(i=;i<=top;i++)
{
g[x]=max(g[x],f[x]+g[stack[i]]);
f[x]+=f[stack[i]];
}
}
int main()
{
int i,x,y;
cin>>n;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
Add(x,y);Add(y,x);
}
Tree_DP(,);
cout<<max(g[],f[]+a[])<<endl;
return ;
}

FarmCraft --(树形DP)的更多相关文章

  1. bzoj 3829: [Poi2014]FarmCraft 树形dp+贪心

    题意: $mhy$ 住在一棵有 $n$ 个点的树的 $1$ 号结点上,每个结点上都有一个妹子. $mhy$ 从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装 $zhx$ 牌杀毒 ...

  2. BZOJ3829[Poi2014]FarmCraft——树形DP+贪心

    题目描述 In a village called Byteville, there are   houses connected with N-1 roads. For each pair of ho ...

  3. 【BZOJ3829】[Poi2014]FarmCraft 树形DP(贪心)

    [BZOJ3829][Poi2014]FarmCraft Description In a village called Byteville, there are   houses connected ...

  4. poj3417 LCA + 树形dp

    Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4478   Accepted: 1292 Descripti ...

  5. COGS 2532. [HZOI 2016]树之美 树形dp

    可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...

  6. 【BZOJ-4726】Sabota? 树形DP

    4726: [POI2017]Sabota? Time Limit: 20 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 128  Solved ...

  7. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  8. 树形DP

    切题ing!!!!! HDU  2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...

  9. BZOJ 2286 消耗战 (虚树+树形DP)

    给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...

  10. POJ2342 树形dp

    原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...

随机推荐

  1. (26)ASP.NET Core EF保存(基本保存、保存相关数据、级联删除、使用事务)

    1.简介 每个上下文实例都有一个ChangeTracker,它负责跟踪需要写入数据库的更改.更改实体类的实例时,这些更改会记录在ChangeTracker中,然后在调用SaveChanges时会被写入 ...

  2. day06可变与不可变类型,if判断,运算符

    1:可变不可变类型 2.什么是条件?什么可以当做条件?为何要要用条件? 显式布尔值:True.False 隐式布尔值:所有数据类型,其中0.None.空为假 3:逻辑运算符:用来 # not. and ...

  3. openwrt 上的 upnp wifi 音频推送 gmediarender

    首先是必须启用的模块 Libraries ---> <*> libupnp Sound ---> <*> alsa-utils<*> madplay-a ...

  4. Sublime text 3 运行python3

    要在Sublime text3编译器中成功运行 python3,需要在编译器设置中将python3添加至编译器中 新建编译系统 编辑弹出的文件,添加如下内容: { "cmd":[& ...

  5. vue项目 github 上传项目并链接地址

    git init git init: 通过命令git init把这个文件夹变成Git可管理的仓库git status git status:查看当前仓库状态 git add . 这里提示你虽然把项目粘 ...

  6. turtle学习笔记

    1.turtle的绘图窗体 turtle.setup(width, height, startx,starty) - setup()设置窗体大小及位置- 4个参数中后两个可选(后两个省略时默认窗口在屏 ...

  7. USB概述及协议基础

    USB概述及协议基础 USB的拓扑结构 USB是一种主从结构的系统.主机叫做Host,从机叫做Device(也叫做设备). 通常所说的主机具有一个或者多个USB主控制器(host controller ...

  8. 小程序post请求,后台接收不到数据的解决方法

    wx.request({ url: 'myurl', method:'POST', dataType:'json', data: { mydata:mydata }, success(res) { c ...

  9. 我成功攻击了Tomcat服务器之后

    Tomcat是一个开源的轻量级Web应用服务器,在我们平常工作过程中接触得非常多.代码也非常经典,很多人为了提升自己的技术也会去阅读学习Tomcat的源码.但正如著名诗人李白所说的:世界上本没有漏洞, ...

  10. Git在公司内部的使用规范

    Git在公司内部的使用规范 目录 Git在公司内部的使用规范 1.版本定义 2.系统开发环境 3. 分支定义 4.Commit 日志规范 5.开发工作流程: 5.1.常规分支debug流程: 5.2. ...