簡介
虛樹,顧名思義就是不真實的樹。
它往往出現在一類樹形動態規划問題中。
換句話說,虛樹實際就是為了解決一類樹形動態規划問題而誕生的!
我們從一道經典的虛樹題目入手
[SDOI2011]消耗戰
鏈接:https://www.luogu.org/problemnew/show/P2495
題目大意
給出一棵樹,每條邊有邊權。
有$m$次詢問,每次詢問給出$k$個點,問使得這$k$個點均不與$1$號點(根節點)相連的最小代價
$n<=250000, m>=1,\sum k <= 500000$(m是smg..)
暴力dp
首先考慮$m=1$,也就是只有一次詢問的情況。我們考慮暴力dp
設$f[x]$為處理完以$x$為根的樹的最小代價
轉移分為兩種情況
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$
那么現在會有兩種情況
- $lca$是$x$,直接將$p$入棧。
- $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; }