BZOJ 2286 消耗战(虚树模板题)

本文介绍了一种解决特定树形结构问题的方法——通过构建虚树并运用树上动态规划技术来高效处理大量询问。文章详细解释了如何进行树的预处理、使用树上倍增法求最近公共祖先(LCA)、构建虚树的过程以及树形DP的具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接https://www.lydsy.com/JudgeOnline/problem.php?id=2286

题解:多次询问+每次询问少量点----->直接在原树上dp会超时----->每次询问构建虚树,在虚树上dp

具体步骤

1. 对于初始的树双向建边

2. dfs预处理得到一些数据

dfn【i】 -->  i节点的dfs序

dep【i】 --> i节点的深度(第几层)

anc【i】【0】i向上2^0层的祖先节点,也就是父节点

dis 【i】-->将i节点与根节点脱离所需要的最小花费

void dfs(int now,int depth){
    dfn[now] = cnt_dfs++;
    dep[now] = depth;
    for (int i = Ghead[now];i!=0;i=G[i].last){
        int v = G[i].to;
        if (v == anc[now][0]) continue;
        anc[v][0] = now;//printf("anc[%d][0]=%d\n",v,anc[v][0]);
        dis[v] = min(dis[now],G[i].w);
        dfs(v,depth+1);
    }
}

3.用树上倍增法求LCA

可以参考一下这篇博客的讲解:https://www.cnblogs.com/yyf0309/p/5972701.html

get_anc这个函数就不讲了,我讲一下自己对LCA函数中两个for循环的理解

void get_anc()
{
    for (int j=1;1<<j<=n;j++)
        for (int i=1;i<=n;i++){
            int a = anc[i][j-1];
            if (~a) anc[i][j] = anc[a][j-1];
        }
}

int LCA(int x,int y)
{
    if (dep[x]<dep[y]) swap(x,y);
    int differ = dep[x] - dep[y];
    for (int i = 0;1<<i<=differ;i++)
        if (differ & (1<<i)) x = anc[x][i];
    if (x==y) return x;
    for (int i =18;i>=0;i--)
        if (~anc[x][i] && anc[x][i]!=anc[y][i]) x = anc[x][i], y = anc[y][i];
    return anc[x][0];
}

①第一个for循环:

首先differ表示深度之差,第一个循环的目的是为了让x,y达到相同深度。

if(判断differ转换为二进制后当前位上是否为1)

将differ转化为2进制 :假设是10101(21),那么跳2^0,2^2,2^4,x就和y相同深度了

②第二个for循环:

i从大到小遍历很关键。

我们都知道,当x和y跳到LCA后,之后不管跳几步,两者的LCA都是同一个,因为x和y已经是同一个点了。

如果跳当前步数两者祖先不一样,那么,他们一定还没到LCA,对吧?于是通过这个for循环最后两个点停在了

LCA的下一层,于是LCA就为当前x/y的父节点(anc[x][0]  or  anc[y][0])

4.对于每次询问建虚树:(建议不要吊死在一篇博客上看懂这个,多翻几篇)

(首先对物资点按dfs序排序,开一个bool型数组把这些物资点标记了)

a【i】表示按dfs序排序后第i个要插入的物资点

id = 1;
int top=0;        
sta[++top] = root; 
       
for (int i =1;i<=k;i++){
    int lca = LCA(sta[top],a[i]);
    if (lca!=sta[top]){
        while (dep[lca] < dep[sta[top]]){
            if (dep[lca]>=dep[sta[top-1]]){
                add(lca,sta[top]);
                top--;
                if (sta[top]!=lca) sta[++top] = lca;
                    break;
            }
            add(sta[top-1],sta[top]);
            top--;
        }
    }
    sta[++top] = a[i];
}

建的是以1为根节点的虚树(单向建边,务必注意)

设u为新加入的点,x为栈顶元素,y为栈顶之后的那个元素(栈中第二个),lca = LCA(u,x)

栈中储存的是右链,是最后可以一次性连起来的点

插入u,碰到两种情况:
lca = x,属于一条右链,u直接入栈
lca != x,表明x和u不在同一树链上{
    dfn【y】 > dfn【lca】 ,而且表明u的层次还挺高的,之前的树链中在dfs序在
lca以后的(dfn>lca)都要连起来退栈了
    dfn【y】= dfn【lca】  ,直接把x连到y上退栈,然后break;u进栈就完事儿了
    dfn【y】< dfn【lca】  ,表明lca不在树链上且x在lca的子树上,那么把
x连在lca上,x退栈,lca进栈就ok,然后break;最后u进栈
}

最后剩一条树链全部连起来虚树就完成了。

我用的是dep,不是dfn,不过思路是一样的,因为同一深度+拥有某子节点-->确定的一个点(而不是深度相同的任意一个点),思路和上面一样。

5.树形dp

dp[i]-->将i节点的子树脱离i需要的最小花费

void treedp(int now,bool marked)
{
    dp[now] = dis[now];
    if (marked){
        for (int i = head[now];i!=0;i=edge[i].last)
            treedp(edge[i].to,mark[edge[i].to]);//
        mark[now] = false;
        head[now] = 0;
        return ;
    }
    ll ans=0;
    for (int i = head[now];i!=0;i=edge[i].last){
        treedp(edge[i].to,mark[edge[i].to]);
        ans += dp[edge[i].to];
    }
    dp[now] = min(ans,dp[now]);
    head[now] = 0;
}

