Codeforces 671 D. 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.
题目大意:
每一条边都需要被修建一次,雇佣第\(i\)个工人需要花 \(ci\) 的钱,能够帮你修建 \(ui\),\(vi\),之间的所有道路,并且保证 \(vi\) 是,\(ui\) 到根的路径上的点
解题报告:
用时:2h,6MLE
分析这题,叶子节点必须选,然后对于没有被覆盖的也必须从子树中选,但是需要决策,所以只能DP,考虑状态:
很容易想到最暴力的DP,定义 \(f[x][j]\),表示 \(x\) 子树内的边都被修过,且已选工人最远能覆盖到的深度为 \(j\),转移非常简单,但是复杂度不对,考虑消除第二维:不会.....
容易想到树链剖分,即把当前节点能更新到的父节点做一遍链剖,更新线段树
显然不行,因为不能保证其他子树都被覆盖
参考题解:
思想非常巧妙,大概是把其他子树中的DP值累加到某一棵子树上,这样就巧妙的使得线段树中的DP值都是合法的了,为了消除在 \(vi\) 结束的影响,我们在 \(ui\) 和 \(vi\) 上分别挂链,然后dfs时对应线段树中添加删除即可
注意实现时可以考虑不参考我的方法,其实不一定只对关键点建立线段树,不然细节很多
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=300005;const ll inf=2e15;
int head[N],nxt[N<<2],to[N<<2],num=0,n,m,val[N],dfn[N],DFN=0,L[N],R[N],s[N],t[N];
void link(int x,int y,int *h){nxt[++num]=h[x];to[num]=y;h[x]=num;}
void priwork(int x,int last){
L[x]=DFN+1;
for(int i=s[x];i;i=nxt[i])dfn[to[i]]=++DFN;
for(int i=head[x];i;i=nxt[i])
if(to[i]!=last)priwork(to[i],x);
R[x]=DFN;
}
ll tree[N<<2],mark[N<<2],f[N];
#define ls (node<<1)
#define rs (node<<1|1)
void build(int l,int r,int node){
tree[node]=inf;if(l==r)return ;
int mid=(l+r)>>1;
build(l,mid,ls);build(mid+1,r,rs);
tree[node]=Min(tree[ls],tree[rs]);
}
il void updata(int l,int r,int node,int sa,int se,ll to){
if(sa>se)return ;
if(l>se || r<sa)return ;
if(sa<=l && r<=se){
tree[node]=Min(tree[node]+to,inf);
mark[node]=Min(mark[node]+to,inf);
return ;
}
int mid=(l+r)>>1;
updata(l,mid,ls,sa,se,to);updata(mid+1,r,rs,sa,se,to);
tree[node]=Min(tree[ls],tree[rs])+mark[node];
}
il void Modify(int l,int r,int node,int sa,ll to){
if(l==r){tree[node]=Min(to,inf);return ;}
int mid=(l+r)>>1;
if(sa<=mid)Modify(l,mid,ls,sa,to);
else Modify(mid+1,r,rs,sa,to);
tree[node]=Min(tree[ls],tree[rs])+mark[node];
}
il ll query(int l,int r,int node,int sa,int se){
if(sa>se)return inf;
if(l==sa && r==se)return tree[node];
int mid=(l+r)>>1;
if(sa>mid)return query(mid+1,r,rs,sa,se)+mark[node];
else if(se<=mid)return query(l,mid,ls,sa,se)+mark[node];
return min(query(l,mid,ls,sa,mid),query(mid+1,r,rs,mid+1,se))
+mark[node];
}
il void dfs(int x,int last){
int u;ll tot=0;
for(int i=head[x];i;i=nxt[i]){
u=to[i];if(to[i]==last)continue;
dfs(u,x);
tot+=f[u];tot=Min(tot,inf);
}
if(x==1){f[x]=tot;return ;}
for(int i=s[x];i;i=nxt[i])Modify(1,m,1,dfn[to[i]],tot+val[to[i]]);
for(int i=t[x];i;i=nxt[i])Modify(1,m,1,dfn[to[i]],inf);
for(int i=head[x];i;i=nxt[i]){
u=to[i];if(u==last)continue;
updata(1,m,1,L[u],R[u],tot-f[u]);
}
f[x]=query(1,m,1,L[x],R[x]);
}
void work()
{
int x,y;
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
scanf("%d%d",&x,&y);
link(x,y,head);link(y,x,head);
}
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&val[i]);
link(x,i,s);link(y,i,t);
}
priwork(1,1);build(1,m,1);dfs(1,1);
if(f[1]!=inf)printf("%lld\n",f[1]);
else puts("-1");
}
int main(){work();return 0;}
Codeforces 671 D. Roads in Yusland的更多相关文章
- 【CodeForces】671 D. Roads in Yusland
[题目]D. Roads in Yusland [题意]给定n个点的树,m条从下往上的链,每条链代价ci,求最少代价使得链覆盖所有边.n,m<=3*10^5,ci<=10^9,time=4 ...
- codesforces 671D Roads in Yusland
Mayor of Yusland just won the lottery and decided to spent money on something good for town. For exa ...
- 【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 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- 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$的路.如果 ...
随机推荐
- 团队作业4——第一次项目冲刺(Alpha版本) Day 2
小队@JMUZJB-集美震惊部 一.Daily Scrum Meeting照片 二.Burndown Chart 燃尽图 三.项目进展 成员 工作 丘雨晨 环境配置 刘向东 数据库搭建,环境配置 江泽 ...
- 2017-2018-1 我爱学Java 第六七周 作业
团队六七周作业 完善版需求规格说明书 制定团队编码规范 数据库设计 后端架构设计 TODOList 参考资料 完善版需求规格说明书 <需求规格说明书>初稿不足之处: 1.开发工具写错 2. ...
- logging日志
import logging logging.basicConfig(filename='log.log', format='%(asctime)s - %(name)s - %(levelname) ...
- mycat入门_介绍与安装
利用闲暇时间接触了下mycat. 一.介绍 1.概述: 国内最活跃的.性能最好的开源数据库中间件,可以理解为数据库和应用层之间的一个代理组件. 2.作用: 读写分离.分表分库.主从切换. 3.原理: ...
- vue-cli 的项目 切换到Linux环境下遇到问题
之前用vue-cli脚手架在windows上开发的一个项目,现在有换mac的打算,但是换系统的话对代码对环境依赖比较严重. 去年和别的FE并行开发两个人用的都是windows,这样还好,没有什么问题, ...
- jacascript 立即执行函数(IIFE)与闭包
前言:这是笔者学习之后自己的理解与整理.如果有错误或者疑问的地方,请大家指正,我会持续更新! 一直没搞清楚立即执行函数和闭包之间的关系,总结一下: 闭包有很多种理解:访问不到内部作用域,函数就是这样, ...
- C#调用Python,报错No module named os
C#调用Python 环境:Windows 8.1,已经安装Python2.7(C:\Python27),配置了环境变量. 已经安装VS2013,VS2017 1.安装IronPython 下载地址h ...
- this对象指向
this表示函数运行时,自动生成的一个内部对象,只能在函数内部运行 function test(){ this.x = 1; } 随着使用场景的变化,this的值会发生变化 原则:this指的值调用函 ...
- ConcurrentHashMap基于JDK1.8源码剖析
前言 声明,本文用的是jdk1.8 前面章节回顾: Collection总览 List集合就这么简单[源码剖析] Map集合.散列表.红黑树介绍 HashMap就是这么简单[源码剖析] LinkedH ...
- laravel Passport - 创建 REST API 用户认证以及Dingo/Api v2.0+Passport实现api认证
第一部分: 安装passport 使⽤ Composer 依赖包管理器安装 Passport : composer require laravel/passport 接下来,将 Passport 的服 ...