问题即:选择价值和最多的链,使得每个点最多被一条链覆盖。

那么考虑其对偶问题:选择最少的点(每个点可以重复选),使得每条链上选了至少$w_i$个点。

那么将链按照LCA的深度从大到小排序,每次若发现点数不够,则在LCA处补充点,树链剖分+线段树维护。

时间复杂度$O(m\log^2n)$。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100010,M=262150;
int Case,cas,n,m,q,i,op,x,y,z;
int g[N],v[N<<1],nxt[N<<1],ed;
int size[N],son[N],f[N],d[N],st[N],top[N],dfn;
int val[M];
int ans;
struct E{int x,y,z,w;}e[N];
inline bool cmp(const E&a,const E&b){return d[a.z]<d[b.z];}
inline void addedge(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;}
void dfs(int x){
size[x]=1;
for(int i=g[x];i;i=nxt[i])if(v[i]!=f[x]){
f[v[i]]=x,d[v[i]]=d[x]+1;
dfs(v[i]),size[x]+=size[v[i]];
if(size[v[i]]>size[son[x]])son[x]=v[i];
}
}
void dfs2(int x,int y){
st[x]=++dfn;top[x]=y;
if(son[x])dfs2(son[x],y);
for(int i=g[x];i;i=nxt[i])if(v[i]!=son[x]&&v[i]!=f[x])dfs2(v[i],v[i]);
}
void build(int x,int a,int b){
val[x]=0;
if(a==b)return;
int mid=(a+b)>>1;
build(x<<1,a,mid),build(x<<1|1,mid+1,b);
}
void change(int x,int a,int b,int c,int p){
val[x]+=p;
if(a==b)return;
int mid=(a+b)>>1;
if(c<=mid)change(x<<1,a,mid,c,p);
else change(x<<1|1,mid+1,b,c,p);
}
int ask(int x,int a,int b,int c,int d){
if(c<=a&&b<=d)return val[x];
int mid=(a+b)>>1,t=0;
if(c<=mid)t=ask(x<<1,a,mid,c,d);
if(d>mid)t+=ask(x<<1|1,mid+1,b,c,d);
return t;
}
inline int lca(int x,int y){
for(;top[x]!=top[y];x=f[top[x]])if(d[top[x]]<d[top[y]])swap(x,y);
return d[x]<d[y]?x:y;
}
inline int chain(int x,int y){
int t=0;
for(;top[x]!=top[y];x=f[top[x]]){
if(d[top[x]]<d[top[y]])swap(x,y);
t+=ask(1,1,n,st[top[x]],st[x]);
}
if(d[x]<d[y])swap(x,y);
t+=ask(1,1,n,st[y],st[x]);
return t;
}
inline void gao(int x,int y,int z,int w){
int t=chain(x,y);
if(t>=w)return;
w-=t;
ans+=w;
change(1,1,n,st[z],w);
}
int main(){
scanf("%d",&Case);
while(Case--){
scanf("%d%d",&n,&m);
for(i=1;i<n;i++){
scanf("%d%d",&x,&y);
addedge(x,y),addedge(y,x);
}
dfs(1);
dfs2(1,1);
build(1,1,n);
for(i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&e[i].w);
e[i].x=x,e[i].y=y;
e[i].z=lca(x,y);
}
sort(e+1,e+m+1,cmp);
for(i=m;i;i--)gao(e[i].x,e[i].y,e[i].z,e[i].w);
printf("%d\n",ans);
for(i=0;i<=n;i++)g[i]=size[i]=son[i]=f[i]=d[i]=st[i]=top[i]=0;
ed=dfn=ans=0;
}
}

  

HDU5293 : Tree chain problem的更多相关文章

  1. hdu5293 Tree chain problem 树形dp+线段树

    题目:pid=5293">http://acm.hdu.edu.cn/showproblem.php?pid=5293 在一棵树中,给出若干条链和链的权值.求选取不相交的链使得权值和最 ...

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

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

  3. 【HDU 5233】Tree chain problem (树形DP+树剖+线段树|树状数组)最大权不相交树链集

    [题目] Tree chain problem Problem Description Coco has a tree, whose vertices are conveniently labeled ...

  4. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  5. 刷题总结——Tree chain problem(HDU 5293 树形dp+dfs序+树状数组)

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

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

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

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

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

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

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

  9. codeforces 671D Roads in Yusland & hdu 5293 Tree chain problem

    dp dp优化 dfs序 线段树 算是一个套路.可以处理在树上取链的问题.

随机推荐

  1. ubuntu安装界面 会出现不完整情况

    解决方法: alt+鼠标左键或者win+鼠标左键拖动

  2. meaven

    一个项目管理工具.java语言编写的,所以可以跨平台 https://mvnrepository.com/

  3. UPC 6616 Small Mulitple

    D - Small Multiple 题目传送门 Time limit : 2sec / Memory limit : 256MB Score : 700 points Problem Stateme ...

  4. C#递归拷贝文件删除文件

    拷贝文件及子文件,最后一个参数排除,哪个不要删除.(其实就是移动的效果) //拷贝文件及子文件 public static void CopyDirectory(string src, string ...

  5. Linux使用退格键时出现^H ^?解决方法

    Linux使用退格键时出现^H ^?解决方法 在linux下执行脚本不注意输错内容需要删除时总是出现^H ^H不是H键的意思,是backspace.主要是当你的终端backspace有问题的时候才需要 ...

  6. 通过expdp和impdp将Oracle11g数据导入到Oracle10g中

    1 导出过程 1.1 查看目录: select * from dba_directories; 1.2 将目录的操作权限赋值给指定的用户(不执行次步骤可能会出现权限问题): grant read,wr ...

  7. webpack学习笔记--配置devServer

    devServer 1-6 使用DevServer 介绍过用来提高开发效率的 DevServer ,它提供了一些配置项可以改变 DevServer 的默认行为. 要配置 DevServer ,除了在配 ...

  8. Select2 多层次赋值时异步赋值的问题

    场景: 当选择人员时加载人员,选择部门时加载部门.所以在人员下,选择人员A后,如果选择部门,会触发二级select 重新获取数据. 问题: 使用select2()方法进行绑定远程数据后,对第二个sel ...

  9. 通过awk获取netstat命令中的进程号

    需要如下: 获取进程号

  10. Codeforces 1144G Two Merged Sequences dp

    Two Merged Sequences 感觉是个垃圾题啊, 为什么过的人这么少.. dp[ i ][ 0 ]表示处理完前 i 个, 第 i 个是递增序列序列里的元素,递减序列的最大值. dp[ i ...