bzoj 3302&2447&2103 树的双中心 树形DP
题目:

题解:
bzoj 3302 == 2447 == 2103 三倍经验
首先我们考虑枚举两个中心的位置,然后统计答案.
我们发现,一定有一部分点离第一个中心更近,另一部分点离第二个中心更近
如果将两部分点分别染成两种颜色,容易发现一定有且只有一条边两端的颜色不相同
所以我们考虑枚举这条边,然后将整个树分成两个部分,然后分别求出分开的两颗树的中心,然后把两部分的代价求和来更新答案.
容易发现这样是\(n^2\)的
然后我们回头看题目,发现有奇怪的条件:深度 <= 100
这启发了我们从深度的角度去考虑.
我们考虑枚举这条边的过程,发现其实我们根本不用枚举所有的边
我们考虑在每一个深度上只枚举一条边
换句话说:我们要选择一条从根开始的链,枚举这条链上的每一条边
我们可以从根考虑来选择每一步走哪一棵子树,从而挑选出一条从根开始的链.
这是我们发现:每一次都走子树权值和最大的哪一棵子树,一定是最优决策.
所以我们可以把复杂度降到\(O(nh)\)
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
typedef long long ll;
const ll maxn = 50005;
const ll inf = 1LL<<60;
struct Edge{
ll v,next;
}G[maxn<<1];
ll n, head[maxn], cnt=1,fa[maxn], dep[maxn], mx[maxn], cmx[maxn];
ll sum[maxn],val[maxn],ans=inf,cut;
inline void add(ll u, ll v) {
G[++cnt] = (Edge){v, head[u]};
head[u] = cnt;
}
inline void dfs(ll x) {
for(ll i = head[x];i; i=G[i].next){
if(G[i].v == fa[x]) continue;
dep[G[i].v] = dep[x] + 1;
fa[G[i].v] = x;
dfs(G[i].v);
sum[x] += sum[G[i].v];
val[x] += val[G[i].v] + sum[G[i].v];
if(mx[x] == 0 || sum[G[i].v] > sum[mx[x]]){
cmx[x] = mx[x];
mx[x] = G[i].v;
}else if(cmx[x] == 0 || sum[G[i].v] > sum[cmx[x]]){
cmx[x] = G[i].v;
}
}
}
inline void find(ll &ret,ll root,ll x,ll k){
ret = min(ret,k);
ll v = mx[x];
if(v == cut || sum[cmx[x]] > sum[mx[x]]) v = cmx[x];
if(v == 0) return;
find(ret,root,v, k + sum[root] - 2*sum[v]);
}
inline void dfss(ll x){
for(ll i = head[x];i;i=G[i].next){
if(G[i].v == fa[x]) continue;
cut = G[i].v;
ll gx = inf,gy = inf;
for(ll j = x; j; j = fa[j]) sum[j] -= sum[cut];
find(gx, 1, 1, val[1] - val[cut] - dep[cut] * sum[cut]);
find(gy, cut, cut, val[cut]);
ans = min(ans, gx + gy);
for(ll j = x; j; j=fa[j]) sum[j] += sum[cut];
dfss(G[i].v);
}
}
int main() {
read(n);
for(ll i=1,u,v;i<n;++i){
read(u);read(v);
add(u, v); add(v, u);
}
for(ll i=1;i<=n;++i) read(sum[i]);
dfs(1);dfss(1);
printf("%lld\n",ans);
getchar();getchar();
return 0;
}
bzoj 3302&2447&2103 树的双中心 树形DP的更多相关文章
- 51nod"省选"模测 A 树的双直径(树形dp)
题意 题目链接 Sol 比赛结束后才调出来..不多说啥了,就是因为自己菜. 裸的up-down dp,维护一下一个点上下的直径就行,一开始还想了个假的思路写了半天.. 转移都在代码注释里 毒瘤题目卡空 ...
- bzoj 4871: [Shoi2017]摧毁“树状图”【树形dp】
做不来--参考https://www.cnblogs.com/ezyzy/p/6784872.html #include<iostream> #include<cstdio> ...
- BZOJ3302: [Shoi2005]树的双中心
BZOJ3302: [Shoi2005]树的双中心 https://lydsy.com/JudgeOnline/problem.php?id=3302 分析: 朴素算法 : 枚举边,然后在两个连通块内 ...
- 【BZOJ3302】[Shoi2005]树的双中心 DFS
[BZOJ3302][Shoi2005]树的双中心 Description Input 第一行为N,1<N<=50000,表示树的节点数目,树的节点从1到N编号.接下来N-1行,每行两个整 ...
- 题解-SHOI2005 树的双中心
SHOI2005 树的双中心 给树 \(T=(V,E)(|V|=n)\),树高为 \(h\),\(w_u(u\in V)\).求 \(x\in V,y\in V:\left(\sum_{u\in V} ...
- 【BZOJ】3302: [Shoi2005]树的双中心 && 2103: Fire 消防站 && 2447: 消防站
[题意]给定带点权树,要求选择两个点x,y,满足所有点到这两个点中较近者的距离*点权的和最小.n<=50000,h<=100. [算法]树的重心 [题解]代码参考自:cgh_Andy 观察 ...
- BZOJ.2159.Crash的文明世界(斯特林数 树形DP)
BZOJ 洛谷 挺套路但并不难的一道题 \(Description\) 给定一棵\(n\)个点的树和\(K\),边权为\(1\).对于每个点\(x\),求\(S(x)=\sum_{i=1}^ndis( ...
- hdu 4612 Warm up 双连通+树形dp思想
Warm up Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others) Total S ...
- HDU 2242 考研路茫茫—空调教室 (边双连通+树形DP)
<题目链接> 题目大意: 给定一个连通图,每个点有点权,现在需要删除一条边,使得整张图分成两个连通块,问你删除这条边后,两联通块点权值和差值最小是多少. 解题分析: 删除一条边,使原连通图 ...
随机推荐
- TensorFlow_action
安装TensorFlow 包依赖 C:\Users\sas> pip3 install --upgrade tensorflow Collecting tensorflow Downloadi ...
- 我的Android进阶之旅------>Android中android:windowSoftInputMode的用法
面试题:如何在显示某个Activity时立即弹出软键盘? 答案:在AndroidManifest.xml文件中设置<activity>标签的android:windowSoftInputM ...
- 关于date和String互相转换的问题
其实原理很简单,就是将String类型的变量使用SimpleDateFormat来转换成Date,然后用getTime()方法比较 SimpleDateFormat sdf = new SimpleD ...
- Spark0.9.0机器学习包MLlib-Classification代码阅读
本章主要讲述MLlib包里面的分类算法实现,目前实现的有LogisticRegression.SVM.NaiveBayes ,前两种算法针对各自的目标优化函数跟正则项,调用了Optimization模 ...
- dygraphs for R
dygraphs一个功能非常强大的处理时间序列的画图包!画出的图在html中打开,鼠标点处,即可得到数据信息.详情见 http://rstudio.github.io/dygraphs/index.h ...
- html post
post请求对应的html页面 页面效果 html代码 <html> <body> <form method="post" > First na ...
- SpringBoot学习笔记(2):引入Spring Security
SpringBoot学习笔记(2):用Spring Security来保护你的应用 快速开始 本指南将引导您完成使用受Spring Security保护的资源创建简单Web应用程序的过程. 参考资料: ...
- js琐碎知识点
1.javascript发展史 javascript首先由Netscape设计,为改善浏览器用户体验,名为liveScript, 网景公司被sun公司收购,为了宣传改名为javascript 后来su ...
- 使用deepfashion实现自己的第一个分类网络
这个过程主要分为三个步骤: 数据预处理 数据处理就是把数据按照一定的格式写出来,以便网路自己去读取数据 1准备原始数据 我的cloth数据一共是四个类别,每个类别有衣服47张,一用是188张图片,这些 ...
- css判断iphoneX、iphoneXs、iphoneXs Max、iphone XR
//iphoneX.iphoneXs @media only screen and (device-width: 375px) and (device-height: 812px) and (-web ...