HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)

时间:2022-09-10 16:15:28

Tree chain problem

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1798    Accepted Submission(s): 585

Problem Description

Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.
There are m chain on the tree, Each chain has a certain weight. Coco would like to pick out some chains any two of which do not share common vertices.
Find out the maximum sum of the weight Coco can pick
 

Input

The input consists of several test cases. The first line of input gives the number of test cases T (T<=10).
For each tests: 
First line two positive integers n, m.(1<=n,m<=100000)
The following (n - 1) lines contain 2 integers ai bi denoting an edge between vertices ai and bi (1≤ai,bi≤n),
Next m lines each three numbers u, v and val(1≤u,v≤n,0<val<1000), represent the two end points and the weight of a tree chain.
 

Output

For each tests:
A single integer, the maximum number of paths.
 

Sample Input

1
7 3
1 2
1 3
2 4
2 5
3 6
3 7
2 3 4
4 5 3
6 7 3
 

Sample Output

6

Hint

Stack expansion program: #pragma comment(linker, "/STACK:1024000000,1024000000")

 

Author

FZUACM
 

Source

对于每条链u,v,w,我们只在lca(u,v)的顶点上处理它

让dp[i]表示以i为根的子树的最大值,sum[i]表示dp[vi]的和(vi为i的儿子们)

则i点有两种决策,一种是不选以i为lca的链,则dp[i]=sum[i]。

另一种是选一条以i为lca的链,那么有转移方程:dp[i]=sigma(dp[vj])+sigma(sum[kj])+w。(sigma表示累加,vj表示那些不在链上的孩子们,kj表示在链上的孩子们)

为了便于计算,我们处理出dp[i]=sum[i]-sigma(dp[k]-sum[k])+w=sum[i]+sigma(sum[k]-dp[k])+w。

利用dfs序和树状数组可以logn算出sigma(sum[k]-dp[k])。

 //2017-09-13
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const int N = ;
const int LOG_N = ; int head[N], tot;
struct Edge{
int v, next;
}edge[N<<]; void add_edge(int u, int v){
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
} int in[N], out[N], idx, depth[N], father[N][LOG_N];
void dfs(int u, int fa){
in[u] = ++idx;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
if(v == fa)continue;
depth[v] = depth[u]+;
father[v][]= u;
for(int j = ; j < LOG_N; j++)
father[v][j] = father[father[v][j-]][j-];
dfs(v, u);
}
out[u] = ++idx;
} int tree[N]; int lowbit(int x){
return x&(-x);
} void add(int pos, int val){
for(int i = pos; i <= N; i+=lowbit(i))
tree[i] += val;
} int query(int l){
int sum = ;
for(int i = l; i > ; i-=lowbit(i))
sum += tree[i];
return sum;
} int lca(int u, int v){
if(depth[u] < depth[v])
swap(u, v);
for(int i = LOG_N-; i >= ; i--){
if(depth[father[u][i]] >= depth[v])
u = father[u][i];
}
if(u == v)return u;
for(int i = LOG_N-; i >= ; i--){
if(father[u][i] != father[v][i]){
u = father[u][i];
v = father[v][i];
}
}
return father[u][];
}
struct Chain{
int u, v, w;
}chain[N];
vector<int> vec[N]; int dp[N], sum[N];
void solve(int u, int fa){
dp[u] = sum[u] = ;
for(int i = head[u]; i != -; i = edge[i].next){
int v = edge[i].v;
if(v == fa)continue;
solve(v, u);
sum[u] += dp[v];
}
dp[u] = sum[u];
for(auto &pos: vec[u]){
int a = chain[pos].u;
int b = chain[pos].v;
int c = chain[pos].w;
dp[u] = max(dp[u], sum[u]+query(in[a])+query(in[b])+c);
}
add(in[u], sum[u]-dp[u]);
add(out[u], dp[u]-sum[u]);
} int T, n, m;
void init(){
tot = ;
idx = ;
depth[] = ;
for(int i = ; i <= n; i++)
vec[i].clear();
memset(head, -, sizeof(head));
memset(dp, , sizeof());
memset(sum, , sizeof());
memset(tree, , sizeof(tree));
} int main()
{
freopen("inputB.txt", "r", stdin);
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
init();
int u, v;
for(int i = ; i < n-; i++){
scanf("%d%d", &u, &v);
add_edge(u, v);
add_edge(v, u);
}
dfs(, );
for(int i = ; i < m; i++){
scanf("%d%d%d", &chain[i].u, &chain[i].v, &chain[i].w);
vec[lca(chain[i].u, chain[i].v)].push_back(i);
}
solve(, );
printf("%d\n", dp[]);
} return ;
}

