题目

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 C1,C2……Cn(1<= C<=109), separated by single spaces; Ci 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,

解说

首先可以明确一个点的安装时间等于走到这个点的时间+安装时间,最后总时间就是所有点的时间中最长的。

刚开始做这道题时深受昨天的Salesman影响,觉得还是需要贪心思想(虽然确实是用贪心,但最开始走偏了……),结合样例一想,突发奇想觉得时间长的肯定是需要先走,那么我就把所有儿子的时间排个序,先走时间长的,再走时间短的,走下一个的时间中再加上之前走兄弟的子树的时间即可。

然后呢?喜提38分。

再仔细想想这个贪心确实不对啊。万一有一个时间很短的儿子,但是这个儿子下面却还有时间很长的后代,那么它还是应该先选的。

这不就尴尬了吗……(万恶的样例)

那么既然这样,我们就不能先给儿子排序再遍历,而是应该先遍历每个儿子再根据遍历结果排序。

(下文中f[i]代表 i 节点所有子树中最长的时间,a[i]代表 i 节点的时间,size[i]代表 i 子树的规模)

假设 u 节点有儿子 x 和 y ,则如果先走 x 的话 u 的时间就为max(f[x]+1,f[y]+2*size[x]+1);同理,先走 y 的话 u 的时间就为max(f[y]+1,f[x]+2*size[y]+1),若先安装x合适,则必有2*size[x]+f[y]+1>2*size[y]+f[x]+1,即f[x]-2*size[y]<f[y]-2*size[x],既然这样,我们就按照f[]-size[]排序即可。

(下面的代码里还有部分解说)

代码

 #include<bits/stdc++.h>
using namespace std;
const int maxn=+;
int t,a[maxn],head[maxn],n,tot,f[maxn],size[maxn],q[maxn];
struct node{
int to,next;
}e[*maxn];
inline int read(){
int s=,w=;
char ch=getchar();
while(ch<''||ch>''){if(ch=='-')w=-;ch=getchar();}
while(ch>=''&&ch<='') s=s*+ch-'',ch=getchar();
return s*w;
}
bool cmp(int x,int y){//比较函数
return f[x]-*size[x]>f[y]-*size[y];
}
void Add(int from,int to){
e[tot].to=to;
e[tot].next=head[from];
head[from]=tot;
tot++;
}
void dfs(int u,int fa){
int cnt=,sum=;//sum为走u子树目前的总时间
//cnt为儿子的数量
if(u==) f[u]=;
else f[u]=a[u];
size[u]=;//叶子节点没有子树size就会存为1
for(int i=head[u];i;i=e[i].next){
int v=e[i].to;
if(v!=fa){
dfs(v,u);
size[u]+=size[v];//统计规模
}
}
for(int i=head[u];i;i=e[i].next) if(e[i].to!=fa) q[++cnt]=e[i].to;
//q数组一定要用一个,不要一个dfs开一个,会直接爆掉(亲身经历)
sort(q+,q++cnt,cmp);//贪心排序
for(int i=;i<=cnt;i++){
f[u]=max(f[u],f[q[i]]+sum);
sum+=*size[q[i]];
}
}
int main(){
tot=;
n=read();
for(int i=;i<=n;i++) a[i]=read();
for(int i=;i<=n-;i++){
int x=read(),y=read();
Add(x,y);
Add(y,x);
}
dfs(,);
printf("%d",max(f[],a[]+*(n-)));
return ;
}

幸甚至哉,歌以咏志。

