分析:一个贪心的想法是每次找到根的点权和最大的点进行操作,关键是怎么维护.每次找最大值,修改后会对这条链上每个点的子树上的点造成影响,可以用线段树来维护.找最大值就是区间求最大值嘛,对子树进行操作利用dfs序维护一下就好了.记录一下最大值的位置,每次从这个位置向上跳并对它的子树进行修改直到当前点的点权为0.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int maxn = ;
typedef long long ll; int n, k, a[maxn], fa[maxn],head[maxn], to[maxn], nextt[maxn],tot = , d[maxn], l[maxn], r[maxn], pos[maxn], dfs_clock;
int maxx[maxn << ], tag[maxn << ], p[maxn << ];
ll ans; void add(int x, int y)
{
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} void dfs(int u, int from, int dist)
{
d[u] = dist;
l[u] = ++dfs_clock;
pos[dfs_clock] = u;
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (v != from)
{
dfs(v, u, dist + a[v]);
fa[v] = u;
}
}
r[u] = dfs_clock;
} void pushup(int o)
{
if (maxx[o * ] > maxx[o * + ])
{
maxx[o] = maxx[o * ];
p[o] = p[o * ];
}
else
{
maxx[o] = maxx[o * + ];
p[o] = p[o * + ];
}
} void build(int o, int l, int r)
{
if (l == r)
{
maxx[o] = d[pos[l]];
p[o] = pos[l];
return;
}
int mid = (l + r) >> ;
build(o * , l, mid);
build(o * + , mid + , r);
pushup(o);
} void pushdown(int o)
{
if (tag[o] != )
{
tag[o * ] += tag[o];
tag[o * + ] += tag[o];
maxx[o * ] += tag[o];
maxx[o * + ] += tag[o];
tag[o] = ;
}
} void update(int o, int l, int r, int x, int y,int v)
{
if (x <= l && r <= y)
{
maxx[o] += v;
tag[o] += v;
return;
}
pushdown(o);
int mid = (l + r) >> ;
if (x <= mid)
update(o * , l, mid, x, y, v);
if (y > mid)
update(o * + , mid + , r, x, y, v);
pushup(o);
} void change(int x)
{
while (a[x])
{
update(, , n, l[x], r[x], -a[x]);
a[x] = ;
x = fa[x];
}
} int main()
{
scanf("%d%d", &n, &k);
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
for (int i = ; i < n; i++)
{
int u, v;
scanf("%d%d", &u, &v);
add(u, v);
add(v, u);
}
dfs(, , a[]);
build(, , n);
for (int i = ; i <= k; i++)
{
ans += maxx[];
change(p[]);
}
printf("%lld\n", ans); return ;
}

noip模拟赛 旅行的更多相关文章

  1. NOIP模拟赛20161022

    NOIP模拟赛2016-10-22 题目名 东风谷早苗 西行寺幽幽子 琪露诺 上白泽慧音 源文件 robot.cpp/c/pas spring.cpp/c/pas iceroad.cpp/c/pas ...

  2. contesthunter暑假NOIP模拟赛第一场题解

    contesthunter暑假NOIP模拟赛#1题解: 第一题:杯具大派送 水题.枚举A,B的公约数即可. #include <algorithm> #include <cmath& ...

  3. NOIP模拟赛 by hzwer

    2015年10月04日NOIP模拟赛 by hzwer    (这是小奇=> 小奇挖矿2(mining) [题目背景] 小奇飞船的钻头开启了无限耐久+精准采集模式!这次它要将原矿运到泛光之源的矿 ...

  4. 大家AK杯 灰天飞雁NOIP模拟赛题解/数据/标程

    数据 http://files.cnblogs.com/htfy/data.zip 简要题解 桌球碰撞 纯模拟,注意一开始就在袋口和v=0的情况.v和坐标可以是小数.为保险起见最好用extended/ ...

  5. 队爷的讲学计划 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的讲学计划 题解:刚开始理解题意理解了好半天,然后发 ...

  6. 队爷的Au Plan CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的Au%20Plan 题解:看了题之后觉得肯定是DP ...

  7. 队爷的新书 CH Round #59 - OrzCC杯NOIP模拟赛day1

    题目:http://ch.ezoj.tk/contest/CH%20Round%20%2359%20-%20OrzCC杯NOIP模拟赛day1/队爷的新书 题解:看到这题就想到了 poetize 的封 ...

  8. CH Round #58 - OrzCC杯noip模拟赛day2

    A:颜色问题 题目:http://ch.ezoj.tk/contest/CH%20Round%20%2358%20-%20OrzCC杯noip模拟赛day2/颜色问题 题解:算一下每个仆人到它的目的地 ...

  9. CH Round #52 - Thinking Bear #1 (NOIP模拟赛)

    A.拆地毯 题目:http://www.contesthunter.org/contest/CH%20Round%20%2352%20-%20Thinking%20Bear%20%231%20(NOI ...

随机推荐

  1. RHEL5.6环境下Oracle10g单主机安装步骤记录

    操作环境:RedHat Enterprise Linux 5.6 安装版本:Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64 ...

  2. MyEclipse去除不必要的validation

    MyEclipse在构建项目时去除不必要的Valication可以加快构建速度. 操作: Window->Perferences->MyEclipse->Validation 在Va ...

  3. 转-UIButton定义和设置圆角

    //login button // .h 中定义 UIButton *_loginBtn; @property (strong,nonatomic)UIButton *loginBtn; // .m ...

  4. VGG16 pre-trained model 实现 image classification

    站在巨人的肩膀上!使用VGG预先训练好的weight来,进行自己的分类. 下一阶段是在这上面进行自己的修改,完成自己想要的功能. Github源码 Github上有我全部的工程代码. 环境配置 Pyt ...

  5. 转】MongoDB主从复制实验 master/slave

    原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! Posted: May 31, 2013 Ta ...

  6. C#中的事件机制

    这几天把事件学了一下,总算明白了一些.不多说了,直接代码. class Program { static void Main(string[] args) { CatAndMouse h = new ...

  7. H.264学习笔记2——帧内预测

    帧内预测:根据经过反量化和反变换(没有进行去块效应)之后的同一条带内的块进行预测. A.4x4亮度块预测: 用到的像素和预测方向如图: a~f是4x4块中要预测的像素值,A~Q是临块中解码后的参考值. ...

  8. android手机web网站拨打电话几种方式

    1. <input name="phone" format="*m" value="13"/> <do type=&quo ...

  9. 数据层优化-jdbc连接池简述、druid简介

    终于回到既定轨道上了,这一篇讲讲数据库连接池的相关知识,线程池以后有机会再结合项目单独写篇文章(自己给自己挖坑,不知道什么时候能填上),从这一篇文章开始到本阶段结束的文章都会围绕数据库和dao层的优化 ...

  10. CSS3 四边形 凹角写法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...