这个题应该是很容易想到贪心的,只要可是怎么贪才是科学的呢?
我们分析一下题干,对于每个边只能一进一出因此,对于树上的一棵子树,我们只要一进子树就必须遍历完,
因此我们只能进行一遍 dfs() 然后我们发现 dfs() 一遍的时间是一定的,
然后见每个妹子的时间就在这个时间轴上,
分析完了,我们说一下要贪什么。
我们先定义一个概念rest[]就是遍历完这个节点的子树以后我们还要为这个节点所费的时间
One_Stage :除了1节点,之外每个妹子一见面就杀du
Two_Stage :我们发现最后的答案是 Max(rest[1],c[1])+2*n-2
Three_Stage :然后我们考虑怎么找rest[1],
我们发现每个节点的最优rest[],是(子节点的rest[],减去其在这个子树里又经过的时间),再和(他的c[]减去遍历他的时间)取Max
这样我们线性一波就结局了

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define MAXN 500005
using namespace std;
inline int read()
{
register int sum=;
register char ch=getchar();
while(ch<''||ch>'')ch=getchar();
while(ch>=''&&ch<='')
{
sum=(sum<<)+(sum<<)+ch-'';
ch=getchar();
}
return sum;
}
inline int Max(int x,int y)
{
return x>y?x:y;
}
int a[MAXN],m[MAXN],n,t[MAXN];
vector<int> Via[MAXN];
void Dfs_T(int x,int fa)
{
register int len=Via[x].size();
for(register int i=;i<len;++i)
if(Via[x][i]!=fa)
{
Dfs_T(Via[x][i],x);
t[x]+=+t[Via[x][i]];
}
}
inline void Init()
{
n=read();
for(register int i=;i<=n;i++)a[i]=read();
for(register int i=,x,y;i<n;i++)x=read(),y=read(),Via[x].push_back(y),Via[y].push_back(x);
Dfs_T(,);
}
int rest[MAXN];
int comp(const int x,const int y)
{
return rest[x]>rest[y];
}
inline void Dfs_A(int x,int fa)
{
register int len=Via[x].size();
for(register int i=;i<len;++i)
if(Via[x][i]!=fa)Dfs_A(Via[x][i],x);
sort(Via[x].begin(),Via[x].end(),comp);
if(x!=)rest[x]=a[x]-t[x];
register int now=t[x];
for(register int i=;i<len;i++)
if(Via[x][i]!=fa)
{
now-=+t[Via[x][i]];
rest[x]=Max(rest[x],rest[Via[x][i]]-now-);
}
if(rest[x]<)rest[x]=;
}
inline void Work()
{
Dfs_A(,);
register int ans=Max(rest[],a[])+t[];
printf("%d",ans);
}
int main()
{
Init();
Work();
return ;
}

[BZOJ3829][Poi2014]FarmCraft 贪心的更多相关文章

  1. 【bzoj3829】[Poi2014]FarmCraft 贪心

    原文地址:http://www.cnblogs.com/GXZlegend/p/6826667.html 题目描述 In a village called Byteville, there are   ...

  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. Mysql错误积累001-load data导入文件数据出现1290错误

    错误出现情景 在cmd中使用mysql命令,学生信息表添加数据.使用load data方式简单批量导入数据. 准备好文本数据: xueshengxinxi.txt 文件  数据之间以tab键进行分割 ...

  2. django中的ContentType使用

    使用背景 最近设计表的时候遇到一个问题,有两门课程  一门专业课,一门学位课,我们按照时间长度来进行售卖,比如专业课一个月19元,两个月35元,三个月50元. 可以这么做但是领导不让我这么设计.... ...

  3. Leecode刷题之旅-C语言/python-58.最后一个单词的长度

    /* * @lc app=leetcode.cn id=58 lang=c * * [58] 最后一个单词的长度 * * https://leetcode-cn.com/problems/length ...

  4. ubuntu64位运行32位程序

    sudo dpkg --add-architecture i386 sudo apt install libc6:i386 转:https://blog.csdn.net/zoomdy/article ...

  5. C++ 求阶乘

    #include<iostream> using namespace std; ; //输入阶乘数 int main() { long long factorial[size]; fact ...

  6. linux io 学习笔记(03)---共享内存,信号灯,消息队列

    system V IPC 1)消息队列 2)共享内存 3)信号灯(信号量集) 1.消息队列. ipcs -q 查看系统中使用消息队列的情况 ipcrm -q +msqid 删除消息队列 消息队列工作原 ...

  7. WPF中使用定时器的注意事项

    原文:WPF中使用定时器的注意事项 注意事项 要使用System.Windows.Threading.DispatcherTimer,而不能使用System.Timers.Timer. 原因是WPF是 ...

  8. 洛谷P2307 迷宫

    怎么又是一道叫迷宫的题呀QWQ 题目链接 这道题主要是对并查集的考察,需要注意的坑点在于有可能存在的不止一个联通块. 我们只需要对输入的两个数据进行判断,如果在一个集合中证明有多条路则输出0,如果不在 ...

  9. 环境变量 - Maven

    Linux 1. 备份并编辑配置文件 # cp /etc/profile /etc/profile.bak # vi /etc/profile 2. 设置Maven环境变量 export MAVEN_ ...

  10. centos linux 因别名问题引起的麻烦及解决技巧

    老男孩儿-19期 L005-13节中分享.自己整理后发到自己微博中留档. 原文:http://oldboy.blog.51cto.com/2561410/699046 实例:老男孩linux实战培训第 ...