原文地址: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. redis 过期回调通知

    redis 过期回调通知 背景 在使用redis的过程中,有时我们会遇到这种情景,当key过期的时候,我们需要去重新做一些操作,比如重新生成value等.之前,一直用的是添加一个celery定时任务, ...

  2. BZOJ1965: [Ahoi2005]SHUFFLE 洗牌(exgcd 找规律)

    Time Limit: 3 Sec  Memory Limit: 64 MBSubmit: 989  Solved: 660[Submit][Status][Discuss] Description ...

  3. python基础数据类型之列表,元组操作

    一.列表的索引和切片1.列表的索引列表和字符串一样样拥有索引 lst = ["a","b","c"] print(lst[0]) # 获取第 ...

  4. vim 个性化设置和操作

    一.vim 设置 1. 设置行号显示 1) 临时显示 命令行模式 :set nu 2) 永久显示 # vim ~/.vimrc 插入一行代码: set number 若没有该文件,在用户主目录 (/h ...

  5. Dapper and Repository Pattern in MVC

    大家好,首先原谅我标题是英文的,因为我想不出好的中文标题. 这里我个人写了一个Dapper.net 的Repository模式的底层基础框架. 涉及内容: Dapper.net结合Repository ...

  6. PHP审计(一)

    一.php中常见的危险函数和审计要点 危险函数(功能过于强大)    参数是否外部可控,有没有正确的过滤. PHP获取外界传入参数是通过下面几个全局函数的形式,所以审计参数传入经常要和下面几个变量打交 ...

  7. 怎样通过互联网ssh访问家里电脑

    需求:用可以上网的公司windows电脑连接家里的manjaro linux电脑.. 环境情况:公司电脑为内网,通过登录出口服务器连接互联网.家里的电脑也为内网,通过连接无线路由器连接外网.路由器有公 ...

  8. 准备篇(二)C语言

    因为C语言部分打算单独维护,所以 目录: 1. C语言基础篇(零)gcc编译和预处理 2. C语言基础篇(一)关键字 3. C语言基础篇(二)运算符 4. C语言指针篇(一)指针与指针变量 5. C语 ...

  9. SGU 169 numbers 数学

    169.Numbers Let us call P(n) - the product of all digits of number n (in decimal notation). For exam ...

  10. Error: Cannot find module 'core-js/fn/array/values' at Function.Module._resolveFilename (module

    E:\codeBase\top605\rescue-master\server\node_modules\_log4js@1.1.1@log4js\lib\log4js.js:321 throw ne ...