【BZOJ3829】[Poi2014]FarmCraft

Description

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个妹子安装时间为Ci。
树上的每条边mhy能且仅能走两次,每次耗费1单位时间。mhy送完所有电脑后会回自己家里然后开始装zhx牌杀毒软件。
卸货和装电脑是不需要时间的。
求所有妹子和mhy都装好zhx牌杀毒软件的最短时间。

Input

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<=Ci<=10^9), 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.

Output

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.

Sample Input

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

Sample Output

11

HINT

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 <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=500010;
int n,cnt;
int to[maxn<<1],next[maxn<<1],head[maxn],v[maxn],p[maxn],f[maxn],siz[maxn];
void add(int a,int b)
{
to[cnt]=b,next[cnt]=head[a],head[a]=cnt++;
}
bool cmp(int a,int b)
{
return f[a]-2*siz[a]>f[b]-2*siz[b];
}
void dfs(int x,int fa)
{
int i,sum=0;
siz[x]=1;
for(i=head[x];i!=-1;i=next[i]) if(to[i]!=fa)
dfs(to[i],x),siz[x]+=siz[to[i]];
p[0]=0;
for(i=head[x];i!=-1;i=next[i]) if(to[i]!=fa) p[++p[0]]=to[i];
sort(p+1,p+p[0]+1,cmp);
if(x!=1) f[x]=v[x];
for(i=1;i<=p[0];i++) f[x]=max(f[x],f[p[i]]+sum+1),sum+=2*siz[p[i]];
}
int main()
{
scanf("%d",&n);
int i,a,b;
memset(head,-1,sizeof(head));
for(i=1;i<=n;i++) scanf("%d",&v[i]);
for(i=1;i<n;i++) scanf("%d%d",&a,&b),add(a,b),add(b,a);
dfs(1,0);
printf("%d",max(f[1],2*(siz[1]-1)+v[1]));
return 0;
}

【BZOJ3829】[Poi2014]FarmCraft 树形DP(贪心)的更多相关文章

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

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

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

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

  3. [POI2014]FAR-FarmCraft 树形DP + 贪心思想

    (感觉洛谷上题面那一小段中文根本看不懂啊,好多条件都没讲,直接就是安装也要一个时间啊,,,明明不止啊!还好有百度翻译......) 题意:一棵树,一开始在1号节点(root),边权都为1,每个点有点权 ...

  4. POI2014 FAR-FarmCraft 树形DP+贪心

    题目链接 https://www.luogu.org/problem/P3574 题意 翻译其实已经很明确了 分析 这题一眼就是贪心啊,但贪心的方法要思索一下,首先是考虑先走时间多的子树,但不太现实, ...

  5. [bzoj3829][Poi2014]FarmCraft_树形dp

    FarmCraft 题目链接:https://lydsy.com/JudgeOnline/problem.php?id=3829 数据范围:略. 题解: 因为每条边只能必须走两次,所以我们的路径一定是 ...

  6. 【bzoj4027】[HEOI2015]兔子与樱花 树形dp+贪心

    题目描述 很久很久之前,森林里住着一群兔子.有一天,兔子们突然决定要去看樱花.兔子们所在森林里的樱花树很特殊.樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1个树枝连接,我们可以把它 ...

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

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

  8. [BZOJ1596] [Usaco2008 Jan]电话网络(树形DP || 贪心)

    传送门 1.树形DP #include <cstdio> #include <cstring> #include <iostream> #define N 1000 ...

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

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

随机推荐

  1. PHP连接Azure Redis

    概述 Azure Redis缓存基于流行的开源Redis缓存,可以通过各种Redis客户端进行访问,这些客户端适用于许多编程语言.每个客户端有自身的API,用于通过Redis命令调用Redis缓存实例 ...

  2. C# OO(初级思想)

    继承,多态,封装 在C#中,为了能够合理描述自然界的规律,面向对象的编程引入了继承的概念,是面向对象编程中最重要的概念之一,定义了如何根据现有的类创建新类的过程. 继承:一个类派生出来的子类具有这个类 ...

  3. ubuntu12.04打开某一个已安装的软件的方法

    1.快捷键win+A,里面显示已安装的软件 2.打开左上角的dash home,即ubuntu标志图,输入想要打开的软件 还有其它方法,探索中... .

  4. [position]返回顶部

    position:fixed;实现 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  5. poj 2846 Repository

    题目大意:给你n个字符串,然后给你m个子串,看这个子串在上面的多少个串中,出现过: 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2846 本题可以在字 ...

  6. hdoj 1053 Entropy(用哈夫曼编码)优先队列

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1053 讲解: 题意:给定一个字符串,根据哈夫曼编码求出最短长度,并求出比值. 思路:就是哈夫曼编码.把 ...

  7. iOS直播-播放基于RTMP协议的视频

    iOS直播-播放基于RTMP协议的视频 流媒体协议介绍 1. 伪流媒体: 渐进式下载 : 边下边存, 文件会保存 使用http协议,也能够实现视频播放, 也能快进快退等, 体验上跟流媒体很像. 优酷, ...

  8. Unity 扩展编辑器

    扩展Inspector界面 继承自Editor,添加CustomEditorAttribute,传入定制的类型 定制显示的类型要求: 类型中所有的public 字段都会自动暴露给Inspector编辑 ...

  9. 线程的sleep()方法和yield()方法有什么区别?

    1.sleep()方法给其他线程运行机会时不考虑线程的优先级,因此会给低优先级的线程以运行的机会 2.yield()方法只会给相同优先级或更高优先级的线程以运行的机会 3.线程执行sleep()方法后 ...

  10. js json转换

    第一种方式: 使用js函数eval(); testJson=eval(testJson);是错误的转换方式. 正确的转换方式需要加(): testJson = eval("(" + ...