zoukankan
html css js c++ java
Codeforces 437D The Child and Zoo(并查集)
[Codeforces 437D The Child and Zoo](http://codeforces.com/problemset/problem/437/D) 题目大意: 有一张连通图,每个点有对应的值。定义从p点走向q点的其中一条路径的花费为途径点的
最小值
。定义f(p,q)为从点p走向点q的所有路径中的
最大花费
。累加每一对p,q的f(p,q),并求平均值。 乍一看以为是对图的搜索,但搜索求和的过程肯定会超时。这一题巧妙的用到了[并查集](http://www.cnblogs.com/orangee/p/8686470.html),因此做简单记录。 思路: 将边的权值定义为两点间的较小值,对边进行降序排序。排序后将每条边的两点进行并查集维护,由于排了序,所以可以保证两个点所属集合合并时,num[u]、num[w]、v三者的乘积得到是两个集合中的点两两组合的f(u,w)的总和(因为此时两集合中任意各取一点都满足所走路径的花费为v(当前边的权值),且是这两点所有路径中花费最大的),这也是个人感觉该解法的巧妙之处(其中num[i]表示根为i的集合的大小)。总之感觉这题对问题的转化真的很有趣。 PS:
要注意累加时中间过程可能溢出,因此可以强制转化其中一个数为double,从而使其他数跟着类型提升,防止溢出。
代码: ```C++ #include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std; typedef long long ll; typedef map
M; typedef queue
Q; typedef vector
V; typedef pair
P; const int maxn=5*100000; int a[maxn],p[maxn],num[maxn];//num[i]表示根为i的集合的大小 struct edge { int u,w,v; }; bool cmp(const edge& a,const edge& b) { return a.v>b.v; } edge e[maxn]; double sum=0; void init(int n) { for (int i=0;i<=n;++i) { p[i]=i; num[i]=1; } return; } int find(int x) { if (p[x]==x) return x; return p[x]=find(p[x]); } void unite(int x,int y,int v) //合并两点所属集合称为一个新的连通块 { x=find(x); y=find(y); if (x!=y) { sum+=double(v)*num[x]*num[y]; p[x]=y; num[y]+=num[x]; return; } return; } int main() { int i,j,n,m,t,k; cin>>n>>m; //输入 for (i=1;i<=n;++i) scanf("%d",&a[i]); for (i=0;i
</font>
查看全文
相关阅读:
如何在IIS添加MIME扩展类型
如何在ASP.NET的web.config配置文件中添加MIME类型
Entity Framework 数据库先行、模型先行、代码先行
Entity Framework 代码先行之约定配置
netcore3.0 IOptions 选项(一)
netcore3.0 IFileProvider 文件系统
netcore3.0 IServiceCollection 依赖注入系统(三)
netcore3.0 IServiceCollection 依赖注入系统(二)
netcore3.0 IServiceCollection 依赖注入系统(一)
netcore3.0 IConfiguration配置源码解析(四)
原文地址:https://www.cnblogs.com/orangee/p/8972677.html
最新文章
吃什么蔬菜可以清理血管垃圾
SQL Server 中 ROWLOCK 行级锁
数据库大并发操作要考虑死锁和锁的性能问题
sql如何判断表字段是否存在默认值
WebUploader UEditor chrome 点击上传文件选择框会延迟几秒才会显示 反应很慢
UEditor-从客户端(editorValue="<p>asd</p>")中检测到有潜在危险的 Request.Form 值。
SQL Server中Text和varchar(max)数据类型区别
Win7/Win8/Win8.1/Win10下的DragEnter DragDrop事件不触发
Oracle表空间,用户,用户授权
ListView.DragEnter触发不了
热门文章
js调用父窗口中的方法
EasyUI-panel 内嵌页面上的js无法被执行
IE11里边form拦截失效,永远被弹回登录页
JS js与css的动态加载
(zxing.net)一维码Code 39的简介、实现与解码
(zxing.net)一维码Codabar的简介、实现与解码
(zxing.net)二维码Aztec的简介、实现与解码
关于SQL Server 安装程序在运行 Windows Installer 文件时遇到错误
NPOI操作Excel辅助类
ASP.NET MVC 了解FileResult的本质
Copyright © 2011-2022 走看看