[POI2014][树形DP]FarmCraft的更多相关文章

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

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

  2. 【BZOJ3872】[Poi2014]Ant colony 树形DP+二分

    [BZOJ3872][Poi2014]Ant colony Description 给定一棵有n个节点的树.在每个叶子节点,有g群蚂蚁要从外面进来,其中第i群有m[i]只蚂蚁.这些蚂蚁会相继进入树中, ...

  3. 【BZOJ3522】[Poi2014]Hotel 树形DP

    [BZOJ3522][Poi2014]Hotel Description 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房 ...

  4. 【BZOJ3522】【BZOJ4543】【POI2014】Hotel 树形DP 长链剖分 启发式合并

    题目大意 ​ 给你一棵树,求有多少个组点满足\(x\neq y,x\neq z,y\neq z,dist_{x,y}=dist_{x,z}=dist_{y,z}\) ​ \(1\leq n\leq 1 ...

  5. BZOJ3522[Poi2014]Hotel——树形DP

    题目描述 有一个树形结构的宾馆,n个房间,n-1条无向边,每条边的长度相同,任意两个房间可以相互到达.吉丽要给他的三个妹子各开(一个)房(间).三个妹子住的房间要互不相同(否则要打起来了),为了让吉丽 ...

  6. 树形DP水题系列(1):FAR-FarmCraft [POI2014][luogu P3574]

    题目 大意: 边权为1 使遍历树时到每个节点的时间加上点权的最大值最小 求这个最小的最大值 思路: 最优化问题 一眼树形DP 考虑状态设立 先直接以答案为状态 dp[u] 为遍历完以u为根的子树的答案 ...

  7. poj3417 LCA + 树形dp

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

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

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

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

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

随机推荐

  1. HTML简单的提示框

    由于项目中需要一个简单的提示框,就是鼠标放上去,可以提示相关信息,引用第三方的比较麻烦,所以,这里封装了一个很简单的HTML方法. <script src="http://cdn.st ...

  2. 简单易懂的Servlet路径问题

    关于servlet路径,我看了一下网上别人的博客园,发现都有一个通病,讲的太专业了,又抓不住关键部分,往往看一眼就不想看第二眼.所以我特地准备了初学者所通识的servlet路径问题. 1.标识符 /j ...

  3. .netCore下的jwt的梳理-->借鉴于“老张的哲学”

    之前在公司的项目中有用到jwt进行token验证,但是公司里用的框架已经集成好了jwt,所以对jwt的的了解不够清晰,感觉还是隔着一层.在看了“老张的哲学”的jwt部分后对jwt的认识才更加深刻了一些 ...

  4. disruptor 核心概念 二

    一.Disruptor图解 二.disruptor核心概念 1.RingBuffer到底是啥?正如名字所说的一样,他是一个环(首尾相接的环)它用做在不同上下文(线程)间传递数据的buffer Ring ...

  5. 第四章、深入理解vue组件

    4-1.使用组件的细节 a.使用is解决html出现bug 如下 table下面应该为tr,所以页面渲染的时候没有找到tr是有问题的,所以是有小bug,所以table中必须是tr b.改上面bug,t ...

  6. 量子计算机编程(二)——QPU基础函数

    第二部分主要是QPU的基础功能,第一部分就像是我们有了哪些基本的语句,第二部分就是我们能写一些简单基础的函数,一些小模块,第三部分就是他的应用了. 先来看一下一个简单量子应用的结构: 第一步,将量子态 ...

  7. NLP(二十五)实现ALBERT+Bi-LSTM+CRF模型

      在文章NLP(二十四)利用ALBERT实现命名实体识别中,笔者介绍了ALBERT+Bi-LSTM模型在命名实体识别方面的应用.   在本文中,笔者将介绍如何实现ALBERT+Bi-LSTM+CRF ...

  8. linux4.1.36 解决 SPI 时钟找不到 不生成设备 device

    最初的问题是 编译内核添加了 spi 支持,配置了 board 后,加载25q64驱动不执行probe 函数. 然后发现是,spi-s3c24xx.c 中的 probe 没有执行完就退出了 没有生成 ...

  9. drf呼啦圈

    呼啦圈 1.1 表结构设计 不会经常变化的值放在内存:choices形式,避免跨表性能低. 分表:如果表中列太多/大量内容可以选择水平分表 表自关联 from django.db import mod ...

  10. Docker深入浅出系列 | 5分钟搭建你的私有镜像仓库

    Docker已经上市很多年,不是什么新鲜事物了,很多企业或者开发同学以前也不多不少有所接触,但是有实操经验的人不多,本系列教程主要偏重实战,尽量讲干货,会根据本人理解去做阐述,具体官方概念可以查阅官方 ...