HDU 5293 Tree chain problem 树形DP
题意:
给出一棵\(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的更多相关文章
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。
Problem Description Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...
- hdu5293 Tree chain problem 树形dp+线段树
题目:pid=5293">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 设$f_i$表示以$i$为根的子树中的能选取的最大和,$sum_x$表示$\sum_{f_y}$ ($y$是$x$的一个儿子),这样子我们把所有给出的链按照两点的$lca$分组, ...
- 树形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 ...
随机推荐
- 在Sql Server中使用Guid类型的列及设置Guid类型的默认值
1.列的类型为uniqueidentifier 2.列的默认值可以设为newid()
- 如何启用SAP C4C OData Event Notification
当我们在试图使用SAP C4C OData事件通知这个功能时,如果遇到下列提示消息,说明这个功能在business configuration里没有开启: The OData Event Notifi ...
- ABI and ISA
ABI定义了如何使用ISA. ISA定义了机器码的使用规则. http://www.delorie.com/gnu/docs/gmp/gmp_6.html ABI and ISA ABI (Appli ...
- cocos2d-x推断sprite点击
我们经常须要推断用户的点击操作是否落于某个sprite之上,进而让这个sprite做出响应. 可是假设我们通过继承CCSprite类来实现自己的Sprite类的时候,产生的视图尺寸会充满屏幕.多个Sp ...
- ORA-01262,oracle启动报错,及Oracle启动原理
错误状态: SQL> startup ORA-01261: Parameter db_recovery_file_dest destination string cannot be transl ...
- UIView的层次调整,及子view布局模式自动布局模式(停靠模式)
UIView*view1=[[UIView alloc]initWithFrame:CGRectMake(10,30,300,30)]; view1.backgroundColor=[UIColor ...
- ffmpeg处理RTMP流媒体的命令大全
最近浏览国外网站时候发现,翻译不准确的敬请谅解. 1.将文件当做直播送至live ffmpeg -re -i localFile.mp4 -c copy -f flv rtmp://server/li ...
- 2018.8.18 servlet使用的会话跟踪除session外还有哪些方式
解释HTTP HTTP是一种无连接的协议,如果一个客户端只是单纯地请求一个文件(HTML或GIF),服务器端可以响应给客户端,并不需要知道一连串的请求是否来自于相同的客户端,而且也不需要担心客户端是否 ...
- C#中datatable的用法/传数据
在开发中,我们常用到表类型数据,不同于string,int....那么datatable类型如何定义呢,具体怎么使用呢,代码如下: namespace Common.Table { using Sys ...
- JDK1.8简单配置环境变量---两步曲
鄙人最近重新装完系统之后,在安装和配置jdk1.8的时候,发现网上许多教程配置jdk环境变量时都还在沿用传统的方式配置,但是随着技术的更新,完全没有必要那么麻烦了. 下载和安装jdk的教程,在这里就不 ...