• CCF 201812-4 数据中心(最小生成树)


    题意: 给一个树,并给一个节点root,树上有边权,现在所有的节点要向节root发送数据,求整棵树上最小的最大边权。
    其实就是最小生成树裸题。构造一颗最小生成树,无论以哪个节点为root,其实都要输出任意一层上的最大边权。也就是说,在构建MST的时候维护个最大的边权作为ans就好了,坑点是明明只有5e4个编号,n值【节点总数】却有5e5个,因此不能直接遍历前n*(n-1)/2条边,会疯狂运行错误,直接裸的排序遍历所有边,克鲁斯卡尔一遍就好。

    #include<bits/stdc++.h>
    #define LL long long
    const int maxn=5e5+7;
    using namespace std;
    struct edge
    {
        int from,to;
        LL val;
        bool operator <(const edge &a)const
        {
            return val<a.val;
        }
    }ma[maxn];
    LL n,m,rt,z[maxn];
    int finds(int x)
    {
        return z[x]==x?x:z[x]=finds(z[x]);
    }
    void join(int x,int y)
    {
        z[finds(x)]=z[y];
    }
    int main()
    {
        scanf("%lld%lld%lld",&n,&m,&rt);
        for(int i=0;i<m;i++)scanf("%d%d%lld",&ma[i].from,&ma[i].to,&ma[i].val);
        sort(ma,ma+m);
    //    m=(n-1)/2*n;
        for(int i=0;i<=n;i++)z[i]=i;
        LL sum=0,cnt=0;
        LL ans=0;
        for(int i=0;i<m;i++)
        {
            if(finds(ma[i].from)!=finds(ma[i].to))
            {
                cnt++;
    //            printf("====%d
    ",ma[i].val);
                if(ans<ma[i].val)ans=ma[i].val;
                join(ma[i].from,ma[i].to);
                sum+=ma[i].val;
            }
    //        if(cnt==m-1)break;
        }
        printf("%lld
    ",ans);
    }
    
  • 相关阅读:
    OpenCV 图像旋转
    单链表插入与删除数据
    opencv 数据训练
    C++ 小波变换
    二十七、miniscrapy,scrapy源码初解
    二十六、Scrapy自定义命令
    二十五、scrapy中的去重规则及自定义
    二十四、在scrapy中如何获取cookies
    十六、 IO多路复用,异步非阻塞
    五、IO模型简介
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135663.html
Copyright © 2020-2023  润新知