虚树与树形动态规划

虚树可以看做是对树形动态规划的一种求解优化

对于需要求答案的点p,只保留对答案有影响的节点,从而减少时间

BZOJ2286

dp[i]=min(val[i],Σdp[j](j为i的儿子)),val[i]表示将i和根节点分离的代价

方程为什么这么写呢?val也就是从当前i到根节点的边权的最小值

为了让代价最小,最后的dp[root]就是答案了,树形dp是先递归调用到根然后回溯的过程中求值,回溯到根节点的时候答案就显然了

我们可以只用询问点及他们的LCA来建一颗新树,我们暂且称其为虚树,然后在虚树上跑dp,效率就会高很多

维护虚树的过程我没有看懂,只能参考程序了,用栈维护的

每次按照关键点的dfs序排序,维护栈构建一棵新的树,在新的树上dp

  1 #include<iostream>
  2 #include<set>
  3 #include<map>
  4 #include<cstdio>
  5 #include<cstring>
  6 #include<cstdlib>
  7 #include<ctime>
  8 #include<vector>
  9 #include<queue>
 10 #include<algorithm>
 11 #include<cmath>
 12 #include<bitset>
 13 #include<stack>
 14 #define inf 1e60
 15 #define pa pair<int,int>
 16 #define ll long long 
 17 using namespace std;
 18 int read()
 19 {
 20     int x=0,f=1;char ch=getchar();
 21     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
 22     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
 23     return x*f;
 24 }
 25 int bin[20];
 26 int n,m,cnt,ind,top;
 27 int last[250005],last2[250005],fa[250005][20];
 28 ll mn[250005],f[250005];
 29 int h[250005],mark[250005],deep[250005];
 30 int st[250005];
 31 struct edge{
 32     int to,next,v;
 33 }e[500005],ed[500005];
 34 void insert(int u,int v,int w)
 35 {
 36     e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w;
 37     e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;e[cnt].v=w;
 38 }
 39 void insert2(int u,int v)
 40 {
 41     if(u==v)return;
 42     ed[++cnt].to=v;ed[cnt].next=last2[u];last2[u]=cnt;
 43 }
 44 bool cmp(int a,int b)
 45 {
 46     return mark[a]<mark[b];
 47 }
 48 void pre(int x)
 49 {
 50     mark[x]=++ind;
 51     for(int i=1;bin[i]<=deep[x];i++)
 52         fa[x][i]=fa[fa[x][i-1]][i-1];
 53     for(int i=last[x];i;i=e[i].next)
 54         if(e[i].to!=fa[x][0])
 55         {
 56             mn[e[i].to]=min(mn[x],(ll)e[i].v);
 57             deep[e[i].to]=deep[x]+1;
 58             fa[e[i].to][0]=x;
 59             pre(e[i].to);
 60         }
 61 }
 62 int lca(int x,int y)
 63 {
 64     if(deep[x]<deep[y])swap(x,y);
 65     int t=deep[x]-deep[y];
 66     for(int i=0;bin[i]<=t;i++)
 67         if(t&bin[i])x=fa[x][i];
 68     for(int i=19;i>=0;i--)
 69         if(fa[x][i]!=fa[y][i])
 70             x=fa[x][i],y=fa[y][i];
 71     if(x==y)return x;
 72     return fa[x][0];
 73 }
 74 void dp(int x)
 75 {
 76     f[x]=mn[x];
 77     ll tmp=0;
 78     for(int i=last2[x];i;i=ed[i].next)
 79     {
 80         dp(ed[i].to);
 81         tmp+=f[ed[i].to];
 82     }
 83     last2[x]=0;
 84     if(tmp==0)f[x]=mn[x];
 85     else if(tmp<=f[x])f[x]=tmp;
 86 }
 87 void solve()
 88 {
 89     cnt=0;
 90     int K=read();
 91     for(int i=1;i<=K;i++)
 92         h[i]=read();
 93     sort(h+1,h+K+1,cmp);
 94     int tot=0;
 95     h[++tot]=h[1];
 96     for(int i=2;i<=K;i++)
 97         if(lca(h[tot],h[i])!=h[tot])h[++tot]=h[i];
 98     st[++top]=1;
 99     for(int i=1;i<=tot;i++)
100     {
101         int now=h[i],f=lca(now,st[top]);
102         while(1)
103         {
104             if(deep[f]>=deep[st[top-1]])
105             {
106                 insert2(f,st[top--]);
107                 if(st[top]!=f)st[++top]=f;
108                 break;
109             }
110             insert2(st[top-1],st[top]);top--;
111         }
112         if(st[top]!=now)st[++top]=now;
113     }
114     while(--top)insert2(st[top],st[top+1]);
115     dp(1);
116     printf("%lld\n",f[1]);
117 }
118 int main()
119 {
120     bin[0]=1;for(int i=1;i<20;i++)bin[i]=bin[i-1]<<1;
121     n=read();
122     for(int i=1;i<n;i++)
123     {
124         int u=read(),v=read(),w=read();
125         insert(u,v,w);
126     }
127     mn[1]=inf;pre(1);
128     m=read();
129     for(int i=1;i<=m;i++)
130         solve();
131     return 0;
132 }

猜你喜欢

转载自www.cnblogs.com/aininot260/p/9623521.html
今日推荐