虛樹入門

簡介

虛樹,顧名思義就是不真實的樹。spa

它每每出如今一類樹形動態規劃問題中。3d

換句話說,虛樹實際就是爲了解決一類樹形動態規劃問題而誕生的!code

咱們從一道經典的虛樹題目入手blog

[SDOI2011]消耗戰

連接:https://www.luogu.org/problemnew/show/P2495排序

題目大意

給出一棵樹,每條邊有邊權。get

有$m$次詢問,每次詢問給出$k$個點,問使得這$k$個點均不與$1$號點(根節點)相連的最小代價string

$n<=250000, m>=1,\sum k <= 500000$(m是smg..)it

暴力dp

首先考慮$m=1$,也就是隻有一次詢問的狀況。咱們考慮暴力dpio

設$f[x]$爲處理完以$x$爲根的樹的最小代價class

轉移分爲兩種狀況

1.斷開本身與父親的聯繫,代價爲從根到該節點的最小值

2.不考慮該節點(前提是該節點不是詢問點),把子樹內的全部詢問點都斷開的代價

可是這樣的複雜度是$O(nm)$的,顯然沒法AC

然而咱們發現$\sum k$是比較小的,咱們可不能夠對$k$下手呢?

因而,虛樹誕生了

虛樹

思想

虛樹的主要思想是:對於一棵樹,僅僅保留有用的點,從新構建一棵樹

這裏有用的點指的是詢問點和它們的lca

煮個栗子

好比這樣的一棵樹(沒錯就是樣例)

對於樣例中的三次詢問,

3
2 10 6
4 5 7 8 3
3 9 4 6

那麼它的虛樹分別長這樣

          

 

第二張比較鬼畜,由於在這道題中咱們必需要斷開$5$號點,所以$7,8$號點用不到

 

構建

考慮獲得了詢問點,如何構造出一棵虛樹。

首先咱們要先對整棵樹dfs一遍,求出他們的dfs序,而後對每一個節點以dfs序爲關鍵字從小到大排序

同時維護一個棧,表示從根到棧頂元素這條鏈

假設當前要加入的節點爲$p$,棧頂元素爲$x = s[top]$,$lca$爲他們的最近公共祖先

由於咱們是按照dfs序遍歷,所以$lca$不多是$p$

那麼如今會有兩種狀況

  1. $lca$是$x$,直接將$p$入棧。
  2. $x,p$分別位於$lca$的兩棵子樹中,此時$x$這棵子樹已經遍歷完畢,(若是沒有,即x的子樹中還有一個未加入的點y,可是dfn[y]<dfn[p],即應先訪問y), 咱們須要對其進行構建

 

設棧頂元素爲$x$,第二個元素爲$y$

 

  • 若$dfn[y]>dfn[lca]$,能夠連邊$y->x$,將$x$出棧;
  • 若$dfn[y]=dfn[lca]$,即$y=lca$,連邊$lca->x$,此時子樹構建完畢(break);
  • 若$dfn[y]<dfn[lca]$,即$lca$在$y,x$之間,連邊$lca->x$,$x$出棧,再將$lca$入棧。此時子樹構建完畢(break)。


此處較爲抽象,建議你們畫圖理解一下

不斷重複這個過程,虛樹就構建完成了,另外咱們須要維護出鏈上的最小值,而後咱們直接在虛樹上dp就能夠了

 

複雜度

虛樹上除了要加入的詢問點外,還有可能出現的$LCA$。

假設當前要加入$x$,它與以前加入的y產生一個新的$LCA$記做$lca1$,還與以前的$z$產生一個新的$LCA$記做$lca2$。

設$dep[lca1]<dep[lca2]$(不然交換$y,z$便可)

那麼$LCA(y,z)=lca1$,因此假設不成立。

即每次加入點,最多會產生一個新的$LCA$。

那麼虛樹中的點數是$O(2*k)$的。

這樣複雜度就只與k有關,$O(2*\sum k_i)$。

 

代碼

