题意:

给出一棵\(n\)个节点的树和\(m\)条链,每条链有一个权值。

从中选出若干条链,两两不相交,并且使得权值之和最大。

分析:

题解

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include <string>
using namespace std;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define PER(i, a, b) for(int i = b - 1; i >= a; i--)
#define SZ(a) ((int)a.size())
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(a) a.begin(), a.end()
typedef long long LL;
typedef pair<int, int> PII; const int maxn = 100000 + 10; int n, m;
vector<int> G[maxn], Q[maxn];
int u[maxn], v[maxn], w[maxn];
int l[maxn], r[maxn], dfs_clock;
int dp[maxn], sum[maxn]; int dep[maxn];
int anc[maxn][20]; void dfs(int u, int fa) {
l[u] = ++dfs_clock;
anc[u][0] = fa;
for(int i = 0; anc[u][i]; i++)
anc[u][i+1] = anc[anc[u][i]][i];
dep[u] = dep[fa] + 1;
for(int v : G[u]) if(v != fa) {
dfs(v, u);
}
r[u] = ++dfs_clock;
} int LCA(int u, int v) {
if(dep[u] < dep[v]) swap(u, v);
PER(i, 0, 20)
if(dep[anc[u][i]] >= dep[v]) u = anc[u][i];
if(u == v) return u;
PER(i, 0, 20) if(anc[u][i] != anc[v][i])
u = anc[u][i], v = anc[v][i];
return anc[u][0];
} int C[maxn << 1];
#define lowbit(x) (x&(-x))
void add(int x, int v) {
while(x <= n * 2) {
C[x] += v;
x += lowbit(x);
}
}
int query(int x) {
int ans = 0;
while(x) {
ans += C[x];
x -= lowbit(x);
}
return ans;
} void init() {
REP(i, 1, n + 1) G[i].clear(), Q[i].clear();
memset(anc, 0, sizeof(anc));
dfs_clock = 0;
memset(C, 0, sizeof(C));
memset(dp, 0, sizeof(dp));
memset(sum, 0, sizeof(sum));
} void upd(int& a, int b) { if(a < b) a = b; } void solve(int x) {
for(int y : G[x]) if(y != anc[x][0]) {
solve(y);
sum[x] += dp[y];
}
dp[x] = sum[x];
for(int q : Q[x]) {
upd(dp[x], sum[x] + query(l[u[q]]) + query(l[v[q]]) + w[q]);
}
add(l[x], sum[x] - dp[x]);
add(r[x], dp[x] - sum[x]);
} int main() {
int T; scanf("%d", &T);
while(T--) {
scanf("%d%d", &n, &m);
init();
REP(i, 1, n) {
int u, v; scanf("%d%d", &u, &v);
G[u].PB(v);
G[v].PB(u);
}
dfs(1, 0);
REP(i, 0, m) {
scanf("%d%d%d", u + i, v + i, w + i);
int lca = LCA(u[i], v[i]);
Q[lca].PB(i);
}
solve(1);
printf("%d\n", dp[1]);
} return 0;
}

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

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

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

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

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

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

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

  4. hdu5293 Tree chain problem 树形dp+线段树

    题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...

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

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

  6. HDU 5293 Tree chain problem

    树状数组 + dp 设$f_i$表示以$i$为根的子树中的能选取的最大和,$sum_x$表示$\sum_{f_y}$  ($y$是$x$的一个儿子),这样子我们把所有给出的链按照两点的$lca$分组, ...

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

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

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

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

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

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

随机推荐

  1. Bootstrap开发

    1.BootStrap开发工具 任意前端工具 专门Bootstrap工具:Jetstrap(下载地址:jetstrap.com) 2.官网: www.bootcss.com(“下载Bootstrap” ...

  2. HCNA配置telnet远程管理

    1.拓扑图 说明:通过配置最终能通过R5 用telnet协议登陆到R4上并将R4改名为R44 2.R4配置 <Huawei>sys Enter system view, return us ...

  3. C语言总结的知识点

    什么是有效数字? ------------------------- 数据类型 运算符: 函数: 程序结构: 存储结构 内存管理 关键字: ------------------------- C语言: ...

  4. MySQL入门很简单: 4 创建 修改删除表

    1. 创建表的方法 1)创建表的语法形式 首先,选择数据库: USE 数据库名: 创建表: CREATE TABLE 表名 (属性名 数据类型 [完整性约束条件], 属性名 数据类型 [完整性约束条件 ...

  5. IOS SQLite函数总结

    SQL语句的种类 ●  数据定义语句(DDL:Data Definition Language) ●  包括create和drop等操作 ●  在数据库中创建新表或删除表(create table或 ...

  6. IOS 封装View的fram(X Y W H )

    @interface UIView (Extension) @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) ...

  7. 使用selenium grid的hub做分发,且可查看分发后的服务器IP地址

    背景:借助selenium 的grid做分布式运行,进行分发任务,(目前不做多浏览器的操作,只对谷歌浏览器进行操作) 目前在A服务器(http://10.40.6.24:4444)上注册了一个hub, ...

  8. poj 2112 Optimal Milking 奶牛

    题目链接:http://poj.org/problem?id=2112 题意:K个产奶机,C头奶牛,每个产奶机最多可供M头奶牛使用:并告诉了产奶机.奶牛之间的两两距离Dij(0<=i,j< ...

  9. 我对XCode Objective

    我对XCode Objective-c Cocoa的简单理解 Xcode Xcode说的通俗一点就是开发OS X 和 iOS 应用程序的. 如果我们想要认真点说 ,Xcode 是运行在操作系统Mac ...

  10. CDH4.5.0下安装lzo

    参考 http://www.cloudera.com/content/cloudera-content/cloudera-docs/Impala/1.0.1/Installing-and-Using- ...