题目描述

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.
 

样例输入

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

样例输出

  1. 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.
 
题解
      论我今天为什么打挂了几乎所有题:昨天接连被ryf、byb、zsq三个dalao膜,RP变为-0x7fffffff,我不炸谁炸啊……
       另外给出的那个参考翻译明显是有道自动翻译吧,能把No.1翻译成“不1”我也是服,找zzh翻也不至于翻得这么奇怪啊;题目里那个翻译倒是没有什么语法错误,但是总觉得怪怪的……一开始当网络流来做,想了半个小时怎么建图,最后感觉这就不是个网络流的题,想到树中路径是唯一的,放下网络流开始树归。其实也不能算是严格的树归,只不过是用子节点推父亲罢了。遍历顺序肯定是需要贪心的,但是拿节点权值贪心总觉得心里不太安稳,果然如此,但是水过了75分~
       遍历的时间是一定的,即边数*2。关键是在遍历结束之后,还有可能一些点没有完成安装。用p[i]表示在遍历完i点及其子树后,仍需要的安装时间,则最后的结果为max(c[1],p[1])+边数*2。1是特别的,要遍历完成后才开始安装;叶节点p值即为c值,由子节点递推父节点p值的公式为:p[father]=max(c[father]-f[father],p[son]-(f[father]-w)),其中f为遍历本树所需步数,w为遍历到这个儿子已走过的步数。当每个子节点的p值在递归后已经确定,按p递减的顺序决定遍历顺序来进一步确定父节点p值(刚开始的时候这里一直没想懂,总觉得自己要拿些根本不知道的东西来确定遍历顺序,其实可以在一次递归里解决)。w值只包括父节点与子节点之间已走过的边,因为子节点的子树已经在子节点的p值被除去过了。c[i]-f[i]可能为负,但这种情况直接当做0来处理。注意到这些细节,一次dfs就可以处理完成了。
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<algorithm>
  5. using namespace std;
  6. const int sj=;
  7. int n,c[sj],h[sj],e,a1,a2,f[sj],jg;
  8. int p[sj],fa[sj];
  9. struct B
  10. {
  11. int ne,v;
  12. }b[sj*];
  13. void add(int x,int y)
  14. {
  15. b[e].v=y;
  16. b[e].ne=h[x];
  17. h[x]=e++;
  18. }
  19. int bj(int x,int y)
  20. {
  21. return x>y?x:y;
  22. }
  23. struct D
  24. {
  25. int num,vl;
  26. }d[sj];
  27. int comp(const D&a,const D&b)
  28. {
  29. return a.vl>b.vl;
  30. }
  31. void dfs(int x,int ge)
  32. {
  33. int temp=ge,ww=;
  34. for(int i=h[x];i!=-;i=b[i].ne)
  35. if(b[i].v!=fa[x])
  36. {
  37. fa[b[i].v]=x;
  38. dfs(b[i].v,temp);
  39. f[x]+=f[b[i].v]+;
  40. d[temp].vl=p[b[i].v];
  41. d[temp].num=b[i].v;
  42. temp++;
  43. }
  44. p[x]=c[x]-f[x];
  45. if(p[x]<) p[x]=;
  46. sort(d+ge,d+temp,comp);
  47. for(int i=ge;i<temp;i++)
  48. {
  49. ww+=f[d[i].num];
  50. p[x]=bj(p[x],p[d[i].num]-(f[x]-ww));
  51. ww+=;
  52. }
  53. }
  54. void init()
  55. {
  56. scanf("%d",&n);
  57. memset(h,-,sizeof(h));
  58. for(int i=;i<=n;i++) scanf("%d",&c[i]);
  59. for(int i=;i<n;i++)
  60. {
  61. scanf("%d%d",&a1,&a2);
  62. add(a1,a2);
  63. add(a2,a1);
  64. }
  65. dfs(,);
  66. }
  67. int main()
  68. {
  69. init();
  70. jg=bj(c[],p[])+*n-;
  71. printf("%d",jg);
  72. return ;
  73. }

farmcraft

