• codefoeces problem 671D——贪心+启发式合并+平衡树


    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.

    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 ≤ n1 ≤ 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.

    Example
    input
    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+7;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); 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[2*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]+1;
            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]
    ",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!=1&&f[x]==x){
            if(tr[x].empty()) puts("-1"),exit(0);
            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=1;i<=n;i++) f[i]=i;
        for(int i=1;i<n;i++) x=read(),y=read(),insert(x,y);
        deep[1]=1; dfs(1,-1);
        for(int i=1;i<=m;i++){
            x=read(); y=read(); w=read();
            pos p=(pos){deep[y],w};
            delet(x,p,0);
        }
        push_ans(1);
        printf("%lld
    ",ans);
        return 0;
    }
    View Code

     

  • 相关阅读:
    生产环境之Nginx高可用方案
    MySQL主从同步配置
    SpringBoot整合MyBatisPlus配置动态数据源
    循环有序数组,查找值
    数组任意取三个数中乘积最大值
    多线程输出123以及有序输出1-75
    有序数组取中值
    RocketMQ原理及源码解析
    docker基础常用命令
    项目常用命令
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7481944.html
Copyright © 2020-2023  润新知