HDU5293(SummerTrainingDay13-B Tree DP + 树状数组 + dfs序)的更多相关文章

  1. 【BZOJ】2434&colon; &lbrack;Noi2011&rsqb;阿狸的打字机 AC自动机&plus;树状数组&plus;DFS序

    [题意]阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的: l 输入小写 ...

  2. codeforces 570 D&period; Tree Requests 树状数组&plus;dfs搜索序

    链接:http://codeforces.com/problemset/problem/570/D D. Tree Requests time limit per test 2 seconds mem ...

  3. E - Apple Tree&lpar;树状数组&plus;DFS序)

    There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. ...

  4. POJ 3321 Apple Tree &lpar;树状数组&plus;dfs序&rpar;

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  5. 【BZOJ-3881】Divljak AC自动机fail树 &plus; 树链剖分&plus; 树状数组 &plus; DFS序

    3881: [Coci2015]Divljak Time Limit: 20 Sec  Memory Limit: 768 MBSubmit: 508  Solved: 158[Submit][Sta ...

  6. 【BZOJ-1103】大都市meg 树状数组 &plus; DFS序

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2009  Solved: 1056[Submit][Sta ...

  7. &lbrack;luogu P3787&rsqb;&lbrack;新创无际夏日公开赛&rsqb; 冰精冻西瓜 &lbrack;树状数组&rsqb;&lbrack;dfs序&rsqb;

    题目背景 盛夏,冰之妖精琪露诺发现了一大片西瓜地,终于可以吃到美味的冻西瓜啦. 题目描述 琪露诺是拥有操纵冷气程度的能力的妖精,一天她发现了一片西瓜地.这里有n个西瓜,由n-1条西瓜蔓连接,形成一个有 ...

  8. BZOJ 2434&colon; &lbrack;Noi2011&rsqb;阿狸的打字机 &lbrack;AC自动机 Fail树 树状数组 DFS序&rsqb;

    2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 2545  Solved: 1419[Submit][Sta ...

  9. BZOJ 1103 &lbrack;POI2007&rsqb;大都市meg(树状数组&plus;dfs序)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1103 [题目大意] 给出一棵树,每条边的经过代价为1,现在告诉你有些路不需要代价了, ...

随机推荐

  1. Atitit 图像处理知识点 &&num;160&semi;知识体系 知识图谱

    Atitit 图像处理知识点  知识体系 知识图谱 图像处理知识点 图像处理知识点体系 v2 qb24.xlsx 基本知识图像金字塔op膨胀叠加混合变暗识别与检测分类肤色检测other验证码生成 基本 ...

  2. Androd Studio layout页面布局无法预览

    Could not initialize class android.support.v7.internal.widget.ActionBarOverlayLayout 导致无法看到布局页面,解决方法 ...

  3. C&num;线程模型脉络

    今天在看同事新买到的<C#本质论 Edition 4>的时候,对比下以前Edtion3的新特性时针对Async/Await关键字时发现对一些线程方面的定义还理解的不是很透彻,脉络还不是很清 ...

  4. Angularjs总结(八)&dollar; cookie和&dollar;rootscope

    AngularJS 提供了很好的 $cookie 和 $cookieStore API 用来处理 cookies .这两个服务都能够很好的发挥HTML5 cookies,当HTML5 API可用时浏览 ...

  5. WebForm页面间传值方法(转)

    Asp.NET WEB FORMS 给开发者提供了极好的事件驱动开发模式.Asp .NET为我们提供了三种方式,一种是可以通过用QueryString来传送相应的值,再一种是通过session变量来传 ...

  6. 20165235 实现pwd功能

    20165235 实现pwd功能 要求 学习pwd命令 2.研究pwd实现需要的系统调用(man -k; grep),写出伪代码 3.实现mypwd 测试mypwd 实现过程 pwd是将当前的文件目录 ...

  7. 详解Python的装饰器

    Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...

  8. react&plus;es6&plus;webpack环境搭建以及项目入门

    前言:拖了这么久,小菜鸟终于开始正式应用react,和es6来开发项目了.之前超喜欢同学的一个博客风格,这里贴一下地址:https://iwenku.net/,PC端是他很久之前做的,最近他重新做了一 ...

  9. java Servlet生成随机验证码

    import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; i ...

  10. RPM 包的构建 - SPEC 基础知识

    spec 文件 制作 rpm 软件包并不是一件复杂的工作,其中的关键在于编写软件包的 spec 描述文件. 要想制作一个 rpm 软件包就必须写一个软件包描述文件 spec.这个文件中包含了软件包的诸 ...