你应该超越自己,走得更远,登得更高,直至群星已在你脚下

FarmCraft[POI2014]的更多相关文章

  1. [补档][Poi2014]FarmCraft

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

  2. [BZOJ 3829][POI2014] FarmCraft

    先贴一波题面... 3829: [Poi2014]FarmCraft Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 421  Solved: 197[ ...

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

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

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

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

  5. 【bzoj3829】[Poi2014]FarmCraft 贪心

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html 题目描述 In a village called Byteville, there are   ...

  6. [POI2014][树形DP]FarmCraft

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

  7. BZOJ3829 : [Poi2014]FarmCraft

    d[x]表示走完x的子树并回到x所需的时间 f[x]表示从走到x开始计时,x子树中最晚的点安装完的最早时间 d[x]=sum(d[i]+2),i是x的孩子 f[x]的计算比较复杂: 考虑将x的各棵子树 ...

  8. [Poi2014]FarmCraft 树状dp

    对于每个点,处理出走完其子树所需要的时间和其子树完全下载完软件的时间 易证,对于每个点的所有子节点,一定优先选择差值大的来给后面的时间 树规+贪心. #include<cstdio> #i ...

  9. BZOJ3829 [Poi2014]FarmCraft 【树形dp】

    题目链接 BZOJ3829 题解 设\(f[i]\)为从\(i\)父亲进入\(i\)之前开始计时,\(i\)的子树中最晚装好的时间 同时记\(siz[i]\)为节点\(i\)子树大小的两倍,即为从父亲 ...

随机推荐

  1. RabbitMQ~开篇与环境部署

    想写这篇文章很久了,今天终于有时间总结一下,一个大型的系统里,消息中间件是必不可少的,它将并发环境处理的数据异步进行处理,有效的提高了系统的并发能力,有很多系统的瓶颈点都在于此,而消息中间件在这个时候 ...

  2. Haoop MapReduce 的Partition和reduce端的二次排序

    先贴一张原理图(摘自hadoop权威指南第三版) 实际中看了半天还是不太理解其中的Partition,和reduce端的二次排序,最终根据实验来结果来验证自己的理解 1eg 数据如下 20140101 ...

  3. Sqlserver2005 破解版下载地址

    Sqlserver2005 破解版下载地址:http://www.xiaidown.com/soft/from/1583.html

  4. SOD开源框架MSF(消息服务框架)介绍

    前言:之前想做消息的广播,拖着就忘记了,现在拿了医生的框架来学习,就按实现了之前想实现的功能. 传送门http://www.cnblogs.com/bluedoctor/,框架的获取,按传送门的链接就 ...

  5. 简单的视频采集demo

    打算做个简单的聊天软件,其中一个我没做过的,就是视频采集. 在网上查了许久资料,终于搞清楚了dshow采集视频的流程 参考资料如下: https://msdn.microsoft.com/en-us/ ...

  6. 【LeetCode】67. Add Binary

    题目: Given two binary strings, return their sum (also a binary string). For example,a = "11" ...

  7. tcp/ip通信传输流

    利用TCP/IP协议族进行网络通信时,会通过分层顺序与对方进行通信,发送端从应用层往下走,接收端则往应用层方向走. 我们用HTTP进行举例 客户端在应用层发出想要看到某个web页面的http请求.HT ...

  8. java基础系列--Exception异常处理

    原创作品,可以转载,但是请标注出处地址:http://www.cnblogs.com/V1haoge/p/7191280.html 1.概述 Java代码中的异常处理是非常重要的一环,从代码中可以看到 ...

  9. Java项目集成SAP BO

    SAP BO报表查看需要登录SAP BO系统,为了方便公司希望将BO报表集成到OA系统中,所以参考网上资料加上与SAP BO的顾问咨询整理出一套通过Java来集成SAP BO的功能. SAPBO中的报 ...

  10. HashTable的故事----Jdk源码解读

    HashTable的故事 很早之前,在讲HashMap的时候,我们就说过hash是散列,把...弄碎的意思.hashtable中的hash也是这个意思,而table呢,是指数据表格,也就是说hasht ...