if (当前节点是物资点) 那么显然dp[i] = dis[i]

else dp[i] = min(dis[i],  i的子节点的dp和)

口胡:

这里你会不会有一个问题?(毕竟dis数组的构造就很奇怪)

当dp【i】的值=i的子节点的dp和时而不是dis[i],会不会有子节点去掉的边在i前面,导致其实i已经掉了。-->就是说为了让i的子节点去掉而砍掉的边会不会把i也顺便砍掉了,导致dp[i]错?

我后来想了一下发现不会这样:如果砍掉的是i之前的边,那么子节点的dp值一定等于dis[i],现在子节点砍掉的和比dis【i】小,那么这边肯定在i节点的子树上。

 

6.最后放一下完整代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
#define debug(x) printf("----Line:%s----\n",#x)
#define inf 1LL<<62
using namespace std;

const int N = 250000+5;

ll dp[N],dis[N];
int anc[N][20], dep[N], dfn[N], head[N<<1], Ghead[N<<1], id=1, gid=1, cnt_dfs=1, n, root, a[N], sta[N];
bool mark[N];

bool cmp(int a,int b){return dfn[a]<dfn[b];}

struct Gnode
{
    int to,last;
    ll w;
}G[N<<1];

void addG(int u,int v,ll w){G[gid].to = v;G[gid].w = w;G[gid].last = Ghead[u];Ghead[u] = gid++;}

struct node
{
    int to,last;
}edge[N];

void add(int u,int v){edge[id].to = v;edge[id].last = head[u];head[u] = id++;}

void dfs(int now,int depth){
    dfn[now] = cnt_dfs++;
    dep[now] = depth;
    for (int i = Ghead[now];i!=0;i=G[i].last){
        int v = G[i].to;
        if (v == anc[now][0]) continue;
        anc[v][0] = now;//printf("anc[%d][0]=%d\n",v,anc[v][0]);
        dis[v] = min(dis[now],G[i].w);
        dfs(v,depth+1);
    }
}

void get_anc()
{
    for (int j=1;1<<j<=n;j++)
        for (int i=1;i<=n;i++){
            int a = anc[i][j-1];
            if (~a) anc[i][j] = anc[a][j-1];
        }
}

int LCA(int x,int y)
{
    if (dep[x]<dep[y]) swap(x,y);
    int differ = dep[x] - dep[y];
    for (int i = 0;1<<i<=differ;i++)
        if (differ & (1<<i)) x = anc[x][i];
    if (x==y) return x;
    for (int i =18;i>=0;i--)
        if (~anc[x][i] && anc[x][i]!=anc[y][i]) x = anc[x][i], y = anc[y][i];
    return anc[x][0];
}

void treedp(int now,bool marked)
{
    dp[now] = dis[now];
    if (marked){
        for (int i = head[now];i!=0;i=edge[i].last)
            treedp(edge[i].to,mark[edge[i].to]);//
        mark[now] = false;
        head[now] = 0;
        return ;
    }
    ll ans=0;
    for (int i = head[now];i!=0;i=edge[i].last){
        treedp(edge[i].to,mark[edge[i].to]);
        ans += dp[edge[i].to];
    }
    dp[now] = min(ans,dp[now]);
    head[now] = 0;
}

int main()
{
    scanf("%d",&n);
    int u,v;
    ll w;
    for (int i =1;i<n;i++){
        scanf("%d %d %lld",&u,&v,&w);
        addG(u,v,w);
        addG(v,u,w);
    }//debug(1);
    root = 1;
    memset(anc,-1,sizeof anc);
    dis[root] = inf;
    dfs(root,1);//debug(2);
    get_anc();//debug(3);
    ///´¦ÀíѯÎÊ
    int q,k;
    scanf("%d",&q);
    while (q--){
        scanf("%d",&k);
        for (int i=1;i<=k;i++) scanf("%d",a+i),mark[a[i]]=true;
        sort(a+1,a+1+k,cmp);
        ///--------build a 虚树------------------------------------------
        id = 1;
        int top=0;
        sta[++top] = root;
        for (int i =1;i<=k;i++){
            int lca = LCA(sta[top],a[i]);
            if (lca!=sta[top]){
                while (dep[lca] < dep[sta[top]]){
                    if (dep[lca]>=dep[sta[top-1]]){
                        add(lca,sta[top]);
                        top--;
                        if (sta[top]!=lca) sta[++top] = lca;
                        break;
                    }
                    add(sta[top-1],sta[top]);
                    top--;
                }
            }
            sta[++top] = a[i];
        }
        while(--top) add(sta[top],sta[top+1]);
        ///--------------------------------------------------------
        treedp(root,false);
        printf("%lld\n",dp[root]);
    }
    return 0;
}


吐槽

首先对因为我也不是很懂而没讲清楚的地方向您道歉。

抱着写完就忘的心态写完了这篇东西。(没见识的我:这题代码真的好长啊,这还tm只是个模板题)

啊,写的太长了,这种又臭又长的文章真的有人看吗   o(╥﹏╥)o

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值