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>>m;
//输入
for (i=1;i</font>
Codeforces 437D The Child and Zoo(并查集)的更多相关文章
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
- Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树
Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The ...
- Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集
B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...
- Codeforces 437 D. The Child and Zoo 并查集
题目链接:D. The Child and Zoo 题意: 题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边.每条边的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么 ...
- Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- codeforces 437D The Child and Zoo
time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...
- [CF#250 Div.2 D]The Child and Zoo(并查集)
题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...
- Codeforces D - The Child and Zoo
D - The Child and Zoo 思路: 并查集+贪心 每条边的权值可以用min(a[u],a[v])来表示,然后按边的权值从大到小排序 然后用并查集从大的边开始合并,因为你要合并的这两个联 ...
- Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心
题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...
随机推荐
- WebClient小结
webclient功能有限,特别是不能使用身份验证证书,这样,上传数据时候问题出现,现在许多站点都不会接受没有身份验证的上传文件.尽管可以给请求添加标题信息并检查相应中的标题信息,但这仅限于一般意义的 ...
- golang(10):web开发 & 连接数据库
http编程 ) Go原生支持 http : import ("net/http") ) Go 的 http 服务性能和 nginx 比较接近 ) 几行代码就可以实现一个 web ...
- VC++ warning C4819 的解决方法(转)
编译VC++程序的时候出现如下提示警告: warning C4819: The file contains a character that cannot be represented in the ...
- python检测域名
pip install python-whois import whois print(whois.whois('baidu.com')) #输出有关baidu.com的所有域名
- centos7安装配置zabbix监控
CentOs7安装部署Zabbix3.4 1.关闭selinux和firewall systemctl stop firewalld setenforce 1.1检测selinux是否关闭 [ro ...
- Oracle学习笔记:窗口函数
目录 1.测试数据 2.聚合函数+over() 3.partition by子句 4.order by子句 5.序列函数 5.1 分析函数之 ntile 5.2 分析函数之 row_number 5. ...
- python之jupyter notebook
jupyter是一种交互式计算和开发环境的笔记,ipython命令行比原生的python命令行更加友好和高效,还可以运行web版的界面,支持多语言,输出图形.音频.视频等功能. 安装 pip inst ...
- linux常用命令(4)
linux常用命令(4) --- Vim编辑器与Shell命令脚本 如何使用vim编辑器来编写文档.配置主机名称.网卡参数以及yum仓库: 通过vim编辑器将Linux命令放入合适的逻辑测试语句(if ...
- Hadoop2.7.3集群安装scala-2.12.8 和spark2.7
Apache Spark™是用于大规模数据处理的统一分析引擎. 从右侧最后一条新闻看,Spark也用于AI人工智能 spark是一个实现快速通用的集群计算平台.它是由加州大学伯克利分校AMP实验室 开 ...
- Win10带有网络连接的安全模式怎么开启?
安全模式是在Windows系统中不加载第三方设备驱动程序的情况下启动电脑,从而可以方便的检测与修复电脑系统的错误,比如在安全模式下可以删除某些顽固的文件.查杀病毒.修复系统故障.卸载恶意软件等.不过在 ...