原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html


题目描述

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


题解

贪心

设f[i]表示子树i全部安装完成所需的最小总时间。

那么对于一个某结点x,f[x]一定大于等于c[x]。

若其为非叶子结点,考虑其子树a和b。

若先安装a再安装b,则a消耗的时间为f[a]+1,b消耗的时间为2*si[a]+f[b]+1

若先安装b再安装a,则a消耗的时间为2*si[b]+f[a]+1,b消耗的时间为f[b]+1

若先安装a合适,则必有2*si[a]+f[b]+1>2*si[b]+f[a]+1,即f[a]-2*si[a]<f[b]-2*si[b]

于是可以将x的所有子树按照f-2si从小到大排序,依次统计答案。

由于题目描述必须先完成2~n再完成1,所以应先将1的时间看作0,再分开计算。

#include <cstdio>
#include <algorithm>
#define N 500010
using namespace std;
struct data
{
int f , si;
}k[N] , a[N];
int head[N] , to[N << 1] , next[N << 1] , cnt , c[N];
bool cmp(data a , data b)
{
return a.f - 2 * a.si > b.f - 2 * b.si;
}
void add(int x , int y)
{
to[++cnt] = y , next[cnt] = head[x] , head[x] = cnt;
}
void dfs(int x , int fa)
{
int i , tot = 0 , now = 1;
k[x].f = c[x] , k[x].si = 1;
for(i = head[x] ; i ; i = next[i]) if(to[i] != fa) dfs(to[i] , x) , k[x].si += k[to[i]].si;
for(i = head[x] ; i ; i = next[i]) if(to[i] != fa) a[++tot] = k[to[i]];
sort(a + 1 , a + tot + 1 , cmp);
for(i = 1 ; i <= tot ; i ++ ) k[x].f = max(k[x].f , a[i].f + now) , now += 2 * a[i].si;
}
int main()
{
int n , i , x , y , t;
scanf("%d" , &n);
for(i = 1 ; i <= n ; i ++ ) scanf("%d" , &c[i]);
t = c[1] , c[1] = 0;
for(i = 1 ; i < n ; i ++ ) scanf("%d%d" , &x , &y) , add(x , y) , add(y , x);
dfs(1 , 0);
printf("%d\n" , max(k[1].f , t + 2 * (k[1].si - 1)));
return 0;
}

【bzoj3829】[Poi2014]FarmCraft 贪心的更多相关文章

  1. [BZOJ3829][Poi2014]FarmCraft 贪心

    这个题应该是很容易想到贪心的,只要可是怎么贪才是科学的呢?我们分析一下题干,对于每个边只能一进一出因此,对于树上的一棵子树,我们只要一进子树就必须遍历完,因此我们只能进行一遍 dfs() 然后我们发现 ...

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

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

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

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

  4. BZOJ3829 : [Poi2014]FarmCraft

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

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

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

  6. [补档][Poi2014]FarmCraft

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

  7. [BZOJ 3829][POI2014] FarmCraft

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

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

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

  9. [Poi2014]FarmCraft 树状dp

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

随机推荐

  1. Exception occurred during processing request: The given object has a null identifier: com.zsn.crm.Model.SaleVisit; nested exception is org.hibernate.TransientObjectException: The given object has a nu

    edit.jsp页面没有加入隐藏字段 id ,导致模型驱动封装时缺少id ,,调用update更新数据库时出错!

  2. datatable行内内容太长,有时不自动换行解决方法

    加一个css属性即可 style = "word-wrap:break-word;" js代码: "render": function (data, type, ...

  3. datatable常用设置

    bSort: false, // 是否排序功能 bFilter: false, // 过滤功能 bPaginate: true, // 翻页功能 bInfo: true, // 页脚信息 bProce ...

  4. rsync+lsyncd实现实时同步

    1.接收端安装rsync,修改/etc/rsyncd.conf配置文件,然后启动服务. uid = rootgid = rootuse chroot = nomax connection = 4sec ...

  5. Windows下安装Python数据库模块--MySQLdb

    ## 1.下载MySQLdb [去官网](http://pypi.python.org/pypi/MySQL-python/) 下载对应的编译好的版本(现在官网最新版本为1.2.5): MySQL-p ...

  6. PHP CI框架学习

    CI框架的URL辅助函数使用 URL 辅助函数文件包含一些在处理 URL 中很有用的函数 加载辅助函数 在使用CI框架的使用经常碰到跳转和路径方面的问题,site_url()和base_url()很容 ...

  7. PHP生成特定长度的纯字母字符串

    PHP中,md5().uniqid()函数可以返回32位和13位不重复的字符串,但是这些字符串都可能包含有数字.如果需要纯字母的字符串,而且长度不定,比如8位,那么直接用这两个函数无法达到效果. 这时 ...

  8. 012---Django的用户认证组件

    知识预览 用户认证 回到顶部 用户认证 auth模块 ? 1 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的 ...

  9. spark stream简介

    1.复杂的迭代计算 假如我们计算的需要100步的计算,但是当我执行到第99步的时候,突然数据消失, 根据血统,从头进行恢复,代价很高 sc.setCheckpointDir("共享存储文件系 ...

  10. salt 通信及其安全

    salt 通信及其安全 模型架构 server-agent通信模型: server就是salt master; agent就是salt-minion salt也可以作为一个单点服务器管理工具使用,或者 ...