FarmCraft --(树形DP)
题目描述
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个妹子安装时间为
。
树上的每条边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
, separated by single spaces;
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[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 <cstring>
#include <iostream>
#include <algorithm>
#define M 500500
using namespace std;
struct abcd{
int to,next;
}table[M<<];
int head[M],tot;
int n;
int a[M],f[M],g[M];
//f表示遍历所需时间,g表示遍历并装机完毕所需时间
void Add(int x,int y)
{
table[++tot].to=y;
table[tot].next=head[x];
head[x]=tot;
}
bool Compare(int x,int y)
{
return g[x]-f[x] > g[y]-f[y];
}
void Tree_DP(int x,int from)
{
int i;
for(i=head[x];i;i=table[i].next)
if(table[i].to!=from)
Tree_DP(table[i].to,x);
static int stack[M];int top=;
for(i=head[x];i;i=table[i].next)
if(table[i].to!=from)
{
f[table[i].to]+=;
g[table[i].to]=max(g[table[i].to]+,f[table[i].to]);
stack[++top]=table[i].to;
}
sort(stack+,stack+top+,Compare);
g[x]=a[x];
for(i=;i<=top;i++)
{
g[x]=max(g[x],f[x]+g[stack[i]]);
f[x]+=f[stack[i]];
}
}
int main()
{
int i,x,y;
cin>>n;
for(i=;i<=n;i++)
scanf("%d",&a[i]);
for(i=;i<n;i++)
{
scanf("%d%d",&x,&y);
Add(x,y);Add(y,x);
}
Tree_DP(,);
cout<<max(g[],f[]+a[])<<endl;
return ;
}
FarmCraft --(树形DP)的更多相关文章
- bzoj 3829: [Poi2014]FarmCraft 树形dp+贪心
题意: $mhy$ 住在一棵有 $n$ 个点的树的 $1$ 号结点上,每个结点上都有一个妹子. $mhy$ 从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装 $zhx$ 牌杀毒 ...
- BZOJ3829[Poi2014]FarmCraft——树形DP+贪心
题目描述 In a village called Byteville, there are houses connected with N-1 roads. For each pair of ho ...
- 【BZOJ3829】[Poi2014]FarmCraft 树形DP(贪心)
[BZOJ3829][Poi2014]FarmCraft Description In a village called Byteville, there are houses connected ...
- poj3417 LCA + 树形dp
Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4478 Accepted: 1292 Descripti ...
- COGS 2532. [HZOI 2016]树之美 树形dp
可以发现这道题的数据范围有些奇怪,为毛n辣么大,而k只有10 我们从树形dp的角度来考虑这个问题. 如果我们设f[x][k]表示与x距离为k的点的数量,那么我们可以O(1)回答一个询问 可是这样的话d ...
- 【BZOJ-4726】Sabota? 树形DP
4726: [POI2017]Sabota? Time Limit: 20 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 128 Solved ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- 树形DP
切题ing!!!!! HDU 2196 Anniversary party 经典树形DP,以前写的太搓了,终于学会简单写法了.... #include <iostream> #inclu ...
- BZOJ 2286 消耗战 (虚树+树形DP)
给出一个n节点的无向树,每条边都有一个边权,给出m个询问,每个询问询问ki个点,问切掉一些边后使得这些顶点无法与顶点1连接.最少的边权和是多少.(n<=250000,sigma(ki)<= ...
- POJ2342 树形dp
原题:http://poj.org/problem?id=2342 树形dp入门题. 我们让dp[i][0]表示第i个人不去,dp[i][1]表示第i个人去 ,根据题意我们可以很容易的得到如下递推公式 ...
随机推荐
- .netCore下的jwt的梳理-->借鉴于“老张的哲学”
之前在公司的项目中有用到jwt进行token验证,但是公司里用的框架已经集成好了jwt,所以对jwt的的了解不够清晰,感觉还是隔着一层.在看了“老张的哲学”的jwt部分后对jwt的认识才更加深刻了一些 ...
- Feign 第一个Feign程序 一
Feign 开源地址:https://github.com/OpenFeign/feign 1.编写接口服务 (1)导入jar包 <parent> <groupId>org.s ...
- 如何优雅的把后台数据(通常是JSON)轻松渲染到html页面
如何优雅的把后台数据(通常是JSON)轻松渲染到html页面 在我们做前后端分离的时候,都有遇到过一些看起卡很简答,确无从下手的问题把.比方说后台给了前端一个list集合,集合里面有很多学生,我们现在 ...
- CMAKE交叉编译快速入门
cmake 工具 cmake 使用非常简单,最常用的用法是 cmake . 在当前目录执行cmake 官方帮助 -D <var>:<type>=<value> -D ...
- web架构之Nginx简介(1)
目录 1.Nginx概述 2.Nginx快速安装 2.1.源码方式安装Nginx 2.2.yum方式安装Nginx 3.Nginx配置文件 4.Nginx核心功能模块 5.Ningx目录介绍 1.Ng ...
- 一文深入了解史上最强的Java堆内缓存框架Caffeine
它提供了一个近乎最佳的命中率.从性能上秒杀其他一堆进程内缓存框架,Spring5更是为了它放弃了使用多年的GuavaCache 缓存,在我们的日常开发中用的非常多,是我们应对各种性能问题支持高并发的一 ...
- 【python系统学习10】布尔值
python的数据类型有好多个,前边写过字符串.整数和浮点数这三种. 本节来整理另一种简单数据类型--布尔值 布尔值(bool) 布尔值和其数据值 计算机可以用数据进行判断,若判断为真则执行特定条件中 ...
- 关于ATL生成COM注册失败解决方法
最近搞C++封装研究了下COM 做最后整理打包的时候发现各种问题引发的注册失败,so整理下备忘. 1.因引用其它动态连接库与你注册的dll不在同一目录下引起的异常.(解决方法将依赖dll放置与注册dl ...
- MySQL数据备份及还原(一)
关于删库跑路的事故现在已经屡见不鲜了,数据备份的必要性是企业数据管理极其重要的一项工作.关于数据备份.恢复也有很多场景及方法,本系列也会将主要的几种工具通过案例进行演示. 本系列将从逻辑备份及恢复开始 ...
- 3. webdriver的常用方法
WebDriver常用方法: clear(): 清除文本. send_keys (value): 模拟按键输入. click(): 单击元素. submit():用于提交表单 from seleniu ...