UVA 1664 Conquer a New Region (并查集+贪心)

题意:

n 个城市形成一棵树,每条边有权值C(i,j),任意两个点的容量S(i,j)定义为i与j 唯一通路上容量的最小值,找一个点,使得它到其他点的容量之和最大,求最大值?

思路:

贪心思路。

我们给边从大到小排序,因为是一棵树, 每个边都要处理。

因此当前枚举的边一定是当前集合中 最小的边,因此他就是容量, 我们分别让两个端点的并查集父亲 作为中心城市, 找一个大的进行合并即可。

这样枚举到最后 我们就可以得出最大容量。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn = 200000 + 6;

typedef long long LL;

struct Edge{
    int u,v,w;
    void read(){
        scanf("%d %d %d",&u, &v, &w);
    }
    bool operator < (const Edge& rhs) const {
        return w > rhs.w;
    }

}p[maxn];


struct Node{
    int num;
    LL sum;
}nod[maxn];

int fa[maxn];
int find(int x){
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

int main(){
    int n, m;
    while(~scanf("%d",&n)){
        m = n-1;
        for (int i = 0; i < m; ++i){
            p[i].read();
        }
        sort(p,p+m);

        for (int i = 1; i <= n; ++i){
            nod[i].num = 1;
            nod[i].sum = 0;
            fa[i] = i;
        }

        for (int i = 0; i < m; ++i){

            int x = find(p[i].u);
            int y = find(p[i].v);
            LL xx = nod[x].sum + (LL)p[i].w * (LL)nod[y].num;
            LL yy = nod[y].sum + (LL)p[i].w * (LL)nod[x].num;
            if (xx < yy) {
                swap(xx,yy);
                swap(x,y);

            }
            fa[y] = x;
            nod[x].sum = xx;
            nod[x].num += nod[y].num;
        }

        printf("%lld\n",nod[find(1)].sum);
    }


    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值