先贴一波题面...

3829: [Poi2014]FarmCraft

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 421  Solved: 197
[Submit][Status][Discuss]

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,

手动忽略奇葩翻译

首先从数据范围看这肯定是一道\(O(n)\)左右复杂度可以解决的好题qwq这时可以选择考虑贪心策略. 对于贪心我们可以设置一个权重$k_i$, 代表遍历一遍以结点 \(i\) 为根的子树以后所有子节点完成软件安装还所需的额外时间. 不难发现我们先遍历需要额外时间最多的子树就可以获得最优解. 因为先开始遍历之后安装所需的额外时间可以在遍历其他子树时抵消一部分, 而遍历整个树所需的时间是一定的, 这样的策略可以尽可能多地在固定需要花费的时间里同时消耗这部分额外时间.

然后我们注意到其实根节点的 $k_1$ 与 $c_1$ 二者的较大值加上欧拉遍历用时就是答案. 所以我们考虑如何计算 $k_i$ .

首先我们要 $DFS$ 一遍求对以 \(i\) 为根的欧拉遍历所需时间 \(t_i\) , (其实等价于子树大小的二倍2333) 然后再 $DFS$ 一遍并在回溯过程中计算贪心顺序与 $k_i$ . 由于我们递归处理并且是在回溯过程中计算, 所以在开始计算 $k_i$ 时 $i$ 结点的子树的 $k$ 值一定都已经求出. 然后根据 $k$ 值降序排序 $i$ 的子节点. 然后建立一个临时变量 $tmp$ 储存后续遍历该节点其他子树所需要的时间, 因为后续遍历的过程中所消耗的时间可以抵消一部分前面遍历的子树的 $k$ 值对答案的贡献. 初始化临时变量为 $i$ 的欧拉遍历耗时, 每次遍历到一个子树后将临时变量减去这个子树的欧拉遍历耗时. 然后最终的公式如下:

\[k_i=max\{k_j - tmp-1 , (i,j) \in E\}\]

$tmp$ 和 $k$ 的意义如上文所述.

最后还需要注意的一点是 $k_i$ 可能在后续计算答案抵消的时候会变成负数, 而它对的贡献不可能是负的, 所以最后要判断一下, 如果 $k_i$ 为负则修改为0.

参考代码如下:

GitHub

 #include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm> const int MAXV=; std::vector<int> E[MAXV]; int v;
int c[MAXV];
int t[MAXV];
int k[MAXV]; void Initialize();
void DFS(int,int);
void Insert(int,int);
bool cmp(const int&,const int&); int main(){
Initialize();
DFS(,);
printf("%d\n",std::max(k[],c[])+*v-);
return ;
} void DFS(int root,int prt){
for(std::vector<int>::iterator i=E[root].begin();i!=E[root].end();i++){
if(*i==prt)
continue;
t[root]++;
DFS(*i,root);
t[root]++;
t[root]+=t[*i];
}
if(root!=)
k[root]=c[root]-t[root];
std::sort(E[root].begin(),E[root].end(),cmp);
int tmp=t[root];
for(std::vector<int>::iterator i=E[root].begin();i!=E[root].end();i++){
if(*i==prt)
continue;
tmp-=+t[*i];
k[root]=std::max(k[root],k[*i]-tmp-);
}
k[root]=std::max(k[root],);
} void Initialize(){
int a,b;
scanf("%d",&v);
for(int i=;i<=v;i++){
scanf("%d",c+i);
}
for(int i=;i<v;i++){
scanf("%d%d",&a,&b);
Insert(a,b);
Insert(b,a);
}
} inline bool cmp(const int& a,const int& b){
return k[a]>k[b];
} void Insert(int from,int to){
E[from].push_back(to);
}

Backup

补图qwq

