首页 技术 正文
技术 2022年11月20日
0 收藏 975 点赞 3,334 浏览 3609 个字

D. Roads in YuslandMayor 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.

Input

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 uivi 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.

Output

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.

Exampleinput

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

output

8

Note

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 ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,034
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,520
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,368
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,149
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,782
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,863