标签:lap tor turn from ble show alt 实用 技术
树状数组 + 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; }
标签:lap tor turn from ble show alt 实用 技术
原文地址:https://www.cnblogs.com/CzxingcHen/p/9819619.html