刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)
题目:
Problem Description
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
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
A single integer, the maximum number of paths.
Sample Input
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
题解:
见: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序+树状数组)的更多相关文章
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
- HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
题目链接 题意: 有n个点的一棵树.其中树上有m条已知的链,每条链有一个权值.从中选出任意个不相交的链使得链的权值和最大. 思路: 树形DP.设dp[i]表示i的子树下的最优权值和,sum[i]表示不 ...
- hdu 3887 Counting Offspring dfs序+树状数组
Counting Offspring Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 5877 Weak Pair dfs序+树状数组+离散化
Weak Pair Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) Prob ...
- HDU 3887:Counting Offspring(DFS序+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=3887 题意:给出一个有根树,问对于每一个节点它的子树中有多少个节点的值是小于它的. 思路:这题和那道苹果树是一样 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 棋盘问题——POJ1321
棋盘问题——深度优先搜索 题目描述: 在一个给定形状的棋盘(形状可能是不规则的)上面摆放棋子,棋子没有区别.要求摆放时任意的两个棋子不能放在棋盘中的同一行或者同一列,请编程求解对于给定形状和大小的棋盘 ...
- mybatis(一):思维导图
- -安装与配置 FTP 服务器
我们经常会使用 FTP,把本地电脑上的文件上传到服务器上,或者把服务器上的文件下载到自己的电脑里面.FTP 有服务端和客户端,FTP 的服务端提供了这种传输文件的服务,FTP 的客户端提供了传输文件的 ...
- Leetcode 20 有效的括号valid-parentheses(栈)
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- LeetCode之Weekly Contest 101
前一段时间比较忙,而且做这个对于我来说挺耗时间的,已经间隔了几期的没做总结了,后面有机会补齐.而且本来做这个的目的就是为了防止长时间不做把编程拉下,不在追求独立作出所有题了.以后完赛后稍微尝试下,做不 ...
- charles抓手机包
charles抓手机包 如果是使用charles抓包.一定要tm的保证手机和电脑连的是一个网. 1.proxy setting,查看charles,端口 2.勾选 3.ipconfig,查 ...
- chrome浏览器跳过(忽略)所有的js断点
在调试程序时我们经常通过打断点的方式来跟踪代码的执行流程,所以可能会在很多时候打很多断点,当我们知道了程序大概的执行流程之后,这时候断点就不太需要了.但是我们又不想马上把所有的断点清除掉,因为我们打的 ...
- 使用Spring MVC后实现一个BaseController
使用Spring MVC技术后,可以实现一个基类的Controller类来分装一些MVC常用的方法,其他的Controller都继承自这个BaseController,这样在使用常用的方法时将会变得非 ...
- 1、python-初探
语言包括编译型语言和解释型语言编译型:全部翻译,再执行:c.c++解释型:边执行边翻译:python.php.java.c#.perl.ruby.javascript 一.系统位数32位系统内存的最大 ...
- python模块之datetime
相比于time模块,datetime模块的接口则更直观.更容易调用 datetime模块定义了下面这几个类: datetime.date:表示日期的类.常用的属性有year, month, day: ...