虚树
来源:互联网 发布:数据库系统概论电子书 编辑:程序博客网 时间:2024/06/03 00:35
bzoj
2286√
题意
一棵n(250000)小于个点的有边权树,m次询问,每次询问断开k个点(两两不相连)的最小花费(所有k加起来小于500000)
分析
嗯~显然虚树~~
虚树搞完以后的问题就变成了一道朴素的树形dp题了。
显然:
#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;const int N=250100;int bh[N],q[N],h[N],st[N],dep[N],fa[N][21];int top,tt,n,m,x,y,z,tot,cnt,a[N],pos[N];long long mn[N],f[N];struct edge{int y,z,next;}g[N*2],b[N*2];void addpath(int x,int y,int z){ g[++tot].y=y; g[tot].next=h[x]; g[tot].z=z; h[x]=tot;}void adp(int x,int y){ if (x==y) return; b[++tt].y=y; b[tt].next=bh[x]; bh[x]=tt;}void dfs(int x){ pos[x]=++cnt; for (int i=h[x];i;i=g[i].next) if (g[i].y!=fa[x][0]) { mn[g[i].y]=min(mn[x],(long long)g[i].z); fa[g[i].y][0]=x; dep[g[i].y]=dep[x]+1; dfs(g[i].y); }}int cmp(int a,int b){ return pos[a]<pos[b];}int lca(int x,int y){ if (dep[x]<dep[y]) swap(x,y); for (int i=19;i>=0;i--) if (dep[x]-dep[y]>=(1<<i)) x=fa[x][i]; for (int i=19;i>=0;i--) if (fa[x][i]!=fa[y][i]) x=fa[x][i],y=fa[y][i]; if (x==y) return x; return fa[x][0];}void dp(int x){ f[x]=mn[x];long long res=0; for (int i=bh[x];i;i=b[i].next) { dp(b[i].y); res+=f[b[i].y]; } bh[x]=0; if (res && f[x]>res) f[x]=res;}int main(){ freopen("a.in","r",stdin); scanf("%d",&n); for (int i=1;i<n;i++) { scanf("%d%d%d",&x,&y,&z); addpath(x,y,z); addpath(y,x,z); } mn[1]=1e60;dfs(1); for (int j=1;j<=20;j++) for (int i=1;i<=n;i++) fa[i][j]=fa[fa[i][j-1]][j-1]; int ttt;scanf("%d",&ttt); for (int i=1;i<=ttt;i++) { scanf("%d",&m); for (int j=1;j<=m;j++) scanf("%d",a+j); sort(a+1,a+m+1,cmp); q[tot=1]=a[1];tt=0; for (int i=2;i<=m;i++) if (lca(q[tot],a[i])!=q[tot]) q[++tot]=a[i]; st[top=1]=1; for (int j=1;j<=tot;j++) { int now=q[j],f=lca(now,st[top]); while (1) { if (dep[f]>=dep[st[top-1]]) { adp(f,st[top--]); if (st[top]!=f) st[++top]=f; break; } adp(st[top-1],st[top]);top--; } if (st[top]!=now) st[++top]=now; } while (--top) adp(st[top],st[top+1]); dp(1); printf("%lld\n",f[1]); }}
3572√
3879(后缀树+虚树)
3991√
(set维护dfs序答案为相邻的距离和最后加上第一个和最后一个的距离。是虚树所有边的两倍)
3611√
0 0
- 虚树
- 虚树
- 虚树
- 虚树
- 【虚树+LCA】
- 【Learning】虚树
- bzoj3611 大工程 虚树
- [BZOJ2286][Sdoi2011消耗战] 虚树
- BZOJ2286: [Sdoi2011]消耗战 虚树
- 【XSY1545】直径 虚树 DP
- bzoj 2286 虚树
- bzoj2286消耗战 虚树+树型动规
- bzoj 3991 虚树 set
- 【BZOJ3879】SvT 后缀树+虚树
- 【BZOJ3572】【Hnoi2014】世界树 虚树
- 【BZOJ 2286】消耗战(虚树)
- 【bzoj2286】【sdoi2011】【消耗战】【虚树+dp】
- 【bzoj3611】【大工程】【虚树+dp】
- javascript初探四
- android5.0沉浸状态栏
- List of devices getting official LineageOS Roms [ Full ]
- 读取assets目录下的文件
- Latex:TexStudio的使用
- 虚树
- 1
- linux 文本处理命令
- Anaconda虚拟环境迁移小记录
- 小程序知识点总结
- 问题五十六:怎么用ray tracing画参数方程表示的曲面(3)—— b-spline surface
- Download Gapps for Lineage OS All version 6.0, 7.1 {Daily Updated}
- 1069. The Black Hole of Numbers
- HDU 2158 最短区间版大家来找碴 [思维]【杂类】