[BZOJ 3829][POI2014] FarmCraft的更多相关文章

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

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

  2. [补档][Poi2014]FarmCraft

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

  3. 【刷题】BZOJ 4543 [POI2014]Hotel加强版

    Description 同OJ3522 数据范围:n<=100000 Solution dp的设计见[刷题]BZOJ 3522 [Poi2014]Hotel 然后发现dp的第二维与深度有关,于是 ...

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

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

  5. 【树形DP】BZOJ 3829 Farmcraft

    题目内容 mhy住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子i. mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒软件,第i个妹子安装时间为Ci. ...

  6. 主席树||可持久化线段树||BZOJ 3524: [Poi2014]Couriers||BZOJ 2223: [Coci 2009]PATULJCI||Luogu P3567 [POI2014]KUR-Couriers

    题目:[POI2014]KUR-Couriers 题解: 要求出现次数大于(R-L+1)/2的数,这样的数最多只有一个.我们对序列做主席树,每个节点记录出现的次数和(sum).(这里忽略版本差值问题) ...

  7. BZOJ 3524: [Poi2014]Couriers [主席树]

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1892  Solved: 683[Submit][St ...

  8. BZOJ 3524: [Poi2014]Couriers

    3524: [Poi2014]Couriers Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1905  Solved: 691[Submit][St ...

  9. Bzoj 3831 [Poi2014]Little Bird

    3831: [Poi2014]Little Bird Time Limit: 20 Sec Memory Limit: 128 MB Submit: 310 Solved: 186 [Submit][ ...

随机推荐

  1. Spring Boot + Spring Cloud 构建微服务系统(七):API服务网关(Zuul)

    技术背景 前面我们通过Ribbon或Feign实现了微服务之间的调用和负载均衡,那我们的各种微服务又要如何提供给外部应用调用呢. 当然,因为是REST API接口,外部客户端直接调用各个微服务是没有问 ...

  2. vue.js的项目实战

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由蔡述雄发表于云+社区专栏 需求背景 组件库是做UI和前端日常需求中经常用到的,把一个按钮,导航,列表之类的元素封装起来,方便日常使用, ...

  3. Java设计模式学习记录-桥接模式

    前言 这次介绍结构型设计模式中的第二种模式,桥接模式. 使用桥接模式的目的就是为了解耦,松散的耦合更利于扩展,但是会增加相应的代码量和设计难度. 桥接模式 桥接模式是为了将抽象化与实现化解耦,让二者可 ...

  4. Struts2之ValueStack、ActionContext

    今天在看Action获取Resquest.Response时,发现了一个词:值栈.于是今天一天都在看,了解了值栈不仅能知道Action怎么获取request.response等这些,还会了解OGNL语 ...

  5. [PHP] 算法-合并两个有序链表为一个有序链表的PHP实现

    合并两个有序的链表为一个有序的链表: 类似归并排序中合并两个数组的部分 1.遍历链表1和链表2,比较链表1和2中的元素大小 2.如果链表1结点大于链表2的结点,该结点放入第三方链表 3.链表1往下走一 ...

  6. ServiceFramework作为Java Web框架都有哪些不错的设计

    前言 最近需要开发一个纯API的项目,mlsql-cluster,从无到有,到最后完整的proxy功能开发完毕,只花了四个小时不到,自己不尽小感叹了一把 ServiceFramework的高效. 关于 ...

  7. Matlab .asv是什么文件

    有时在存放m文件的文件夹中会出现*.asv asv 就是auto save的意思,*.asv文件的内容和相应的*.m文件内容一样,用记事本和matlab都能打开它.它可以作为*.m文件的"备 ...

  8. js 中导出excel 较长数字串会变成科学计数法

    在做项目中,碰到如题的问题.比如要将居民的信息导出到excel中,居民的身份证号码因为长度过长(大于10位),excel会自动的将过长的数字串转换成 科学计数法.现在网上找到解决方案之一: (在数字串 ...

  9. JS校验身份证号的合法性

    前端表单中有身份证号的校验,下边是用JS来校验身份证号的合法性. 中国居民身份证号码编码规则 第一.二位表示省(自治区.直辖市.特别行政区). 第三.四位表示市(地级市.自治州.盟及国家直辖市所属市辖 ...

  10. PHP与.Net的区别(一)接口

    一.关于接口成员 PHP的接口成员只能包括两种: 1.函数签名 2.常量 .Net的接口成员只能包括三种: 1.函数签名 2.属性(注意:是属性,不是字段) 3.事件 4.索引器(也叫有参属性)