=-=抓住叶节点往上揪

Tree chain problem

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

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")

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e5+88;
int fa[maxn],dep[maxn],size[maxn],pos[maxn],bl[maxn],head[maxn];
int sum[maxn];
int dp[maxn],su[maxn],n,m;
vector<int>G[maxn];
struct node{
   int to,next;
}edge[maxn<<1];
struct cst{
   int x,y,z;
}road[maxn];
int tot,sz;
void init(){
   tot=sz=0;
   memset(sum,0,sizeof(sum));
   memset(head,-1,sizeof(head));
   for(int i=1;i<=n;++i) G[i].clear();
}
void add(int u,int v) {
   edge[tot].to=v;
   edge[tot].next=head[u];
   head[u]=tot++;
}
void sadd(int u,int val) {
   for( ; u<=n;u+=u&(-u))
    sum[u]+=val;
}
int getsum(int u) {
    int ret=0;
   for(;u;u-=u&(-u))
    ret+=sum[u];
   return ret;
}
void dfs1(int x){
    size[x]=1;
    for(int i=head[x];i+1;i=edge[i].next){
        int v=edge[i].to;
        if(v==fa[x]) continue;
        fa[v]=x;
        dep[v]=dep[x]+1;
        dfs1(v);
        size[v]+=size[x];
    }
}
void dfs2(int x,int chain)
{
    bl[x]=chain;
    pos[x]=++sz;
    int k=0;
    for(int i=head[x];i+1;i=edge[i].next){
        int v=edge[i].to;
        if(dep[v]>dep[x]&&size[v]>size[k])
            k=v;
    }
    if(!k) return;
    dfs2(k,chain);
    for(int i=head[x];i+1;i=edge[i].next)
        if(dep[edge[i].to]>dep[x]&&edge[i].to!=k)
        dfs2(edge[i].to,edge[i].to);
}
int LCA(int x,int y){
   while(bl[x]!=bl[y]) {
    if(dep[bl[x]]<dep[bl[y]]) swap(x,y);
    x=fa[bl[x]];
   }
   if(pos[x]>pos[y]) swap(x,y);
   return x;
}
int query(int x,int y){
    int ret=0;
   while(bl[x]!=bl[y]){
    if(dep[bl[x]]<dep[bl[y]]) swap(x,y);
    ret+=getsum(pos[x])-getsum(pos[bl[x]]-1);
    x=fa[bl[x]];
   }
   if(dep[x]>dep[y]) swap(x,y);
   ret+=getsum(pos[y])-getsum(pos[x]-1);
   return ret;
}
void solve(int u){
   su[u]=0;
   for(int i=head[u];i+1;i=edge[i].next){
    int v=edge[i].to;
    if(v==fa[u]) continue;
    solve(v);
    su[u]+=dp[v];
   }
   dp[u]=su[u];
   for(int i=0;i<(int)G[u].size();++i){
    int v=G[u][i];
    dp[u]=max(dp[u],query(road[v].x,road[v].y)+su[u]+road[v].z);
   }
   sadd(pos[u],su[u]-dp[u]);
}
int main(){
   int t,u,v;
   for(scanf("%d",&t);t--;){
    scanf("%d%d",&n,&m);
    init();
    for(int i=1;i<n;++i){
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    dfs1(1);
    dfs2(1,1);
    for(int i=1;i<=m;++i)
    {
        scanf("%d%d%d",&road[i].x,&road[i].y,&road[i].z);
        G[LCA(road[i].x,road[i].y)].push_back(i);
    }
    solve(1);
    printf("%d\n",dp[1]);
   }
}

HDU5293 树链剖分+树形DP的更多相关文章

  1. (中等) HDU 5293 Tree chain problem,树链剖分+树形DP。

    Problem Description   Coco has a tree, whose vertices are conveniently labeled by 1,2,…,n.There are ...

  2. Codeforces 856D - Masha and Cactus(树链剖分优化 dp)

    题面传送门 题意: 给你一棵 \(n\) 个顶点的树和 \(m\) 条带权值的附加边 你要选择一些附加边加入原树中使其成为一个仙人掌(每个点最多属于 \(1\) 个简单环) 求你选择的附加边权值之和的 ...

  3. [NOIP2018提高组] 保卫王国 (树链剖分+动态DP)

    题面 题目链接-Luogu 题目链接-Loj(要加Freopen) 题解 什么是动态DP? OneInDark:你不需要知道这么多,你只需要知道是利用了广义矩阵乘法就够了! 广义矩乘 广义矩阵乘法,简 ...

  4. BZOJ.4543.[POI2014]Hotel加强版(长链剖分 树形DP)

    题目链接 弱化版:https://www.cnblogs.com/SovietPower/p/8663817.html. 令\(f[x][i]\)表示\(x\)的子树中深度为\(i\)的点的个数,\( ...

  5. BZOJ4543[POI2014]Hotel加强版——长链剖分+树形DP

    题意参见BZOJ3522 n<=100000 数据范围增强了,显然之前的转移方程不行了,那么不妨换一种. 因为不能枚举根来换根DP,那么我们描述的DP方程每个点要计算三个点都在这个点的子树内的方 ...

  6. bzoj4543 [POI2014]Hotel加强版 长链剖分+树形DP

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4543 题解 这道题的弱化版 bzoj3522 [POI2014]Hotel 的做法有好几种吧. ...

  7. 【洛谷4719】 动态dp(树链剖分,dp,矩阵乘法)

    前言 其实我只是为了过掉模板而写的ddp,实际应用被吊着锤 Solution 并不想写详细的过程 一句话过程:将子树中轻儿子的贡献挂到这个点上面来 详细版:(引用yyb) 总结一下的话,大致的过程是这 ...

  8. 【BZOJ4712】洪水 树链剖分优化DP+线段树

    [BZOJ4712]洪水 Description 小A走到一个山脚下,准备给自己造一个小屋.这时候,小A的朋友(op,又叫管理员)打开了创造模式,然后飞到山顶放了格水.于是小A面前出现了一个瀑布.作为 ...

  9. CF1111E Tree 树链剖分,DP

    CF1111E Tree 过年了,洛咕还没爬这次的题,先放个CF的链接吧. 补个LG传送门. 对于每个询问点\(x\),设它的祖先即不能和它放在同一个集合中的点的个数为\(f[x]\),设\(dp[i ...

随机推荐

  1. Linux 软链接和硬链接

    系统链接文件 文件有文件名和数据,在Linux上被分成两个部分:用户数据(user data)与元数据(metadata) 用户数据:文件数据块(data block),数据块是记录文件真实内容的地方 ...

  2. CSS开发技巧(三):图片点击缩放

    前言  利用CSS实现图片的点击缩放是一个很值得研究的效果.在某些业务需求场景中,我们可能并没有足够的空间展示过大的图片,这就需要限制图片容器的宽度和高度.然而图片限制了宽度,一些图片的细节便又无法看 ...

  3. javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake

    2019独角兽企业重金招聘Python工程师标准>>> 问题 前两天一个学弟在群里面问一个问题: 请问一下用阿里云服务器发送https请求为什么会失败,是需要有些其他什么配置吗? 同 ...

  4. Codeforce 263D Cycle in Graph 搜索 图论 哈密尔顿环

    You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph inde ...

  5. python selenium(常用关键字)

    1.文本按钮操作相关: send_keys()输入文本 from selenium import webdriver import time dr = webdriver.Chrome() dr.ge ...

  6. spark系列-7、spark调优

    官网说明:http://spark.apache.org/docs/2.1.1/tuning.html#data-serialization 一.JVM调优 1.1.Java虚拟机垃圾回收调优的背景 ...

  7. 从一条数据说起——InnoDB存储数据结构

    本篇博客参考掘金小册--MySQL 是怎样运行的:从根儿上理解 MySQL 先给大家讲一个故事,我刚参加工作,在一个小作坊里面当[码畜](尽管现在也是),有一天老板从我背后走过,说了一句举世震惊的话: ...

  8. C语言程序设计实验报告三

    C程序设计实验报告 姓 名:张美盛 实验地点:家 实验时间:2020年3月29日 实验项目:4.3.1 If语句的应用 4.3.2 switch-case的应用 4.3.3 switch-case嵌套 ...

  9. (Lineup the Dominoes筛子)三维状压

    传送门 描述:\(一堆筛子,每个筛子两个面,上面有1-6之间的数字.后一个筛子与前一个筛子的接触面的点数必须相等.\) \(求,有多少种方案堆完筛子.(方案只关心筛子的位置,不关心是否翻转)\) \( ...

  10. E. Reachability from the Capital dfs暴力

    E. Reachability from the Capital 这个题目就是给你一个有向图,给你起点,问增加多少条边让这个图变成一个连通图. 这个因为n只有5000m只有5000 所以可以暴力枚举这 ...