HDU 5293 Tree chain problem
树状数组 + 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的更多相关文章
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。
Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- codeforces 671D Roads in Yusland & hdu 5293 Tree chain problem
dp dp优化 dfs序 线段树 算是一个套路.可以处理在树上取链的问题.
- HDU 5293 Tree chain problem 树形DP
题意: 给出一棵\(n\)个节点的树和\(m\)条链,每条链有一个权值. 从中选出若干条链,两两不相交,并且使得权值之和最大. 分析: 题解 #include <cstdio> #incl ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- HDU 5293 Train chain Problem - 树链剖分(树状数组) + 线段树+ 树型dp
传送门 题目大意: 一颗n个点的树,给出m条链,第i条链的权值是\(w_i\),可以选择若干条不相交的链,求最大权值和. 题目分析: 树型dp: dp[u][0]表示不经过u节点,其子树的最优值,dp ...
- 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集
[题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...
- hdu5293(2015多校1)--Tree chain problem(树状dp)
Tree chain problem Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
随机推荐
- UIScrollView的左右滑动和侧滑手势冲突的解决办法
转载自:https://blog.csdn.net/kst_123/article/details/77762811 当ViewController中添加了一个全屏的UIScrollView的时候,U ...
- 《笔者带你剖析Apache Commons DbUtils 1.6》(转)
前言 关于Apache的DbUtils中间件或许了解的人并不多,大部分开发人员在生成环境中更 多的是依靠Hibernate.Ibatis.Spring JDBC.JPA等大厂提供的持久层技术解决方案, ...
- 8 函数类型——《Swift3.0从入门到出家
Swift语言中每一个函数都有它特定的数据类型,称其为函数类型 函数类型和基本数据类型一样,可以定义变量或者常量,可以定义函数形参,也可以做为函数的返回值类型 函数类型的格式为:参数列表的数据类型—& ...
- JDBC预编译语句表名占位异常
有时候,我们有这样的需求,需要清空多个表的内容,这样我们有两种做法,可用delete from table 或 truncate table table,两种方法视情况而定,前者只是一条条的删除表数据 ...
- Proxmark3笔记(一)
Kali下使用Proxmark3 apt-get update apt-get install build-essential libreadline5 libreadline-dev libusb- ...
- 使用airmon-ng工具开启监听模式
使用ifconfig命令查看活动的网络接口 可以看出网卡已经激活了,然后将网卡设置为混杂模式 root@sch01ar:~# airmon-ng start wlan0 用ifconfig查看网卡是否 ...
- Python Twisted系列教程10:增强defer功能的客户端
作者:dave@http://krondo.com/an-introduction-to-asynchronous-programming-and-twisted/ 译者:杨晓伟(采用意译) 可以从这 ...
- pandas数据对齐
Pandas的对齐运算 是数据清洗的重要过程,可以按索引对齐进行运算,如果没对齐的位置则补NaN,最后也可以填充NaN Series的对齐运算 1. Series 按行.索引对齐 示例代码: s1 = ...
- MYCAT实战之分片迁移
实践扩容 1.要求: travelrecord 表定义为10个分片,尝试将10个分片中的 2 个分片转移到第二台MySQL上, 并完成记录要求,最快的数据迁移做法,中断业务时间最短 2.针对分片以及迁 ...
- vmware 仅主机模式 ip配置
首先关闭防火墙 主机(宿主机器 win7) 虚拟机(xp) 3..重要提示: 如果ping不通首先考虑防火墙的问题!!! vmware配置: nat模式下玩耍: 1. 配置nat的虚拟网卡: 2. ...