// luogu-judger-enable-o2
// luogu-judger-enable-o2
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
#define LL long long
char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
using namespace std;
const int MAXN = 250001;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
char obuf[1 << 24], *O=obuf;
void print(LL x) {
    if(x > 9) print(x / 10);
    *O++= x % 10 + '0';
}
int N, M;
struct Edge {
    int u, v, w, nxt;
}E[MAXN << 1];
int head[MAXN], num = 1;
inline void AddEdge(int x, int y, int z) {
    E[num] = (Edge) {x, y, z, head[x]};
    head[x] = num++;
}
vector<int> v[MAXN];
void add_edge(int x, int y) {
    v[x].push_back(y);
}
int a[MAXN], dfn[MAXN], topf[MAXN], siz[MAXN], son[MAXN], s[MAXN], top, deep[MAXN], fa[MAXN], ID = 0;
LL mn[MAXN];
void dfs1(int x, int _fa) {
    siz[x] = 1; fa[x] = _fa;
    for(int i = head[x]; i != -1; i = E[i].nxt) {
        if(E[i].v == _fa) continue;
        deep[E[i].v] = deep[x] + 1;
        mn[E[i].v] = min(mn[x], (LL)E[i].w);
        dfs1(E[i].v, x);
        siz[x] += siz[E[i].v];
        if(siz[E[i].v] > siz[son[x]]) son[x] = E[i].v;
    }
}
void dfs2(int x, int topfa) {
    topf[x] = topfa;
    dfn[x] = ++ID;
    if(!son[x]) return ;
    dfs2(son[x], topfa);
    for(int i = head[x]; i != -1; i = E[i].nxt) 
        if(!topf[E[i].v]) 
            dfs2(E[i].v, E[i].v);
}
int LCA(int x, int y) {
    while(topf[x] != topf[y]) {
        if(deep[topf[x]] < deep[topf[y]]) swap(x, y);
        x = fa[topf[x]];
    }
    if(deep[x] < deep[y]) swap(x, y);
    return y;
}
void insert(int x) {
    if(top == 1) {s[++top] = x; return ;}
    int lca = LCA(x, s[top]);
    if(lca == s[top]) return ;
    while(top > 1 && dfn[s[top - 1]] >= dfn[lca]) add_edge(s[top - 1], s[top]), top--;
    if(lca != s[top]) add_edge(lca, s[top]), s[top] = lca;//
    s[++top] = x;
}
LL DP(int x) {
    if(v[x].size() == 0) return mn[x];
    LL sum = 0;
    for(int i = 0; i < v[x].size(); i++) 
        sum += DP(v[x][i]);
    v[x].clear();
    return min(sum, (LL)mn[x]);
}
int comp(const int &a, const int &b) {
    return dfn[a] < dfn[b];
}
int main() {
    memset(head, -1, sizeof(head));
    //memset(mn, 0xff, sizeof(mn));
    mn[1] = 1ll << 60;
    N = read();
    for(int i = 1; i <= N - 1; i++) {
        int x = read(), y = read(), z = read();
        AddEdge(x, y, z); AddEdge(y, x, z);
    }
    deep[1] = 1;
    dfs1(1, 0);
    dfs2(1, 1);
    M = read();
    /*for(int i = 1; i <= N; i++)    
        for(int j = 1; j <= N; j++)
            printf("%d %d %d\n", i, j, LCA(i, j));*/
    //for(int i = 1; i <= N; i++) printf("%d ", mn[i]); puts("");
    while(M--) {
        int K = read();
        for(int i = 1; i <= K; i++) a[i] = read();
        sort(a + 1, a + K + 1, comp);
        s[top = 1] = 1;
        for(int i = 1; i <= K; i++) insert(a[i]);
        while(top > 0)  add_edge(s[top - 1], s[top]), top--;
        print(DP(1)), *O++ = '\n'; 
    }
    fwrite(obuf, O-obuf, 1 , stdout);    
    return 0;
}
相關文章
相關標籤/搜索