P3574 FAR-FarmCraft 题解
题目
In a village called Byteville, there are \(n\) 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 \(n\).
The house no. 1 belongs to the village administrator Byteasar.
As part of enabling modern technologies for rural areas framework, nn 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.
在一个叫做比特村的小村庄中,有\(n-1\)条路连接着这个村庄中的全部nn个房子。
每两个房子之间都有一条唯一的通路。这些房子的编号为\(1\)至\(n\)。
1号房子属于村庄的管理员比特安萨尔。
为了提升村庄的科技使用水平,\(n\)台电脑被快递到了比特安萨尔的房子。每个房子都应该有一台电脑,且分发电脑的任务就落在了比特安萨尔的肩上。
比特村的居民一致同意去玩农场物语这个游戏的最新快照版,而且好消息是他们很快就要得到他们最新的高配置电脑了。
比特安萨尔将所有电脑都装在了他的卡车上,而且他准备好完成这个艰巨的任务了。
他的汽油恰好够走每条路两遍。
在每个房子边,比特安萨尔把电脑贴心的配送给居民,且立即前往下一个房子。(配送过程不花费任何时间)
只要每间房子的居民拿到了他们的新电脑,它们就会立即开始安装农场物语。安装农场物语所用的时间根据居民的科技素养而定。幸运的是,每间房子中居民的科技素养都是已知的。
在比特安萨尔配送完所有电脑后,他会回到他自己的1号房子去安装他自己的农场物语。
用卡车开过每条路的时间恰好是1分钟,而居民开电脑箱的时间可以忽略不计。(因为他们太想玩农场物语了)
请你帮助比特安萨尔算出从开始配送到所有居民都玩上了农场物语的最少时间。
输入格式
The first line of the standard input contains a single integer \(n(2\le n\le 500\ 000)\) that gives the number of houses in Byteville.
The second line contains \(n\) integers \(c_1,c_2,\cdots,c_n(1\le c_i\le 10^9)\),separated by single spaces; \(c_i\) 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 aa and \(b (1\le a<b\le n)\), separated by a single space.These indicate that there is a direct road between the houses no. aa and bb.
第一行包含一个整数\(n(2 \leq n \leq 500000)\),代表比特村中有多少房子。
第二行包含nn个整数\(c_1, c_2, ⋯, c_n(1 \leq c_i \leq 10^9)\),每个数都被单个空格隔开。\(c_i\)是第i号房间中居民安装农场物语所用的时间。
接下来的n-1n−1行代表了每一条路的两个顶点。两个顶点\(a\)和\(b\)满足\((1 \leq a < b \leq n)\),两个数之间有一个空格。
输出格式
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
样例解释

题解
前几天刚写过salesman,一看这题就觉得有点相似,主要是DFS的过程相似
对于一棵子树的根节点来说,已知遍历时间和装软件时间,我们该怎么分配遍历的顺序?
首先明确,要输出的答案是遍历所有子树且所有子树安装好软件的时间,那么,有没有可能比特安萨尔已经遍历完成,但是因为某些技术水平不高的居民而不得不继续等待耗费时间的情况?有可能,那么为了尽可能减少这个时间,我就先遍历那些技术水平不高的居民.
注意,如果安装时间最长,但需要遍历的时间也很长,不一定需要先遍历,因为遍历是一定需要遍历的,这些时间不能节省,遍历是比特安萨尔去遍历,一个人同时只能干一件事情,但安装是居民安装,可以同时多个居民同时安装.
如果本来可以同时安装但我让他尽可能多地同时安装,就会浪费时间.
所以我们应该先遍历的是总时间减遍历时间最大的,这些时间就是从DFS完成,回溯开始到软件安装完成的时间.这些时间可以用来遍历别的子树.
看一下这个图:

