1. 程式人生 > >HDU 5293 Tree chain problem

HDU 5293 Tree chain problem

ear struct || log ont mem fine 一個點 fat

樹狀數組 + dp

設$f_i$表示以$i$為根的子樹中的能選取的最大和,$sum_x$表示$\sum_{f_y}$ ($y$是$x$的一個兒子),這樣子我們把所有給出的鏈按照兩點的$lca$分組,對於每一個點$x$,$sum_x$顯然是一個$f_x$的一個備選答案,而當有樹鏈的$lca$正好是$x$時,我們發現$sum_x + w + \sum_{sum_t} - \sum_{f_t}$($w$代表這條樹鏈能產生的價值,$t$是樹鏈上的一個點)。

那麽我們只要能快速計算出這兩個$\sum$就可以轉移了,其實用兩個樹狀數組維護$dfs$序即可。

具體可以參照代碼。

時間復雜度$O(Tnlogn)$。

Code:

技術分享圖片
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

const int N = 1e5 + 5;
const int Lg = 20;

int testCase, n, m, f[N], sum[N], dfsc, in[N], out[N];
int tot, head[N], fa[N][Lg], dep[N];
vector <int> vec[N];

struct Edge {
    
int to, nxt; } e[N << 1]; inline void add(int from, int to) { e[++tot].to = to; e[tot].nxt = head[from]; head[from] = tot; } struct Item { int u, v, lca, val; } a[N]; inline void read(int &X) { X = 0; char ch = 0; int op = 1; for(; ch > 9 || ch <
0; ch = getchar()) if(ch == -) op = -1; for(; ch >= 0 && ch <= 9; ch = getchar()) X = (X << 3) + (X << 1) + ch - 48; X *= op; } struct Bit { int s[N << 1]; #define lowbit(p) (p & (-p)) inline void clear() { memset(s, 0, sizeof(s)); } inline void modify(int p, int val) { for(; p <= 2 * n; p += lowbit(p)) s[p] += val; } inline int query(int p) { int res = 0; for(; p > 0; p -= lowbit(p)) res += s[p]; return res; } } bit1, bit2; inline void swap(int &x, int &y) { int t = x; x = y; y = t; } inline void chkMax(int &x, int y) { if(y > x) x = y; } void dfs(int x, int fat, int depth) { fa[x][0] = fat, dep[x] = depth; in[x] = ++dfsc; for(int i = 1; i <= 18; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; for(int i = head[x]; i; i = e[i].nxt) { int y = e[i].to; if(y == fat) continue; dfs(y, x, depth + 1); } out[x] = ++dfsc; } inline int getLca(int x, int y) { if(dep[x] < dep[y]) swap(x, y); for(int i = 18; i >= 0; i--) if(dep[fa[x][i]] >= dep[y]) x = fa[x][i]; if(x == y) return x; for(int i = 18; i >= 0; i--) if(fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; return fa[x][0]; } void solve(int x, int fat) { for(int i = head[x]; i; i = e[i].nxt) { int y = e[i].to; if(y == fat) continue; solve(y, x); sum[x] += f[y]; } chkMax(f[x], sum[x]); for(unsigned int i = 0; i < vec[x].size(); i++) { int id = vec[x][i]; int now = bit1.query(in[a[id].v]) + bit1.query(in[a[id].u]) - bit2.query(in[a[id].u]) - bit2.query(in[a[id].v]) + sum[x]; chkMax(f[x], now + a[id].val); } bit1.modify(in[x], sum[x]), bit1.modify(out[x], -sum[x]); bit2.modify(in[x], f[x]), bit2.modify(out[x], -f[x]); } int main() { for(read(testCase); testCase--; ) { tot = 0; memset(head, 0, sizeof(head)); read(n), read(m); for(int x, y, i = 1; i < n; i++) { read(x), read(y); add(x, y), add(y, x); } dfsc = 0; dfs(1, 0, 1); for(int i = 1; i <= n; i++) vec[i].clear(); for(int i = 1; i <= m; i++) { read(a[i].u), read(a[i].v), read(a[i].val); a[i].lca = getLca(a[i].u, a[i].v); vec[a[i].lca].push_back(i); } memset(f, 0, sizeof(f)); memset(sum, 0, sizeof(sum)); bit1.clear(), bit2.clear(); solve(1, 0); printf("%d\n", f[1]); } return 0; }
View Code

HDU 5293 Tree chain problem