【学习笔记】虚树

虚树学习笔记

前言:前两天北航校赛有一题考了这个,看到题解觉得挺神奇的,之后又在南昌网络赛的题解里看到这个词,赶紧先来把板子学了

先了解几个基本的问题

Q:虚树是做什么的? A:虚树就是把一棵树上的对问题有影响的点扒出来,建一棵新的树

Q:为什么会需要这个东西? A:如果题目有多组询问,每次询问的点不同,树上其他的点不会对答案产生影响,每次询问对所有的点跑DFS或者DP,是很浪费时间的,尤其是询问的数量特别多的情况

Q:对每次询问建一棵新的树,这样不会很浪费时间吗? A:对\(m\)个点,建立虚树的复杂度可以优化到\(O(m\log n)\) (甚至可以优化到\(O(m)\))。对所有询问,总的复杂度就是\(O(\sum{m} \log{n})\), 本篇文章的核心也在于虚树的构建。

Q:如果\(\sum{m}=q*n\)呢? A:再见

虚树的构造

基本思路:

  1. 用栈维护一个链
  2. 按照dfs序,从小到大尝试向虚树中添加点
  3. 如果当前的点可以链接到 当前栈所维护的链,或者栈中的点少于1(没有链),直接入栈
  4. 如果当前的点不能链接到栈中的链,即\(lca(u,s.top)\neq s.top\)
    \(v=s.top,g=lca(u,v)\),说明\(u,v\)分别在\(g\)的两个子树上,且\(v\)所在的子树已经全部扒完了(从dfs序的角度考虑
    这种情况下,把\(v\)所在的\(g\)子树退栈,边退栈边在虚树中建边,直到\(pre(s.top) <= pre(g)\) 如果\(s.top \neq g\), \(g\)入栈,之前退掉的最后一个点连到\(g\)
  5. 所有的点填入之后,栈中保存的是一条链,不在栈中的也通过\(lca\)和这条链相连接

    整理一下:

    先将询问的点按照\(dfs\)序从小到大排序 对于当前点\(u\):
  6. 如果当前栈大小小于1,直接入栈
  7. 如果\(lca(s.top.u)=s.top\), 直接入栈
  8. 如果\(lca(s.top,u)\neq s.top\),将\(s.top\)向上直至\(lca\)的链退栈并连边(保留\(lca\)),\(u\)入栈
  9. 如果\(u\)是最后一个点,清空栈,同时将栈中的点连边

    代码如下

sort(p+1, p+1+Vn, cmp);
stak[++top] = 1; 
for(int i = 1; i <= Vn; i++){
    if(top <= 1){
        stak[++top] = p[i];
    }
    else{
        int g = lca(stak[top],p[i]);
        if(g == stak[top]){
            stak[++top] = p[i];
        }
        else{
            while(top > 1 && pre[stak[top-1]] >= pre[g]){
                VG[stak[top-1]].push_back(stak[top]);
                top--;
            }
            if(g != stak[top]){
                VG[g].push_back(stak[top]);
                stak[top] = g;
            }
            stak[++top] = p[i];
        }
    }
}
while(top > 1){
    VG[stak[top-1]].push_back(stak[top]);
    top--;
}

例题 [SDOI2011]消耗战

题意:

给定一个树\((2\leq n \leq 250000)\),\(m\)次询问,每次给定\(k_i\)个点,回答最少需要切断的边的权值之和,让\(1\)与给定的\(k_i\)个点不连通 \(\sum{k_i} \leq 500000\)

解法:

  1. 先考虑单次询问,可以预处理每个点的 令点\(i\)\(1\)不连通的最小费用\(cost_i\), 然后\(dfs\)即可
  2. 再考虑多次询问,由于\(\sum{k_i}\)是固定的,显然可以构建虚树解决

参考代码

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5;
//
struct Edge{
    int u,v;
    int d;
    int nxt;
    Edge(int u,int v,int d,int nxt):
        u(u),v(v),d(d),nxt(nxt){};
    Edge(){};
};
Edge e[maxn*2];
int edge_cnt, head[maxn];
int n, m;
//

//lca
int fa[maxn][20];
int pre[maxn];
int dfs_clock = 0;
int depth[maxn];
int lg[maxn];
//
int cost[maxn]; 
void dfs(int u){
    pre[u] = ++dfs_clock;
    for(int i = head[u]; i; i = e[i].nxt){
        if(pre[e[i].v])continue;

        // 倍增LCA
        fa[e[i].v][0] = u;
        depth[e[i].v] = depth[u] + 1;
        for(int j = 1; j < 20; j++){
            fa[e[i].v][j] = fa[ fa[e[i].v][j-1] ][j-1];
            if(fa[e[i].v][j] == 0)break;
        }
        //
        // DP处理 切断root到当前子树需要最小的cost
        if(u == 1)cost[e[i].v] = e[i].d;
        else cost[e[i].v] = min(cost[u], e[i].d);

        dfs(e[i].v);
    }
    return;
}
int lca(int u,int v){
    if(depth[u] < depth[v])swap(u,v);
    while(depth[u] > depth[v])
        for(int j = lg[depth[u]-depth[v]]; j >= 0; j--){
            if(depth[fa[u][j]] >= depth[v])u = fa[u][j];
        }
    while(u != v){
        for(int j = lg[depth[u]]; j >= 0; j--){
            while(fa[u][j] == fa[v][j] && j > 0)j--;
            u = fa[u][j]; v = fa[v][j];
        }
    }
    return u;
}
bool k[maxn];
int p[maxn];

int stak[maxn];
int top = 0;

// 虚树上dfs
vector<int> VG[maxn];
ll solve(int u){
    ll cost = 0;
    for(int i : VG[u]){
        cost += solve(i);
    }
    VG[u].clear();
    if(u == 1)return cost;
    if(k[u])return cost[u];
    else return min(cost[u],cost);
}

//按照dfs序对结点进行排序
bool cmp(int u,int v){
    return pre[u] < pre[v];
}

int main(){
    // head
    #ifdef FWL
    freopen("data.in","r",stdin);
    #endif // FWL
    ios::sync_with_stdio(false);
    cin.tie(0);
    //

    //init
    for(int i = 2; i < maxn; i++){
        if(i&(i-1))lg[i] = lg[i-1];
        else lg[i] = lg[i-1] + 1;
    }
    edge_cnt = 1;
    //

    //read & build tree
    cin >> n;
    for(int i = 1; i < n; i++){
        int u,v; ll d;
        cin >> u >> v >> d;
        e[++edge_cnt] = Edge(u,v,d,head[u]); head[u] = edge_cnt;
        e[++edge_cnt] = Edge(v,u,d,head[v]); head[v] = edge_cnt;
    }
    depth[1] = 0;
    dfs(1);
    //

    // solve
    cin >> m;
    cost[1] = 2e9; //第一个结点不可能切的掉的
    for(int i = 1; i <= m; i++){
        int Vn;
        cin >> Vn;
        for(int i = 1; i <= Vn; i++){
            cin >> p[i];
        }
        top = 0; // init stack
        for(int i = 1; i <= Vn; i++) k[ p[i] ] = true;
        sort(p+1, p+1+Vn, cmp);

        stak[++top] = 1; //根节点先入栈
        for(int i = 1; i <= Vn; i++){
            if(top <= 1){
                stak[++top] = p[i];
            }
            else{
                int g = lca(stak[top],p[i]);
                if(g == stak[top]){
                    stak[++top] = p[i];
                }
                else{
                    while(top > 1 && pre[stak[top-1]] >= pre[g]){
                        VG[stak[top-1]].push_back(stak[top]);
                        top--;
                    }
                    if(g != stak[top]){
                        VG[g].push_back(stak[top]);
                        stak[top] = g;
                    }
                    stak[++top] = p[i];
                }
            }
        }
        while(top > 1){
            VG[stak[top-1]].push_back(stak[top]);
            top--;
        }
        cout << solve(1) << '\n';

        //clear
        for(int i = 1; i <= Vn; i++) k[ p[i] ] = false;
        //
    }
    return 1; //Don't Cheat
}

猜你喜欢

转载自www.cnblogs.com/fanwl/p/10753829.html