题目描述

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牌杀毒软件的最短时间。

输入

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.

输出

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[i]表示以i为根节点的子树中最长耗时是多少(是指从i开始走的最长耗时)。
对于一个点i的两个子节点a,b(其中size[]代表子树大小):
如果先遍历a再遍历b,则a的耗时是1+f[a],b的耗时是1+2*size[a]+f[b]
如果先遍历b再遍历a,则a的耗时是1+2*size[b]+f[a],b的耗时是1+f[b]
f[i]=min(max(f[a],2*size[a]+f[b]),max(f[b],2*size[b]+f[a]))
到这一步就可以按上式排序遍历了,但还可以再进一步简化。
我们分类讨论上式两个最大值分别是什么:
1、f[a]>2*size[a]+f[b];f[b]>2*size[b]+f[a],这种情况显然是不可能的
2、f[a]>2*size[a]+f[b];2*size[b]+f[a]>f[b],显然2*size[b]+f[a]>f[a]>2*size[a]+f[b],这种情况优先遍历a更优
即2*size[b]+f[a]>2*size[a]+f[b],f[a]-2*size[a]>f[b]-2*size[b],也就是优先遍历f[]-2*size[]大的
3、2*size[a]+f[b]>f[a];f[b]>2*size[b]+f[a],显然2*size[a]+f[b]>f[b]>2*size[b]+f[a],这种情况优先遍历b更优
即2*size[a]+f[b]>2*size[b]+f[a],f[b]-2*size[b]>f[a]-2*size[a],同样是优先遍历f[]-2*size[]大的
4、2*size[a]+f[b]>f[a];2*size[b]+f[a]>f[b],假设遍历a更优,反过来同样
即2*size[a]+f[b]<2*size[b]+f[a],f[b]-2*size[b]<f[a]-2*size[a],依然是优先遍历f[]-2*size[]大的
那么只要按f[]-2*size[]排序之后更新f[i]就好了。
因为1号节点要求回来再安装,因此将f[1]初始成0,最后再比较一下f[1]和2*size[1]-2+c[1]的大小即可。
  1. #include<set>
  2. #include<map>
  3. #include<queue>
  4. #include<stack>
  5. #include<cmath>
  6. #include<vector>
  7. #include<cstdio>
  8. #include<cstring>
  9. #include<iostream>
  10. #include<algorithm>
  11. #define ll long long
  12. using namespace std;
  13. int c[500010];
  14. int n;
  15. int head[500010];
  16. int to[1000010];
  17. int next[1000010];
  18. int size[500010];
  19. int fa[500010];
  20. ll f[500010];
  21. int q[500010];
  22. int x,y;
  23. int tot;
  24. void add(int x,int y)
  25. {
  26. tot++;
  27. next[tot]=head[x];
  28. head[x]=tot;
  29. to[tot]=y;
  30. }
  31. bool cmp(int x,int y)
  32. {
  33. return f[x]-2*size[x]>f[y]-2*size[y];
  34. }
  35. void dfs(int x)
  36. {
  37. int cnt=0;
  38. int sum=1;
  39. size[x]=1;
  40. f[x]=c[x];
  41. if(x==1)
  42. {
  43. f[x]=0;
  44. }
  45. for(int i=head[x];i;i=next[i])
  46. {
  47. if(to[i]!=fa[x])
  48. {
  49. fa[to[i]]=x;
  50. dfs(to[i]);
  51. size[x]+=size[to[i]];
  52. }
  53. }
  54. for(int i=head[x];i;i=next[i])
  55. {
  56. if(to[i]!=fa[x])
  57. {
  58. q[++cnt]=to[i];
  59. }
  60. }
  61. sort(q+1,q+1+cnt,cmp);
  62. for(int i=1;i<=cnt;i++)
  63. {
  64. f[x]=max(f[x],f[q[i]]+sum);
  65. sum+=2*size[q[i]];
  66. }
  67. }
  68. int main()
  69. {
  70. scanf("%d",&n);
  71. for(int i=1;i<=n;i++)
  72. {
  73. scanf("%d",&c[i]);
  74. }
  75. for(int i=1;i<n;i++)
  76. {
  77. scanf("%d%d",&x,&y);
  78. add(x,y);
  79. add(y,x);
  80. }
  81. dfs(1);
  82. printf("%lld",max(2ll*(n-1)+c[1],f[1]));
  83. }

BZOJ3829[Poi2014]FarmCraft——树形DP+贪心的更多相关文章

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

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

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

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

  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. Android BaseAdapter加载多个不同的Item布局时出现UncaughtException in Thread main java.lang.ArrayIndexOutOfBoundsException: length=15; index=15

    java.lang.ArrayIndexOutOfBoundsException: length=15; index=15 异常出现的场景:在做聊天界面时,需要插入表情,图片,文字,名片,还有几种较为 ...

  2. JDK 升级问题小结

    JDK8 发布很久了,它提供了许多吸引人的新特性,能够提高编程效率. 如果是新的项目,使用 JDK8 当然是最好的选择.但是,对于一些老的项目,升级到 JDK8 则存在一些兼容性问题,是否升级需要酌情 ...

  3. 【webstorm】注册码 更新笔记

    20190225 1.修改hosts文件,windows的hosts文件路径是  C:\ Windows \ System32 \ drivers \ etc \ hosts 0.0.0.0 acco ...

  4. React-state props与render()的关系

    state或者props发生改变,render()j就会执行一次. 父组件的render()被重新执行时,它的子组件的render()都会重新执行.

  5. Luogu P1494 [国家集训队]小Z的袜子

    比较简单的莫队题,主要是为了熟练板子. 先考虑固定区间时我们怎么计算,假设区间\([l,r]\)内颜色为\(i\)的袜子有\(cnt_i\)只,那么对于颜色\(i\)来说,凑齐一双的情况个数为: \( ...

  6. Flutter - 本地化语言

    Flutter有很多本地化的packages使用,我现在用的是 flutter_i18n 项目主页:https://pub.dartlang.org/packages/flutter_i18n 1.安 ...

  7. 磁盘挂载问题:Fdisk最大只能创建2T分区的盘,超过2T使用parted

    需求说明:云服务器上买了一块8T的磁盘,准备挂载到服务器上的/data目录下. ===================================parted命令说明=============== ...

  8. rrd文件及rrd文件与实际数据的对比研究。

    一,什么是rrd文件? 所 谓的“Round Robin” 其实是一种存储数据的方式,使用固定大小的空间来存储数据,并有一个指针指向最新的数据的位置.我们可以把用于存储数据的数据库的空间看成一个圆,上 ...

  9. python基础学习笔记(十三)

    re模块包含对 正则表达式.本章会对re模块主要特征和正则表达式进行介绍. 什么是正则表达式 正则表达式是可以匹配文本片段的模式.最简单的正则表达式就是普通字符串,可以匹配其自身.换包话说,正则表达式 ...

  10. 牛客第二场-J-farm-二维树状数组

    二维树状数组真的还挺神奇的,更新也很神奇,比如我要更新一个区域内的和,我们的更新操作是这样的 add(x1,y1,z); add(x2+1,y2+1,z); add(x1,y2+1,-z); add( ...