[Bzoj3611]大工程(虚树+DP)
Description
Solution
在虚树上跑DP即可
关于虚树的建立,是维护一个最右链的过程
关键代码如下:
sort(A+1,A+k+1,cmp);//按dfs序排序
s[top=1]=1;//栈维护最右链
for(int i=1;i<=k;++i){
int t=A[i],f=0;
while(top){
f=LCA(A[i],s[top]);
if(top>1&&dep[f]<dep[s[top-1]])
Link(s[top-1],s[top]),top--;
else if(dep[f]<dep[s[top]]){Link(f,s[top--]);break;}
else break;
}
if(s[top]!=f) s[++top]=f;
s[++top]=t;
}
while(--top) Link(s[top],s[top+1]);
Code
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#define Inf 0x7fffffff
#define ll long long
#define N 1000010
using namespace std;
struct info{int to,nex,w;}e[N*4];
int n,tot,head[N],dfn[N],fa[N][20],_log,dep[N],A[N],mn[N],mx[N];
bool b[N];
ll Ans1,Ans2,f[N],size[N],sum;
inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
inline void Link(int u,int v){
if(u==v) return;
e[++tot].to=v;e[tot].nex=head[u];head[u]=tot;e[tot].w=dep[v]-dep[u];
}
void dfs(int u,int pre){
dfn[u]=++tot;
for(int i=1;i<=_log;++i)
fa[u][i]=fa[fa[u][i-1]][i-1];
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
if(v==pre) continue;
dep[v]=dep[u]+1;
fa[v][0]=u;
dfs(v,u);
}
head[u]=0;
}
int LCA(int u,int v){
if(dep[u]>dep[v]) swap(u,v);
int d=dep[v]-dep[u];
for(int i=0;i<=_log;++i)
if(d&(1<<i)) v=fa[v][i];
if(u==v) return v;
for(int i=_log;i>=0;--i)
if(fa[u][i]!=fa[v][i]){
u=fa[u][i];
v=fa[v][i];
}
return fa[u][0];
}
bool cmp(int a,int b){return dfn[a]<dfn[b];}
int s[N],top;
void bulid(){
int k=read();for(int i=1;i<=k;++i) b[A[i]=read()]=1;
sort(A+1,A+k+1,cmp);
s[top=1]=1;
for(int i=1;i<=k;++i){
int t=A[i],f=0;
while(top){
f=LCA(A[i],s[top]);
if(top>1&&dep[f]<dep[s[top-1]])
Link(s[top-1],s[top]),top--;
else if(dep[f]<dep[s[top]]){Link(f,s[top--]);break;}
else break;
}
if(s[top]!=f) s[++top]=f;
s[++top]=t;
}
while(--top) Link(s[top],s[top+1]);
}
void dp(int u){
size[u]=b[u];
f[u]=0;
mn[u]=b[u]?0:Inf;
mx[u]=b[u]?0:-Inf;
for(int i=head[u];i;i=e[i].nex){
int v=e[i].to;
dp(v);
sum+=(f[u]+size[u]*e[i].w)*size[v]+f[v]*size[u];
size[u]+=size[v];
f[u]+=f[v]+e[i].w*size[v];
Ans1=min(Ans1,(ll)mn[u]+mn[v]+e[i].w);
Ans2=max(Ans2,(ll)mx[u]+mx[v]+e[i].w);
mn[u]=min(mn[u],mn[v]+e[i].w);
mx[u]=max(mx[u],mx[v]+e[i].w);
}
head[u]=0;
}
int main(){
n=read();_log=log(n)/log(2);
for(int i=1;i<n;++i){
int u=read(),v=read();
Link(u,v);Link(v,u);
}
tot=0;dfs(1,0);
int q=read();
while(q--){
bulid();
Ans1=1e16,Ans2=-1e16,sum=0;
dp(1);
printf("%lld %lld %lld\n",sum,Ans1,Ans2);
memset(b,0,sizeof(b));
}
return 0;
}
[Bzoj3611]大工程(虚树+DP)的更多相关文章
- [HEOI2014][bzoj3611] 大工程 [虚树+dp]
题面: 传送门 思路: 又是一道虚树入门级的题目,但是这道题的实际难点在于dp 首先,这道题是可以点分治做的,而且因为6s时限随便浪,所以写点分治也不是不可以 但是,dp因为$O\left(n\rig ...
- bzoj 3611[Heoi2014]大工程 虚树+dp
题意: 给一棵树 每次选 k 个关键点,然后在它们两两之间 新建 C(k,2)条 新通道. 求: 1.这些新通道的代价和 2.这些新通道中代价最小的是多少 3.这些新通道中代价最大的是多少 分析:较常 ...
- luogu P4103 [HEOI2014]大工程 虚树 + 树形 DP
Description 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通 ...
- 洛谷P4103 [HEOI2014]大工程(虚树 树形dp)
题意 链接 Sol 虚树. 首先建出虚树,然后直接树形dp就行了. 最大最小值直接维护子树内到该节点的最大值,然后合并两棵子树的时候更新一下答案. 任意两点的路径和可以考虑每条边两边的贡献,\(d[x ...
- BZOJ.3611.[HEOI2014]大工程(虚树 树形DP)
题目链接 要求的和.最大值.最小值好像都可以通过O(n)的树形DP做,总询问点数<=2n. 于是建虚树就可以了.具体DP见DP()函数,维护三个值sum[],mx[],mn[]. sum[]要开 ...
- bzoj 3611: [Heoi2014]大工程 虚树
题目: 国家有一个大工程,要给一个非常大的交通网络里建一些新的通道. 我们这个国家位置非常特殊,可以看成是一个单位边权的树,城市位于顶点上. 在 2 个国家 a,b 之间建一条新通道需要的代价为树上 ...
- 【HEOI2014】大工程<虚树>
虚树 我们每天都用心思索着,这究竟是为了什么呢?我想我也不知道,只是觉得如果人不思考问题就很无聊. 我觉得虚树不是什么数据结构,就是一种技巧或者工具.它能把树中\(k\)个关键点以\(O(klogk) ...
- bzoj 3611(洛谷 4103) [Heoi2014]大工程——虚树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3611 https://www.luogu.org/problemnew/show/P4103 ...
- BZOJ 3611 [Heoi2014]大工程 ——虚树
虚树第二题.... 同BZOJ2286 #include <map> #include <cmath> #include <queue> #include < ...
- bzoj 3572世界树 虚树+dp
题目大意: 给一棵树,每次给出一些关键点,对于树上每个点,被离它最近的关键点(距离相同被标号最小的)控制 求每个关键点控制多少个点 分析: 虚树+dp dp过程如下: 第一次dp,递归求出每个点子树中 ...
随机推荐
- Angular搭建脚手架
1.安装CLI: cnpm install -g @angular/cli //卸载: npm uninstall -g @angular/cli npm cache clean 2.检测是否成功 ...
- SublimeText插件eslint : 语法检测
参考: http://www.tuicool.com/articles/faANRvj 安装之后的效果: 误用了 = ,在文件保存时就会被提示,直接顺手改掉就行了,方便的不行 步骤1:Sublime集 ...
- mac笔记本上的工具
svn可是换工具:cornerstone host修改工具:switchHosts!
- Unified Service Desk Overview
As we implement CRM in enterprise, we usually integrate with many other information system such as E ...
- ComponentOne、Spread、ActiveReports 5折起 加入惊喜惠
慧都十周年,GrapeCity也来共襄盛举,旗下三大产品产品线齐齐参与.界面控件套包ComponentOne.Excel表格控件Spread与报表开发工具ActiveReports,指定授权5折起加入 ...
- Android AS升级3.1 编译报错:The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin.
AndroidStudio升级到3.1后编译报错:The SourceSet ‘instrumentTest’ is not recognized by the Android Gradle Plug ...
- 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser
Intents 这个例子的代码非常简单: public void onGetMusic(View view) { Intent intent = new Intent(Intent.ACTION_GE ...
- html和java的交互,利用jsBridge开源框架
html中,js注册监听和回调 function connectWebViewJavascriptBridge(callback) { if (window.WebViewJavascriptBrid ...
- 仿照everything写的一个超级速查 原创
http://files.cnblogs.com/files/jacd/%E8%B6%85%E9%80%9F%E6%9F%A5%E6%96%87%E4%BB%B6.zip 速度奇快无比,体积奇小无比, ...
- C语言中 fputs() fgets() 的使用方法
一.读字符串函数fgets函数的功能是从指定的文件中读一个字符串到字符数组中,函数调用的形式为: fgets(字符数组名,n,文件指针): 其中的n是一个正整数.表示从文件中读出的字符串不超过 n-1 ...