codefoeces problem 671D——贪心+启发式合并+平衡树
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.
————————————————————————————————————————
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
#define LL long long
const int M=3e5+;
int read(){
int ans=,f=,c=getchar();
while(c<''||c>''){if(c=='-') f=-; c=getchar();}
while(c>=''&&c<=''){ans=ans*+(c-''); c=getchar();}
return ans*f;
}
LL ans;
int n,m;
int f[M];
int find(int x){while(f[x]!=x) x=f[x]=f[f[x]]; return x;}
int first[M],cnt;
struct node{int to,next;}e[*M];
void ins(int a,int b){e[++cnt]=(node){b,first[a]}; first[a]=cnt;}
void insert(int a,int b){ins(a,b); ins(b,a);}
int deep[M],fa[M];
int dfs(int x,int last){
for(int i=first[x];i;i=e[i].next){
int now=e[i].to;
if(now==last) continue;
deep[now]=deep[x]+;
fa[now]=x;
dfs(now,x);
}
}
struct pos{
int d,w;
bool operator <(const pos &x)const{return d!=x.d?d>x.d:w>x.w;}
};
std::multiset<pos>tr[M];
typedef std::multiset<pos>::iterator IT;
void delet(int x,pos p,int s){
p.w+=s;IT it=tr[x].upper_bound(p);
if(it!=tr[x].begin()){
it--;
while(it->w>=p.w){
if(it==tr[x].begin()){tr[x].erase(it);break;}
IT now=it; --now;
tr[x].erase(it);
it=now;
}
}
it=tr[x].upper_bound(p);
if(it==tr[x].end()||it->w>p.w) tr[x].insert(p);
}
int dec[M];
void push_ans(int x){
for(int i=first[x];i;i=e[i].next){
int now=e[i].to;
if(now==fa[x]) continue;
push_ans(now);
if(tr[now].size()>tr[x].size()) tr[x].swap(tr[now]),std::swap(dec[x],dec[now]);
for(IT it=tr[now].begin();it!=tr[now].end();it++) delet(x,*it,dec[x]-dec[now]);
tr[now].clear();
}
//if(x==2) for(IT it=tr[x].begin();it!=tr[x].end();it++) printf("A[%d %d]\n",it->d,it->w);
while(tr[x].size()){
IT it=tr[x].begin();
if(it->d==deep[x]) tr[x].erase(it);
else break;
}
if(x!=&&f[x]==x){
if(tr[x].empty()) puts("-1"),exit();
IT it=tr[x].begin();
ans+=it->w-dec[x];
dec[x]=it->w;
int v=x; while(deep[v]>it->d) v=f[v]=find(fa[v]);
tr[x].erase(it);
}
}
int main(){
int x,y,w;
n=read(); m=read();
for(int i=;i<=n;i++) f[i]=i;
for(int i=;i<n;i++) x=read(),y=read(),insert(x,y);
deep[]=; dfs(,-);
for(int i=;i<=m;i++){
x=read(); y=read(); w=read();
pos p=(pos){deep[y],w};
delet(x,p,);
}
push_ans();
printf("%lld\n",ans);
return ;
}
codefoeces problem 671D——贪心+启发式合并+平衡树的更多相关文章
- CEOI 2019 Day2 T2 魔法树 Magic Tree (LOJ#3166、CF1993B、and JOI2021 3.20 T3) (启发式合并平衡树,线段树合并)
前言 已经是第三次遇到原题. 第一次是在 J O I 2021 S p r i n g C a m p \rm JOI2021~Spring~Camp JOI2021 Spring Camp 里遇到的 ...
- Luogu5290 十二省联考2019春节十二响(贪心+启发式合并)
考虑链的做法,显然将两部分各自从大到小排序后逐位取max即可,最后将根计入.猜想树上做法相同,即按上述方式逐个合并子树,最后加入根.用multiset启发式合并即可维护.因为每次合并后较小集合会消失, ...
- BZOJ 2809: [Apio2012]dispatching( 平衡树 + 启发式合并 )
枚举树上的每个结点做管理者, 贪心地取其子树中薪水较低的, 算出这个结点为管理者的满意度, 更新答案. 用平衡树+启发式合并, 时间复杂度为O(N log²N) ------------------- ...
- bzoj 2809 左偏树\平衡树启发式合并
首先我们对于一颗树,要选取最多的节点使得代价和不超过m,那么我们可以对于每一个节点维护一个平衡树,平衡树维护代价以及代价的和,那么我们可以在logn的时间内求出这个子树最多选取的节点数,然后对于一个节 ...
- 【BZOJ1483】【HNOI2009】梦幻布丁(启发式合并,平衡树)
[BZOJ1483][HNOI2009]梦幻布丁 题面 题目描述 N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1 ...
- ☆ [HNOI2012] 永无乡 「平衡树启发式合并」
题目类型:平衡树启发式合并 传送门:>Here< 题意:节点可以连边(不能断边),询问任意两个节点的连通性与一个连通块中排名第\(k\)的节点 解题思路 如果不需要询问排名,那么并查集即可 ...
- 【pb_ds】【平衡树启发式合并】【并查集】bzoj2733 [HNOI2012]永无乡
用并查集维护联通性.对每个联通块维护一个平衡树.合并时启发式合并.比较懒,用了pb_ds. #include<cstdio> #include<ext/pb_ds/assoc_con ...
- 【BZOJ1483】[HNOI2009]梦幻布丁(平衡树启发式合并+并查集)
题目: BZOJ1483 分析: (这题码了一下午,码了近250行,但是意外跑的比本校各位神仙稍快,特写博客纪念) 首先能看出一个显然的结论:颜色段数只会变少不会变多. 我们考虑用并查集维护区间,对于 ...
- [多校 NOIP 联合模拟 20201130 T4] ZZH 的旅行(斜率优化dp,启发式合并,平衡树)
题面 题目背景 因为出题人天天被 ZZH(Zou ZHen) 吊打,所以这场比赛的题目中出现了 ZZH . 简要题面 数据范围 题解 (笔者写两个log的平衡树和启发式合并卡过的,不足为奇) 首先,很 ...
随机推荐
- Android学习笔记之,调用系统图库,添加自定义字体,屏幕截图
新年开始的第一天就来学习了慕课迎春活动中的Android心愿分享一课,学到了几个知识点,在此记录一下. 1.调用系统图库调用系统图库用的是intent,步骤为弹出系统图库选择器,选择图片后获取到所选择 ...
- (原)一段看似美丽的for循环,背后又隐藏着什么
之前很长一段时间,潜心修炼汇编,专门装了一个dos7,慢慢玩到win32汇编,再到linux的AT&A汇编,尝试写mbr的时候期间好几次把centos弄的开不了机,又莫名其妙的修好了,如今最大 ...
- vue3.0 部署的基础流程
1.创建vue.config.js 主要是负责做设置的 2.修改vue.config.js 参考官方说明: 注意:对于本地开发的同学要注意,你之前在处理网络请求时是在8080端口下请求,现在如果换成了 ...
- Python-学习-项目1-即时标记-1
买了一本Python入门,奈何看不下去,只能是先看后面的项目,看到那里不懂的时候在回去学习. 项目名字:即时标记 大致的意思就是把一个纯文本文件标记成自己想要的格式文件. 首先就是待处理文本,我找不到 ...
- 自动化测试学习之路--java String、StringBuilder
Java中的String和StringBuilder类: 1.String对象是不可变的.每一个看起来修改了String值的方法,实际上都是创建了全新的String对象.代码示例如下: String ...
- fidder工具学习抓取Firefox包
fidder抓取Firefox的https请求 抓包之前需要设置fidder,我下面的截图是fidder4,打开fidder—>Tools—>Options如图: 选择https,勾选所有 ...
- django2.0 以上版本安装 xadmin
1.xadmin的下载 源码包下载地址: https://github.com/sshwsfc/xadmin/tree/django2 2.使用命令安装xadmin pip install 你下载的压 ...
- Spring实战第四章学习笔记————面向切面的Spring
Spring实战第四章学习笔记----面向切面的Spring 什么是面向切面的编程 我们把影响应用多处的功能描述为横切关注点.比如安全就是一个横切关注点,应用中许多方法都会涉及安全规则.而切面可以帮我 ...
- 【积累】LinqToSql复合查询结果转DataTable数据
最近的项目用到了大量的复合查询结果用于数据源,绑定到数据控件上. 为了方便,我们把它转换成DataTable的数据源形式.请看下面的示例: 1)思考自己需要的数据,然后组合,因此创建一个新的类: // ...
- $this是什么意思-成员变量和局部变量的调用
关键字$this代表其所在的当前对象 使用当前对象的属性和方法 $this->取值 方法内的局部变量 不能用$this 关键字取值 /* 和java,c++相比 方法体内想访问调用者的属性,必须 ...