博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4424 Conquer a New Region 并查集
阅读量:6852 次
发布时间:2019-06-26

本文共 1106 字,大约阅读时间需要 3 分钟。

题意:给出n个点和n-1条边,a到b的最大承载量为a和b之间承载量的最小值。以某一点为中心,求承载量之和的最大值。

由于a和b之间的承载量为它们之间承载量的最小值,所以先以两点之间的承载量从大到小排序。每次合并时有A,B两个集合,他们之间的承载量(cost)为当前最小,如果B合并到A,则A的总承载量为A之前的总承载量加上A,B之间的承载量成衣集合B中点的个数,即 cost[A]=cost[A]+num[B]*cost.

#include 
#include
#include
#include
#include
using namespace std;typedef long long ll;const int inf=1000000000;const int maxn=200005;int f[maxn];ll num[maxn],cost[maxn];struct Branch{ int a; int b; ll cost;}bra[maxn];bool cmp(Branch a,Branch b){ return a.cost>b.cost;}int Find(int x){ if(x!=f[x]) f[x]=Find(f[x]); return f[x];}ll Function(int n){ for(int i=0;i
=cost[b]+num[a]*bra[i].cost) { f[b]=a; cost[a]+=num[b]*bra[i].cost; num[a]+=num[b]; } else { f[a]=b; cost[b]+=num[a]*bra[i].cost; num[b]+=num[a]; } } return cost[Find(1)];}int main(){ //freopen("in.txt","r",stdin); int n; while(scanf("%d",&n)!=EOF) { for(int i=0;i<=n;i++) { f[i]=i; num[i]=1; } memset(cost,0,sizeof(cost)); for(int i=0;i

 

转载于:https://www.cnblogs.com/pach/p/5764112.html

你可能感兴趣的文章
Freebsd下PF的安装使用
查看>>
jhead命令详解
查看>>
去你的lua和go,哥发现node.js原来才是最爱~
查看>>
OC中initialize方法和init方法的区别
查看>>
一些不可思议的小问题
查看>>
界面间传值
查看>>
3.vsphere client的安装
查看>>
Linux实现最常用的磁盘阵列-- RAID5
查看>>
简单的菜单 menu
查看>>
Android布局之LinearLayout
查看>>
Intellij Idea 2017创建非Maven web项目使用tomcat部署实战
查看>>
工程DHCP配置
查看>>
GIL(全局解释器锁)与互斥锁
查看>>
我的友情链接
查看>>
Git常用操作及分支
查看>>
关于一种求最大公约数的算法的分析与证明
查看>>
微信授权莫名创建用户数据失败的原因
查看>>
网络高手修身
查看>>
JavaWeb综合案例-键盘模拟
查看>>
Android Day03-SQLite数据库操作及ListView详解
查看>>