树状数组 + 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 + ;
const int Lg = ; 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 << ]; 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 = ; char ch = ; int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} struct Bit {
int s[N << ]; #define lowbit(p) (p & (-p)) inline void clear() {
memset(s, , sizeof(s));
} inline void modify(int p, int val) {
for(; p <= * n; p += lowbit(p))
s[p] += val;
} inline int query(int p) {
int res = ;
for(; p > ; 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][] = fat, dep[x] = depth;
in[x] = ++dfsc;
for(int i = ; i <= ; i++)
fa[x][i] = fa[fa[x][i - ]][i - ]; for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs(y, x, depth + );
}
out[x] = ++dfsc;
} inline int getLca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = ; i >= ; i--)
if(dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if(x == y) return x;
for(int i = ; i >= ; i--)
if(fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][];
} 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 = ; 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 = ; memset(head, , sizeof(head));
read(n), read(m);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
} dfsc = ; dfs(, , ); for(int i = ; i <= n; i++) vec[i].clear();
for(int i = ; 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, , sizeof(f));
memset(sum, , sizeof(sum));
bit1.clear(), bit2.clear();
solve(, ); printf("%d\n", f[]);
} return ;
}

HDU 5293 Tree chain problem的更多相关文章

  1. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  2. (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。

    Problem Description   Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...

  3. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

  4. codeforces 671D Roads in Yusland & hdu 5293 Tree chain problem

    dp dp优化 dfs序 线段树 算是一个套路.可以处理在树上取链的问题.

  5. HDU 5293 Tree chain problem 树形DP

    题意: 给出一棵\(n\)个节点的树和\(m\)条链,每条链有一个权值. 从中选出若干条链,两两不相交,并且使得权值之和最大. 分析: 题解 #include <cstdio> #incl ...

  6. 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)

    题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...

  7. HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp

    传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...

  8. 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集

    [题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...

  9. hdu5293(2015多校1)--Tree chain problem(树状dp)

    Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

随机推荐

  1. PHP使用curl请求https站点的常见错误及解决方案

    使用curl请求http站点和https站点最大的不同就是https站点有证书验证这一环节,如果证书验证不通过则无法发起请求,不管是请求什么类型的站点遇到问题时先把错误码打印出来看一下,打印错误码的代 ...

  2. 谨慎安装Python3.7.0,SSL低版本导致Pip无法使用

    最新新配置了一台服务器.安装 的时候直接使用了最新的Python 3.7最新版本. 安装成功,编译成功.但是用pip 安装包的时候提示:pip is configured with locations ...

  3. 设置ubantu的软件源地址

    查看所用的源 $ sudo vim /etc/apt/sources.list 由于安装的Ubuntu Server 16.04.1 LTS是英文版的,软件源就默认都是 us.archive.ubun ...

  4. apt安装工具

    apt-cache search package 搜索包 apt-cache show package 获取包的相关信息,如说明.大小.版本等 sudo apt-get install package ...

  5. 在Activity中使用Menu

    1.右击res-->New-->Directory输入文件名menu,在此文件夹下新建main菜单文件:右击menu-->New-->Menu resource file 2. ...

  6. 在mac上 使用jenkins 执行python文件

    1.要选择 [执行 shell]构建

  7. 杂项:UN-标准通用置标语言

    ylbtech-杂项:标准通用置标语言 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   7.返回顶部   8.返回顶部   9.返回顶部 ...

  8. 1117 Eddington Number

    题意:给出了N个数字,确定一个尽可能大的数字E,要求这N个数字中大于E的数字有E个. 思路: 乍一看不知道题目在说啥.静下心来多读几遍题目,在草稿纸上比划比划,发现是个大水题.解释一下样例,原始序列为 ...

  9. python3+ros+telnet+telnetlib

    利用python3的telnetlib模块 远程登录ros,输入帐号密码,然后执行命令,并导出结果到txt文本: 不过实际操作这种方式不行,因为telnet导出来的文本文件,带颜色编码,根本无法看哦. ...

  10. python写一个能生成三种一句话木马的脚本

    代码: import time import os from threading import Thread import optparse def aspyijuhua(): try: juy=op ...