【题意】给定n个点的带边权树,每次询问给定ki个特殊点,求隔离点1和特殊点的最小代价。n<=250000,Σki<=500000。

【算法】虚树+DP

【题解】考虑普通树上的dp,设f[x]表示隔离1和子树x内特殊点的最小代价,val[x]表示x到1路径上的最小代价(预处理)。

点x特殊,f[x]=val[x]

否则,f[x]=min{val[x],Σf[y]},y=son[x]

在询问总数有限制的前提下,可以建虚树进行如上DP。

复杂度O(Σki log n)。

注意:

1.清空时垃圾回收,保证复杂度。

2.询问点数组开两倍,因为要加入两两LCA。

#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int read(){
int s=,t=;char c;
while(!isdigit(c=getchar()))if(c=='-')t=-;
do{s=s*+c-'';}while(isdigit(c=getchar()));
return s*t;
}
const int maxn=;
struct edge{int v,w,from;}e[maxn*];
int in[maxn],ou[maxn],deep[maxn],f[maxn][],st[maxn],n,N,tot,first[maxn],a[maxn*];//a[]
bool v[maxn];
ll val[maxn]; namespace cyc{
struct edge{int v,w,from;}e[maxn*];
int first[maxn],dfsnum=,tot;
void insert(int u,int v,int w){tot++;e[tot].v=v;e[tot].w=w;e[tot].from=first[u];first[u]=tot;}
void dfs(int x,int fa){
in[x]=++dfsnum;
for(int j=;(<<j)<=deep[x];j++)f[x][j]=f[f[x][j-]][j-];
for(int i=first[x];i;i=e[i].from)if(e[i].v!=fa){
deep[e[i].v]=deep[x]+;
val[e[i].v]=min(val[x],1ll*e[i].w);
f[e[i].v][]=x;
dfs(e[i].v,x);
}
ou[x]=dfsnum;
}
int lca(int x,int y){
if(deep[x]<deep[y])swap(x,y);
int d=deep[x]-deep[y];
for(int i=;i<=;i++)if((<<i)&d)x=f[x][i];
if(x==y)return x;
for(int i=;i>=;i--)if((<<i)<=deep[x]&&f[x][i]!=f[y][i]){
x=f[x][i];y=f[y][i];
}
return f[x][];
}
void build(){
n=read();
for(int i=;i<n;i++){
int u=read(),v=read(),w=read();
insert(u,v,w);insert(v,u,w);
}
val[]=1ll<<;//
dfs(,-);
}
}
void insert(int u,int v){tot++;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
bool cmp(int a,int b){return in[a]<in[b];}
ll dp(int x){
if(v[x])return val[x];
ll sum=;
for(int i=first[x];i;i=e[i].from)sum+=dp(e[i].v);
return min(val[x],sum);
}
bool check(int x,int y){return in[y]<=in[x]&&ou[x]<=ou[y];}
void build(){
int last=read();N=last;
for(int i=;i<=N;i++)a[i]=read(),v[a[i]]=;//
sort(a+,a+N+,cmp);
for(int i=;i<last;i++)a[++N]=cyc::lca(a[i],a[i+]);
sort(a+,a+N+,cmp);
N=unique(a+,a+N+)-a-;
for(int i=;i<=N;i++)first[a[i]]=;tot=;//
int top=;
for(int i=;i<=N;i++){
while(top&&!check(a[i],st[top]))top--;
if(top)insert(st[top],a[i]);
st[++top]=a[i];
}
printf("%lld\n",dp(a[]));
for(int i=;i<=N;i++)v[a[i]]=;//
}
int main(){
cyc::build();
int m=read();
while(m--)build();
return ;
}

【BZOJ】2286: [Sdoi2011]消耗战 虚树+DP的更多相关文章

  1. bzoj 2286 [Sdoi2011]消耗战 虚树+dp

    题目大意:多次给出关键点,求切断边使所有关键点与1断开的最小费用 分析:每次造出虚树,dp[i]表示将i和i子树与父亲断开费用 对于父亲x,儿子y ①y为关键点:\(dp[x]\)+=\(dismn( ...

  2. bzoj 2286: [Sdoi2011]消耗战 虚树+树dp

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 在一 ...

  3. BZOJ 2286: [Sdoi2011]消耗战 虚树 树形dp 动态规划 dfs序

    https://www.lydsy.com/JudgeOnline/problem.php?id=2286 wa了两次因为lca犯了zz错误 这道题如果不多次询问的话就是裸dp. 一棵树上多次询问,且 ...

  4. BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)

    题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...

  5. BZOJ 2286 [Sdoi2011]消耗战 ——虚树

    虚树第一题. 大概就是建一颗只与询问有关的更小的新树,然后在虚树上DP #include <map> #include <ctime> #include <cmath&g ...

  6. BZOJ 2286: [Sdoi2011]消耗战 虚树

    Description 在一场战争中,战场由n个岛屿和n-1个桥梁组成,保证每两个岛屿间有且仅有一条路径可达.现在,我军已经侦查到敌军的总部在编号为1的岛屿,而且他们已经没有足够多的能源维系战斗,我军 ...

  7. [BZOJ2286][SDOI2011]消耗战(虚树DP)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4998  Solved: 1867[Submit][Statu ...

  8. BZOJ 2286: [Sdoi2011]消耗战

    2286: [Sdoi2011消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 2082  Solved: 736[Submit][Status] ...

  9. BZOJ 3572 [HNOI2014]世界树 (虚树+DP)

    题面:BZOJ传送门 洛谷传送门 题目大意:略 细节贼多的虚树$DP$ 先考虑只有一次询问的情况 一个节点$x$可能被它子树内的一个到x距离最小的特殊点管辖,还可能被管辖fa[x]的特殊点管辖 跑两次 ...

随机推荐

  1. 买卖股票的最佳时机II

    题目描述 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润. 注意你不能在买入股票前卖出股 ...

  2. Python3 与 C# 基础语法对比(List、Tuple、Dict、Set专栏)

      Code:https://github.com/lotapp/BaseCode 多图旧版:https://www.cnblogs.com/dunitian/p/9156097.html 在线预览: ...

  3. java实现sftp客户端上传文件以及文件夹的功能

    1.依赖的jar文件 jsch-0.1.53.jar 2.登录方式有密码登录,和密匙登录 代码: 主函数: import java.util.Properties; import com.cloudp ...

  4. MVC aspx

    客户端服务器---Model(模型)---View(视图)---Control(控制器) 1.ASP.NET  MVC 2.新建项目引擎选aspx.在Controllers创建控制器,默认启动Home ...

  5. 洛谷P4363 一双木棋 chess

    洛谷P4363 一双木棋 chess 省选最水的一道题了. 且看我数个月AC一道题...... 具体是这样的:我们发现这个下了棋的地方一定形成一个锯齿形,那么怎么状态压缩呢? 维护轮廓线! 从左下角出 ...

  6. A1113. Integer Set Partition

    Given a set of N (> 1) positive integers, you are supposed to partition them into two disjoint se ...

  7. 详解Vue 开发模式下跨域问题

    vue项目中,前端与后台进行数据请求或者提交的时候,如果后台没有设置跨域,前端本地调试代码的时候就会报“No 'Access-Control-Allow-Origin' header is prese ...

  8. 退回win7后无法上网 的解决方法

    如果网卡驱动没问题的话,那你是不是装了360安全卫士,如果装了你打开网络和共享中心———更改适配器设置————右键本地连接———属性————把360局域网防护驱动程序前面的对勾去掉然后确定,一般就能解 ...

  9. 【优秀的Sketch插件】PaintCode for Sketch for Mac 1.1

    [简介] PaintCode for Sketch 是一款结合了PaintCode和Sketch两款软件强大功能的插件,今天和大家分享最新的1.1版本,支持Sketch 40+版本,这个插件能够让你的 ...

  10. maven编译或者打包web项目显示“软件包 javax.servlet.http 不存在"

    2.解决办法: 这是由于缺少servlet-api.jar包,其实tomcat下有,但是在java build path把他加载过来,还是报这个错误,所以我们直接在pom.xml里面加入这个jar包即 ...