题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5293

被这题打蹦了,看着题解写的,很是爆炸,确实想不到,我用的DFS序+LCA+树形DP,当然也可以写树剖,不过这里DFS序更简单,因为都是对点到根的操作

 #include<cstdio>
#include<vector>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
#define F(i,a,b) for(int i=a;i<=b;i++)
#define root 1,n,1
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
using namespace std; const int N=1e5+,DEG=;
int t,n,m,x,y,dp[N],sum[N],c1[N*],c2[N*];
struct edge{int u,v,w,lca;}p[N];
vector<int>vec[N];
inline void add(int x,int k,int *c){for(;x<=n*;x+=x&-x)c[x]+=k;}
inline int ask(int x,int *c){int an=;while(x)an+=c[x],x-=x&-x;return an;}
//LCA+dfs序
int ed,g[N],nxt[*N],v[*N],fa[N][DEG],dep[N],idx,l[*N],r[*N];
inline void adg(int x,int y){v[++ed]=y,nxt[ed]=g[x],g[x]=ed;} void LCA_dfs(int u,int pre){
dep[u]=dep[pre]+,fa[u][]=pre,l[u]=++idx;
F(i,,DEG-)fa[u][i]=fa[fa[u][i-]][i-];
for(int i=g[u];~i;i=nxt[i])if(v[i]!=pre)LCA_dfs(v[i],u);
r[u]=++idx;
} int LCA(int a,int b){
if(dep[a]>dep[b])a^=b,b^=a,a^=b;
if(dep[a]<dep[b])F(i,,DEG-)if((dep[b]-dep[a])&(<<i))b=fa[b][i];
if(a!=b)for(int i=DEG-;i<?a=fa[a][]:,i>=;i--)
if(fa[a][i]!=fa[b][i])a=fa[a][i],b=fa[b][i];
return a;
} void fuck(int s,int fa){
dp[s]=sum[s]=;
for(int i=g[s];~i;i=nxt[i])if(v[i]!=fa)fuck(v[i],s),sum[s]+=dp[v[i]];
dp[s]=sum[s];
for(int i=;i<vec[s].size();i++){
int uu=p[vec[s][i]].u,vv=p[vec[s][i]].v;
int tmp=ask(l[uu],c1)+ask(l[vv],c1)-ask(l[uu],c2)-ask(l[vv],c2)+sum[s];
dp[s]=max(dp[s],tmp+p[vec[s][i]].w);
}
add(l[s],sum[s],c1),add(r[s],-sum[s],c1);
add(l[s],dp[s],c2),add(r[s],-dp[s],c2);
}
void init(){
F(i,,n)g[i]=-,vec[i].clear();ed=idx=;
F(i,,n*)c1[i]=c2[i]=;
} int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
init();
F(i,,n-)scanf("%d%d",&x,&y),adg(x,y),adg(y,x);
LCA_dfs(,);
F(i,,m){
scanf("%d%d%d",&p[i].u,&p[i].v,&p[i].w);
p[i].lca=LCA(p[i].u,p[i].v);
vec[p[i].lca].push_back(i);
}
fuck(,);
printf("%d\n",dp[]);
}
return ;
}

