题目:

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

题解:

见:http://blog.csdn.net/cdsszjj/article/details/78249687

很好的一道树形dp题··感觉以后要是考到关于根节点到其所在子树中某一点所形成的链的相关题都可以这样考虑·····

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int N=2e5+;
struct node
{
int x,y,val;
}line[N];
vector<int>root[N];
inline int R()
{
char c;int f=;
for(c=getchar();c<''||c>'';c=getchar());
for(;c<=''&&c>='';c=getchar()) f=(f<<)+(f<<)+c-'';
return f;
}
int T,fst[N],go[N],nxt[N],tot,n,m;
int tree[N],g[N][],deep[N],f[N],sum[N],l[N],r[N],cnt;
inline void comb(int a,int b)
{
nxt[++tot]=fst[a],fst[a]=tot,go[tot]=b;
nxt[++tot]=fst[b],fst[b]=tot,go[tot]=a;
}
inline void dfs(int u,int fa)
{
l[u]=++cnt;
for(int e=fst[u];e;e=nxt[e])
{
int v=go[e];if(v==fa) continue;
deep[v]=deep[u]+,g[v][]=u;dfs(v,u);
}
r[u]=cnt+;
}
inline int get(int a,int b)
{
int i,j;
if(deep[a]<deep[b]) swap(a,b);
for(i=;(<<i)<=deep[a];i++);i--;
for(j=i;j>=;j--)
if(deep[a]-(<<j)>=deep[b]) a=g[a][j];
if(a==b) return a;
for(i=;i>=;i--)
if(g[a][i]!=g[b][i]) a=g[a][i],b=g[b][i];
return g[a][];
}
inline int query(int pos)
{
int temp=;
for(int i=pos;i;i-=(i&(-i))) temp+=tree[i];
return temp;
}
inline void insert(int pos,int x)
{
for(int i=pos;i<=n+;i+=(i&(-i))) tree[i]+=x;
}
inline void dp(int u,int fa)
{
sum[u]=,f[u]=;
for(int e=fst[u];e;e=nxt[e])
{
int v=go[e];
if(v==fa) continue;
dp(v,u);sum[u]+=f[v];
}
f[u]=sum[u];
for(int i=;i<root[u].size();i++)
{
node temp=line[root[u][i]];
int a=temp.x,b=temp.y,c=temp.val;
f[u]=max(f[u],sum[u]+query(l[a])+query(l[b])+c);
}
insert(l[u],sum[u]-f[u]);
insert(r[u],f[u]-sum[u]);
}
inline void pre()
{
memset(fst,,sizeof(fst));tot=,cnt=;
memset(g,,sizeof(g));memset(tree,,sizeof(tree));
for(int i=;i<=n;i++) root[i].clear();
}
int main()
{
//freopen("a.in","r",stdin);
T=R();
while(T--)
{
n=R(),m=R();int a,b,c;
pre();
for(int i=;i<n;i++) a=R(),b=R(),comb(a,b);
dfs(,);
for(int i=;i<=;i++)
for(int j=;j<=n;j++) g[j][i]=g[g[j][i-]][i-];
for(int i=;i<=m;i++)
{
a=R(),b=R(),c=R();
int lca=get(a,b);
root[lca].push_back(i);
line[i].x=a,line[i].y=b,line[i].val=c;
}
dp(,);
cout<<f[]<<endl;
}
return ;
}
 

刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)的更多相关文章

  1. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

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

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

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

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

  4. hdu 3887 Counting Offspring dfs序+树状数组

    Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. hdu 5877 Weak Pair dfs序+树状数组+离散化

    Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Prob ...

  6. HDU 3887:Counting Offspring(DFS序+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...

  7. POJ 3321:Apple Tree + HDU 3887:Counting Offspring(DFS序+树状数组)

    http://poj.org/problem?id=3321 http://acm.hdu.edu.cn/showproblem.php?pid=3887 POJ 3321: 题意:给出一棵根节点为1 ...

  8. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+ 树状数组或线段树

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

  9. Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组

    C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...

随机推荐

  1. codeforces Gym 100286H Hell on the Markets

    紫书上面的题,队友做的,WA了freopen..爆了int... UVA 1614 - Hell on the Markets 奇怪的股市(贪心,结论)

  2. IEDA的安装与破解

    百度搜索IDEA 进入官网下载 https://www.jetbrains.com/idea/download/#section=windows 2.下载好双击即可 3. 完成破解

  3. 在Linux下安装redis

    http://www.cnblogs.com/xiaohongxin/p/6854095.html 追加: 通过配置文件启动最好先./redis.cli shutdown ,再到当前目录在./redi ...

  4. 使用mfc CHtmlView内存泄露解决方法

    第一步,谷歌有文章说CHtmlView部分api使用BSTR没释放: 解决方法是重写一下接口: CString GetFullName() const; CString GetFullName() c ...

  5. cocos2d-x的基本动作2

    1.基本动作 Cocos2d提供的基本动作:瞬时动作.延时动作.运作速度. 瞬时动作:就是不需要时间,马上就完成的动作.瞬时动作的共同基类是 InstantAction. Cocos2d提供以下瞬时动 ...

  6. VB6 代码编辑页面添加支持滚轮模式

    VB6 中的代码编辑页面默认是不支持滚轮模式的,这让在编辑代码时的体验很是不爽. 但在64位win10系统进行加载配置时,可能会出现问题,可用如下方法解决: http://download.micro ...

  7. Linux系统修改网卡名(eth0-3)

    一.命名规则策略 规则1: 对于板载设备命名合并固件或 BIOS 提供的索引号,如果来自固件或 BIOS 的信息可读就命名,比如eno1,这种命名是比较常见的,否则使用规则2. 规则2: 命名合并固件 ...

  8. 传智 Python基础班+就业班+课件 【最新完整无加密视频课程】

    点击了解更多Python课程>>> 传智 Python基础班+就业班+课件 [最新完整无加密视频课程] 直接课程目录 python基础 linux操作系统基础) 1-Linux以及命 ...

  9. 《Java并发编程实战》读书笔记一 -- 简介

    <Java并发编程实战>读书笔记一 -- 简介 并发的历史 并发的历史,也是人类利用有限的资源去提高生产效率的一个的例子. 设想现在有台计算机,这台计算机具有以下的资源: 单核CPU一个 ...

  10. Install GStreamer on Ubuntu

    apt-get install libgstreamer1.0-0 gstreamer1.0-plugins-base gstreamer1.0-plugins-good gstreamer1.0-p ...