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>
查看全文
相关阅读:
CARTA:持续自适应风险与信任评估
Quantitative Finance Reading List
金融是不是一群不事生产的人,对社会毫无贡献的人,互相对赌的零和游戏?
青藤云安全细述最具影响力的三大安全架构:零信任、ATT&CK、自适应安全
以太坊DApp开发指南
开源 serverless 产品原理剖析(一)
Top 5 decentralized platforms
Top 5 decentralized platforms
区块链上编程:DApp 开发简介
说说期货高频的一些分类
原文地址:https://www.cnblogs.com/orangee/p/8972677.html
最新文章
[AWS] AWS
[Docker] Create a base container
数据库 SQLite ORM框架 LitePal [MD]
docker 网络互连
docker 容器使用
python 3.8.3 + openssl-1.1.1g 编译
图像对齐(图像配准)方法记录
GJB5000A与CMMI的有哪些区别与哪些共性?
刚体变换与非刚体变换
python 进度条 tqdm
热门文章
Ubuntu系统安装VNC虚拟网络控制台
Linux Kernel 5.9-rc3 发布了
传播小知识:timeout限制时间命令
Centos7 使用ssh进行x11图形界面转发
Linux命令行:对内容进行大小写字符转换
XFS文件系统的备份、恢复、修复
实战-使用 Cobbler 安装操作系统
使用nmcli配置主备模式链路聚合
Hive学习之路 (一)Hive初识
jsplumb 中文教程
Copyright © 2011-2022 走看看