codesforces 671D Roads in Yusland
Mayor of Yusland just won the lottery and decided to spent money on something good for town. For example, repair all the roads in the town.
Yusland consists of n intersections connected by n - 1 bidirectional roads. One can travel from any intersection to any other intersection using only these roads.
There is only one road repairing company in town, named "RC company". Company's center is located at the intersection 1. RC company doesn't repair roads you tell them. Instead, they have workers at some intersections, who can repair only some specific paths. The i-th worker can be paid ci coins and then he repairs all roads on a path from ui to some vi that lies on the path from ui to intersection 1.
Mayor asks you to choose the cheapest way to hire some subset of workers in order to repair all the roads in Yusland. It's allowed that some roads will be repaired more than once.
If it's impossible to repair all roads print - 1.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 300 000) — the number of cities in Yusland and the number of workers respectively.
Then follow n−1 line, each of them contains two integers xi and yi (1 ≤ xi, yi ≤ n) — indices of intersections connected by the i-th road.
Last m lines provide the description of workers, each line containing three integers ui, vi and ci (1 ≤ ui, vi ≤ n, 1 ≤ ci ≤ 109). This means that the i-th worker can repair all roads on the path from vi to ui for ci coins. It's guaranteed that vi lies on the path from ui to 1. Note that vi and ui may coincide.
If it's impossible to repair all roads then print - 1. Otherwise print a single integer — minimum cost required to repair all roads using "RC company" workers.
6 5
1 2
1 3
3 4
4 5
4 6
2 1 2
3 1 4
4 1 3
5 3 1
6 3 2
8
In the first sample, we should choose workers with indices 1, 3, 4 and 5, some roads will be repaired more than once but it is OK. The cost will be equal to 2 + 3 + 1 + 2 = 8 coins.
设f[i]为控制了i点的子树的费用
显然f是存在后效性的,因为当前选的工人会影响祖先的状态
最暴力的方法f[x][i]表示工人最高控制到x,控制了i的子树,不过显然超时
想办法消除后效性
我们考虑把一些工人的属性叠加到一个工人上,那么我们最后查询时只要查询某些能维修完整棵树的最小值
因为我们已经的到了这几棵子树的最优答案,并且子树之间互不影响,那么我们考虑把控制其他子树费用叠加到一个子树工人上
假设儿子有p1,p2,p3,tot=∑f[p]
p1的儿子的工人加上tot-f[p1],其他同理
也就是说,每一个工人都有控制其他所有子树的花费
也就是说,祖先x要用到p儿子这个工人时,就可以直接算出控制p儿子及x的费用
而且没有后效性,因为工人存了包含此工人的最优解,而且工人一直到vi被消掉之前,
都可以覆盖当前点,所以把工人都计算,肯定能算出最优解
因为工人在到vi之前都需要
而且对于计算一个节点的f,我们要给开始的工人赋值,给结束的工人消值,且给子节点所有工人加数
所以我们用dfs序维护以工人为下标的线段树,这样给子节点的工人加数等于线段树的区间加法
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
struct Node
{
int next,to;
}edge[],edgeS[],edgeT[];
int num,numS,numT,head[],headS[],headT[];
int L[],R[],cnt,dfn[],n,m;
ll c[],lazy[],val[],inf=2e15,f[];
void add(int u,int v)
{
num++;
edge[num].next=head[u];
head[u]=num;
edge[num].to=v;
}
void addS(int u,int v)
{
numS++;
edgeS[numS].next=headS[u];
headS[u]=numS;
edgeS[numS].to=v;
}
void addT(int u,int v)
{
numT++;而且没有后效性,因为
edgeT[numT].next=headT[u];
headT[u]=numT;
edgeT[numT].to=v;
}
void pushup(int rt)
{
c[rt]=min(c[rt*],c[rt*+]);
}
void pushdown(int rt)
{
if (lazy[rt])
{
c[rt*]+=lazy[rt];
c[rt*+]+=lazy[rt];
lazy[rt*]+=lazy[rt];
lazy[rt*+]+=lazy[rt];
lazy[rt]=;
}
}
void build(int rt,int l,int r)
{
c[rt]=inf;
if (l==r)
return;
int mid=(l+r)/;
build(rt*,l,mid);
build(rt*+,mid+,r);
}
void prework(int x,int pa)
{int i;
L[x]=cnt+;
for (i=headS[x];i;i=edgeS[i].next)
{
int v=edgeS[i].to;
dfn[v]=++cnt;
}
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v!=pa)
prework(v,x);
}
R[x]=cnt;
}
void update(int rt,int l,int r,int x,ll d)
{
if (l==r)
{
c[rt]=min(inf,d);
return;
}
pushdown(rt);
int mid=(l+r)/;
if (x<=mid) update(rt*,l,mid,x,d);
else update(rt*+,mid+,r,x,d);
pushup(rt);
}
void change(int rt,int l,int r,int L,int R,ll d)
{
if (L>R) return;
if (l>=L&&r<=R)
{
c[rt]+=d;
c[rt]=min(c[rt],inf);
lazy[rt]+=d;
lazy[rt]=min(lazy[rt],inf);
return;
}
pushdown(rt);
int mid=(l+r)/;
if (L<=mid) change(rt*,l,mid,L,R,d);
if (R>mid) change(rt*+,mid+,r,L,R,d);
pushup(rt);
}
ll query(int rt,int l,int r,int L,int R)
{
if (L>R) return inf;
if (l>=L&&r<=R)
{
return c[rt];
}
pushdown(rt);
int mid=(l+r)/;
ll s=inf;
if (L<=mid) s=min(s,query(rt*,l,mid,L,R));
if (R>mid) s=min(s,query(rt*+,mid+,r,L,R));
pushup(rt);
return s;
}
void dfs(int x,int pa)
{int i;
ll tot=;
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa) continue;
dfs(v,x);
tot+=f[v];
tot=min(tot,inf);
}
if (x==)
{
f[]=tot;
return;
}
for (i=headS[x];i;i=edgeS[i].next)
{
int v=edgeS[i].to;
update(,,m,dfn[v],tot+val[v]);
}
for (i=headT[x];i;i=edgeT[i].next)
{
int v=edgeT[i].to;
update(,,m,dfn[v],inf);
}
for (i=head[x];i;i=edge[i].next)
{
int v=edge[i].to;
if (v==pa) continue;
change(,,m,L[v],R[v],tot-f[v]);
}
f[x]=query(,,m,L[x],R[x]);
}
int main()
{int i,u,v;
cin>>n>>m;
for (i=;i<=n-;i++)
{
scanf("%d%d",&u,&v);
add(u,v);add(v,u);
}
for (i=;i<=m;i++)
{
scanf("%d%d%I64d",&u,&v,&val[i]);
addS(u,i);addT(v,i);
}
prework(,);
build(,,m);
dfs(,);
if (f[]!=inf) cout<<f[];
else cout<<-;
}
codesforces 671D Roads in Yusland的更多相关文章
- Codeforces 671D Roads in Yusland [树形DP,线段树合并]
洛谷 Codeforces 这是一个非正解,被正解暴踩,但它还是过了. 思路 首先很容易想到DP. 设\(dp_{x,i}\)表示\(x\)子树全部被覆盖,而且向上恰好延伸到\(dep=i\)的位置, ...
- codeforces 671D Roads in Yusland & hdu 5293 Tree chain problem
dp dp优化 dfs序 线段树 算是一个套路.可以处理在树上取链的问题.
- Codeforces 671D. Roads in Yusland(树形DP+线段树)
调了半天居然还能是线段树写错了,药丸 这题大概是类似一个树形DP的东西.设$dp[i]$为修完i这棵子树的最小代价,假设当前点为$x$,但是转移的时候我们不知道子节点到底有没有一条越过$x$的路.如果 ...
- CF 671D Roads in Yusland
弄完之后点进去一看,竟然是div1的D题……最近真是天天被题虐哭 推荐这一篇博客 https://www.cnblogs.com/Sakits/p/8085598.html 感觉讲清楚了,也是基本照着 ...
- Codeforces 671 D. Roads in Yusland
题目描述 Mayor of Yusland just won the lottery and decided to spent money on something good for town. Fo ...
- 【CF671D】Roads in Yusland(贪心,左偏树)
[CF671D]Roads in Yusland(贪心,左偏树) 题面 洛谷 CF 题解 无解的情况随便怎么搞搞提前处理掉. 通过严密(大雾)地推导后,发现问题可以转化成这个问题: 给定一棵树,每条边 ...
- [Codeforces671D]Roads in Yusland
[Codeforces671D]Roads in Yusland Tags:题解 题意 luogu 给定以1为根的一棵树,有\(m\)条直上直下的有代价的链,求选一些链把所有边覆盖的最小代价.若无解输 ...
- 【CF617D】Roads in Yusland
[CF617D]Roads in Yusland 题面 蒯的洛谷的 题解 我们现在已经转化好了题目了,戳这里 那么我们考虑怎么求这个东西,我们先判断一下是否所有的边都能被覆盖,不行的话输出\(-1\) ...
- 【CodeForces】671 D. Roads in Yusland
[题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...
随机推荐
- caffe使用ctrl-c不能保存模型
caffe使用Ctrl-c 不能保存模型: 是因为使用的是 tee输出日志 解决方法:kill -s SIGINT <proc_id> 或者使用 GLOG_log_dir=/path/to ...
- nyoj 邮票分你一半
邮票分你一半 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 小珂最近收集了些邮票,他想把其中的一些给他的好朋友小明.每张邮票上都有分值,他们想把这些邮票分 ...
- c语言中宏定义和常量定义的区别
他们有共同的好处就是"一改全改,避免输入错误"哪两者有不同之处吗?有的. 主要区别就在于,宏定义是在编译之前进行的,而const是在编译阶段处理的 宏定义不占用内存单元而const ...
- Hazelcast分布式
一般的应用正式环境中都不止一台服务器(也就是说是集群的),那么如果只是简单的将数据预加载到内存,那么就会有数据不同步的现象. (更新了其中一台JVM,另一台JVM并不会收到通知从而保持数据同步). 这 ...
- DSkin 的WebUI开发模式介绍,Html快速开发Winform的UI
新版WebUI开发模式采用MiniBlink内核,这个内核功能更完善,dll压缩之后才5M,而且提供开发者功能,内核还在更新中,而且是开源项目:https://github.com/weolar/mi ...
- confluence搭建详情
Confluence安装&破解&汉化 编辑时间: 2017年7月7日18:01:13 1.介绍 Atlassian Confluence(简称Confluence)是一个专业的wiki ...
- 非PE病毒介绍
1.宏病毒 1.1 介绍 本文中的宏特制office系列办公软件中的宏,Microsoft Office中对宏的定义为"宏就是能够组织在一起的,可以作为一个独立命令来执行的一系列Word 命 ...
- Angular 学习笔记 ( CDK - Accessibility )
@angular/ckd 是 ng 对于 ui 组建的基础架构. 是由 material 团队开发与维护的, 之所以会有 cdk 看样子是因为在开发 material 的时候随便抽象一个层次出来给大家 ...
- 新概念英语(1-31)Where's Sally?
新概念英语(1-31)Where's Sally? Is the cat climbing the tree ? A:Where is Sally, Jack ? B:She is in the ga ...
- eclipse开发Groovy代码,与java集成,maven打包编译
今天尝试了一下在eclipse里面写Groovy代码,并且做到和Java代码相互调用,折腾了一下把过程记录下来. 首先需要给eclipse安装一下Groovy的插件,插件地址:https://gith ...