hdu_5293_Tree chain problem(DFS序+树形DP+LCA)的更多相关文章

  1. [JSOI2016]最佳团体 DFS序/树形DP

    题目 洛谷 P4322 [JSOI2016]最佳团体 Description 茜茜的舞蹈团队一共有\(N\)名候选人,这些候选人从\(1\)到\(N\)编号.方便起见,茜茜的编号是\(0\)号.每个候 ...

  2. 【bzoj4182】Shopping 树的点分治+dfs序+背包dp

    题目描述 给出一棵 $n$ 个点的树,每个点有物品重量 $w$ .体积 $c$ 和数目 $d$ .要求选出一个连通子图,使得总体积不超过背包容量 $m$ ,且总重量最大.求这个最大总重量. 输入 输入 ...

  3. dfs序+RMQ求LCA详解

    首先安利自己倍增求LCA的博客,前置(算不上)知识在此. LCA有3种求法:倍增求lca(上面qwq),树链剖分求lca(什么时候会了树链剖分再说.),还有,标题. 是的你也来和我一起学习这个了qwq ...

  4. HDU 5296 Annoying problem dfs序 lca

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

  5. hdu5293(2015多校1)--Tree chain problem(树状dp)

    Tree chain problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Other ...

  6. poj3417 Network 树形Dp+LCA

    题意:给定一棵n个节点的树,然后在给定m条边,去掉m条边中的一条和原树中的一条边,使得树至少分为两部分,问有多少种方案. 神题,一点也想不到做法, 首先要分析出加入一条边之后会形成环,形成环的话,如果 ...

  7. HDU 1520 Anniversary party(DFS或树形DP)

    Problem Description There is going to be a party to celebrate the 80-th Anniversary of the Ural Stat ...

  8. Codeforces 916E(思维+dfs序+线段树+LCA)

    题面 传送门 题目大意:给定初始根节点为1的树,有3种操作 1.把根节点更换为r 2.将包含u,v的节点的最小子树(即lca(u,v)的子树)所有节点的值+x 3.查询v及其子树的值之和 分析 看到批 ...

  9. 【二叉树-最长路径系列(任意路径)】直径、最长同值路径、 最大路径和(DFS、树形DP)

    总述 这类题目都是求一个最长路径,这个路径可以不经过根节点. 使用dfs(即递归地遍历树)的方法.维护一个全局最长路径max作为最终结果,而递归方法dfs返回的是含根节点的最长路径.(若不使用全局变量 ...

随机推荐

  1. 共享AFHTTPSessionManager 单例好处浅析

      很多时候,AFNetworking都是目前iOS开发者网络库中的不二选择.Github上2W+的star数足见其流行程度.而从iOS7.0开始,苹果推出了新的网络库继承者NSURLSession后 ...

  2. php 常用的调试方法

    file_put_contents("c:\1.log","输出字符串",FILE_APPEND);第三个参数是防止前面的内容被覆盖 error_log(pri ...

  3. 使用minidom来处理XML的示例(Python 学习)(转载)

    作者网站:http://www.donews.net/limodou/archive/2004/07/15/43609.aspx  一.XML的读取.在 NewEdit 中有代码片段的功能,代码片段分 ...

  4. Acer VN7 Win10小键盘修改

    由于 Home End 正常位置太远, NumberLock 容易误按, 故设置win10 键位映射如下图

  5. 修改虚拟机内容导致oracle不能启动

    虚拟机内存目前设置为4G,想要改变成2G,数据库启动时导致报targetmomory错误,解决办法如下: 1.查看分配的memory_target和memory_max_target大小 SQL> ...

  6. String类之endsWith方法--->检测该字符串以xx为结尾

    endsWith(XX)方法是java内置类String类的一个内置方法,我们直接拿来用即可了,下边是api说明:检测该字符串以xx为结尾,结果返回布尔值 public class Demo { pu ...

  7. display:inline-block和float:left的选择

    参考文章: http://www.zhangxinxu.com/wordpress/2010/11/%E6%8B%9C%E6%8B%9C%E4%BA%86%E6%B5%AE%E5%8A%A8%E5%B ...

  8. bootstrap复习:组件

    一.下拉菜单 1.实例:将下拉菜单触发器和下拉菜单都包裹在 .dropdown 里,或者另一个声明了 position: relative; 的元素.然后加入组成菜单的 HTML 代码.为下拉菜单的父 ...

  9. GlusterFS无法启动原因及处理方案

    启动结果: Redirecting to /bin/systemctl status  glusterd.serviceglusterd.service - GlusterFS, a clustere ...

  10. tomcat 配置SSL

    本教程使用 JDK 6 和 Tomcat 7,其他版本类似. 基本步骤: 使用 java 创建一个 keystore 文件 配置 Tomcat 以使用该 keystore 文件 测试 配置应用以便使用 ...