每一行代表一个居民的时间分配.
黄色是遍历时间,蓝色是安装时间.黄色首尾相接代表只能同时遍历一棵子树,蓝色可以重叠表示居民可以同时安装,
上面一块是没有优化的版本,可以看出来4号居民的安装时间大大拖慢了总体时间
下面是优化过的,可以看出来4号居民长长的安装时间是和遍历同时进行的,没有拖慢时间.
所以更新子树时,根据总时间减遍历时间从大到小排序即可.
代码
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 500001;
struct Edge {
int to, next;
} edges[maxn<<1];
int n, head[maxn], cnt, u, v, a[maxn], dp[maxn], dis[maxn], son[maxn];
bool cmp(int x, int y) { return dp[x] - dis[x] > dp[y] - dis[y]; }
void add(int u, int v) { edges[++cnt].to = v, edges[cnt].next = head[u], head[u] = cnt; }
void dfs(int root, int fa) {
if (root != 1) dp[root] = a[root];
for (int i = head[root]; i; i = edges[i].next)
if (edges[i].to != fa) dfs(edges[i].to, root);
int sonn = 0;
for (int x = head[root]; x; x = edges[x].next)
if (edges[x].to != fa) son[++sonn] = edges[x].to;
sort(son + 1, son + sonn + 1, cmp);
for (int i = 1; i <= sonn; ++i)
dp[root] = max(dp[root], dp[son[i]] + dis[root] + 1), dis[root] += dis[son[i]] + 2;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; ++i) scanf("%d", &a[i]);
for (int i = 1; i < n; ++i) {
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(1, 0);
printf("%d\n", max(dp[1], dis[1] + a[1]));
return 0;
}
P3574 FAR-FarmCraft 题解的更多相关文章
- 题解【洛谷P3574】[POI2014]FAR-FarmCraft
题面 简化版题意: 有一棵 \(n\) 个点的树,有边权. 你初始在 \(1\) 号节点,你需要走遍整棵树为 \(2 \sim n\) 号点的居民分发电脑,但你的汽油只够经过每条边恰好两次. 一个居民 ...
- FarmCraft[POI2014]
题目描述 In a village called Byteville, there are houses connected with N-1 roads. For each pair of ho ...
- POI2014题解
POI2014题解 [BZOJ3521][Poi2014]Salad Bar 把p当作\(1\),把j当作\(-1\),然后做一遍前缀和. 一个合法区间\([l,r]\)要满足条件就需要满足所有前缀和 ...
- 【BZOJ3829】[Poi2014]FarmCraft 树形DP(贪心)
[BZOJ3829][Poi2014]FarmCraft Description In a village called Byteville, there are houses connected ...
- 【bzoj3829】[Poi2014]FarmCraft 贪心
原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html 题目描述 In a village called Byteville, there are ...
- FarmCraft --(树形DP)
题目描述 In a village called Byteville, there are houses connected with N-1 roads. For each pair of hous ...
- 【树形dp】Farmcraft
题目 In a village called Byteville, there are houses connected with N-1 roads. For each pair of houses ...
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
随机推荐
- Jmeter之Json提取器详解(史上最全)
参考资料:https://www.bbsmax.com/A/D854lmBw5E/ Jsonpath在线测试:http://jsonpath.com/ 实际工作中用到的一些场景: 提取某个特定的值 提 ...
- 浅谈Python内置对象类型——数字篇(附py2和py3的区别之一)
Python是一门面向对象的编程设计语言,程序中每一样东西都可以视为一个对象.Python内置对象可以分为简单类型和容器类型,简单类型主要是数值型数据,而容器类型是可以包含其他对象类型的集体,如序列. ...
- 2 个步骤为 VSCode 配置工程头文件路径!
我用 VSCode 来 Coding,这个编辑器需要自己配置头文件路径,就是自动建立一个 c_cpp_properties.json 文件来管理头文件路径,然后需要用哪些库就手动加上即可,方法很简单, ...
- char、short、int、unigned int 之间的类型转换
标准数据类型之间会进行 隐式类型的安全转换 转换规则如下: char→int→unsigned int →long→unsigned long→float→double ↓ sho ...
- 【代理】内网穿透工具 frp&frps
frp 是一个高性能的反向代理应用,可以帮助您轻松地进行内网穿透,对外网提供服务,支持 tcp, http, https 等协议类型,并且 web 服务支持根据域名进行路由转发. ### frp 的作 ...
- 启动fiddler情况下,网络连接错误[Fiddler] The connection to ** failed.解决办法
这种错误是由于浏览器设置了代理,而代理服务器配置不正确导致 解决办法 1.关闭浏览器服务器代理,设置-高级-网络代理 2.检查网络代理设置是否正确,Fiddler中配置的端口号需要跟浏览器中配置的端口 ...
- OpenSSH详解
OpenSSH详解(思维导图) 1. SSH概述 SSH 软件架构 认证方式 2. OpenSSH 2.1 客户端程序ssh 配置文件 ssh命令 客户端免密登录 scp sftp 2.2 服务端程序 ...
- 循序渐进VUE+Element 前端应用开发(10)--- 基于vue-echarts处理各种图表展示
在我们做应用系统的时候,往往都会涉及图表的展示,综合的图表展示能够给客户带来视觉的享受和数据直观体验,同时也是增强客户认同感的举措之一.基于图表的处理,我们一般往往都是利用对应第三方的图表组件,然后在 ...
- CentOS7.5搭建Hive2.3.3
一 Hive的下载 软件下载地址:https://mirrors.tuna.tsinghua.edu.cn/apache/hive/ 这里下载的版本是:apache-hive-2.3.3-bin.t ...
- 【JMeter_05】创建第一个简单的接口脚本
聚合数据:提供了很多开放的API,可以去练习使用https://www.juhe.cn/ 如果有小伙伴对HTTP协议不是很了解,可以看下这里 http://home.ustc.edu.cn/~xie1 ...