树状数组 + 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. iTunes文件共享

    在Info.plist文件中添加UIFileSharingEnabled键,并将键值设置为YES.将您希望共享的文件放在应用程序的Documents目录.

  2. php单链表实现

    php单链表实现 <?php //单链表 class Hero{ public $no; public $name; public $nickname; public $next=null; f ...

  3. java中二维数组求最大值代码。。。。。。不懂呀

    总结:二维数组理解不了,,,,求解析... package com.c2; public class Aaa { // 求数组元素中最大的 public static void main(String ...

  4. CGI/MIME/servlet术语解释

    CGI→一种协议, 一种标准, 一种规范 使用CGI协议, 能够让用户访问某些动态资源的时候, 触发web服务器, 让web服务器根据CGI协议能够调用外部(web服务器外部)的程序来执行处理这个动态 ...

  5. 获取Request.Form所有内容

    string wwww = "";        for (int i = 0; i < Request.Form.Count; i++)        {          ...

  6. 什么是闭包?在js中的作用是什么?

    闭包就是讲函数内部生成的变量保存到内存中,进行下次调用:也可以说函数外不可以调用函数内部的变量: 当函数内部返回一个函数时,闭包搭建了方法内部与方法外部的桥梁,使得外部也可以任意的获取到方法内部的资源 ...

  7. Venom的简单使用

    工具地址:https://github.com/r00t-3xp10it/venom 打开到venom目录,输入./venom.sh 打开程序 按回车键继续 这里有很多的模块,要用哪个模块就输入它的编 ...

  8. 10.solr学习速成之高亮显示

    Solr高亮显示的三种实现 高亮显示在搜索中使用的比较多,比较常用的有三种使用方式,如果要对某field做高亮显示,必须对该field设置stored=true .          第一种是普通的高 ...

  9. Flask之单元测试

    5.2单元测试 为什么要测试? Web程序开发过程一般包括以下几个阶段:[需求分析,设计阶段,实现阶段,测试阶段].其中测试阶段通过人工或自动来运行测试某个系统的功能.目的是检验其是否满足需求,并得出 ...

  10. app端内容播放,web端的玩具,app通过websocket远程遥控玩具播放内容,玩具管理页面

    一.app端内容播放 下载代码 https://github.com/987334176/Intelligent_toy/archive/v1.0.zip 注意:由于涉及到版权问题,此附件没有图片和音 ...