[POI2014]FarmCraft
题目大意:
一个$n(n\le5\times10^5)$个点的树,每个点有一个权值$c_i$,从$1$出发进行欧拉遍历,每个单位时间移动一条边,记每个点$i$被访问到的时间是$t_i$,问最后$\max\{t_i+c_i\}$的最小值(点$1$算作最后访问)。
思路:
$f[i]$表示欧拉遍历以$i$为根的子树的时间,$g[i]$表示对于以点$i$为根的子树中,对于每个点$j$,$\max\{t_j+c_j\}-t_i$的最小值。
转移时考虑贪心,对于子结点$j$,$g[j]-f[j]$的值可以在遍历别的子树的过程中抵消掉,因此可以将子结点关于$g[j]-f[j]$降序排序并进行转移。最后答案即为$\max(g[1],f[1]+c_1)$。
#include<cstdio>
#include<cctype>
#include<vector>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'';
while(isdigit(ch=getchar())) x=(((x<<)+x)<<)+(ch^'');
return x;
}
const int N=;
int c[N],f[N],g[N];
std::vector<int> e[N];
inline void add_edge(const int &u,const int &v) {
e[u].push_back(v);
e[v].push_back(u);
}
inline bool cmp(const int &a,const int &b) {
return g[a]-f[a]>g[b]-f[b];
}
void dfs(const int &x,const int &par) {
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
dfs(y,x);
g[y]=std::max(g[y]+,f[y]+=);
}
std::sort(e[x].begin(),e[x].end(),cmp);
g[x]=c[x];
for(unsigned i=;i<e[x].size();i++) {
const int &y=e[x][i];
if(y==par) continue;
g[x]=std::max(g[x],f[x]+g[y]);
f[x]+=f[y];
}
}
int main() {
const int n=getint();
for(register int i=;i<=n;i++) c[i]=getint();
for(register int i=;i<n;i++) {
add_edge(getint(),getint());
}
dfs(,);
printf("%d\n",std::max(g[],f[]+c[]));
return ;
}
[POI2014]FarmCraft的更多相关文章
- [补档][Poi2014]FarmCraft
[Poi2014]FarmCraft 题目 mhy住在一棵有n个点的树的1号结点上,每个结点上都有一个妹子. mhy从自己家出发,去给每一个妹子都送一台电脑,每个妹子拿到电脑后就会开始安装zhx牌杀毒 ...
- [BZOJ 3829][POI2014] FarmCraft
先贴一波题面... 3829: [Poi2014]FarmCraft Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 421 Solved: 197[ ...
- 【BZOJ3829】[Poi2014]FarmCraft 树形DP(贪心)
[BZOJ3829][Poi2014]FarmCraft Description In a village called Byteville, there are houses connected ...
- 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 贪心
原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html 题目描述 In a village called Byteville, there are ...
- BZOJ3829 : [Poi2014]FarmCraft
d[x]表示走完x的子树并回到x所需的时间 f[x]表示从走到x开始计时,x子树中最晚的点安装完的最早时间 d[x]=sum(d[i]+2),i是x的孩子 f[x]的计算比较复杂: 考虑将x的各棵子树 ...
- [Poi2014]FarmCraft 树状dp
对于每个点,处理出走完其子树所需要的时间和其子树完全下载完软件的时间 易证,对于每个点的所有子节点,一定优先选择差值大的来给后面的时间 树规+贪心. #include<cstdio> #i ...
- BZOJ3829 [Poi2014]FarmCraft 【树形dp】
题目链接 BZOJ3829 题解 设\(f[i]\)为从\(i\)父亲进入\(i\)之前开始计时,\(i\)的子树中最晚装好的时间 同时记\(siz[i]\)为节点\(i\)子树大小的两倍,即为从父亲 ...
- [BZOJ3829][Poi2014]FarmCraft 贪心
这个题应该是很容易想到贪心的,只要可是怎么贪才是科学的呢?我们分析一下题干,对于每个边只能一进一出因此,对于树上的一棵子树,我们只要一进子树就必须遍历完,因此我们只能进行一遍 dfs() 然后我们发现 ...
随机推荐
- CentOS vim中backspace不能用,出现^?的解决方法
查看在VI配置器下面使用backspace删除时提示输出那个字符例如:^H.^?.^a等字符如果输出的是:^? 字符 则使用以下命令:[oracle@junyii~]$ stty erase ^? 再 ...
- 孤荷凌寒自学python第三天 初识序列
孤荷凌寒自学python第三天 初识序列 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) Python的序列非常让我着迷,之前学习的其它编程语言中没有非常特别关注过序列这种类型的对象,而pyt ...
- coreos ipa image Injection of public key
查看readme To embed the oem/ directory into a CoreOS pxe image: Note: In order to have the ability t ...
- codeforces Registration system
Registration system A new e-mail service "Berlandesk" is going to be opened in Berland in ...
- 2 25urllib.py
""" urllib.request.urlopen(url,data,timeout) """ # from urllib.request ...
- web自动化测试:watir+minitest(四)
脚本连跑: rake是ruby中的一个构建工具,和make很像.允许用ruby来写rakefile. 我们使用rake以任务的方式来运行我们的脚本集. 新建Rakefile文件,写入如下内容: req ...
- SqlServer中截取小数位数
方法一:convert(float,字段名) as 别名 select convert(float,round(10.123232,2)) 结果:10.12 select convert(float, ...
- iOS大神班笔记02-模仿苹果创建单例
首先我们得要知道苹果是如何实现单例的:1.不能外界调用alloc,一调用就崩掉,其实就是抛异常(类内部第一次调用alloc就不崩溃,其他都崩溃). 2.提供一个方法给外界获取单例. 3.内部创建一次 ...
- golang effective 翻译
参考 Effective Go 官方文档 其他参考译文 https://studygolang.com/articles/3228 http://docscn.studygolang.com/doc/ ...
- Educational Codeforces Round 57 (Rated for Div. 2) ABCDEF题解
题目总链接:https://codeforces.com/contest/1096 A. Find Divisible 题意: 给出l,r,在[l,r]里面找两个数x,y,使得y%x==0,保证有解. ...