[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(并查集)的更多相关文章

  1. Codeforces 437D The Child and Zoo(贪心+并查集)

    题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...

  2. 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 ...

  3. 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/ ...

  4. Codeforces 437 D. The Child and Zoo 并查集

    题目链接:D. The Child and Zoo 题意: 题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边.每条边的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么 ...

  5. 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 ...

  6. codeforces 437D The Child and Zoo

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  7. [CF#250 Div.2 D]The Child and Zoo(并查集)

    题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...

  8. Codeforces D - The Child and Zoo

    D - The Child and Zoo 思路: 并查集+贪心 每条边的权值可以用min(a[u],a[v])来表示,然后按边的权值从大到小排序 然后用并查集从大的边开始合并,因为你要合并的这两个联 ...

  9. Codeforces Round #376 (Div. 2) C. Socks---并查集+贪心

    题目链接:http://codeforces.com/problemset/problem/731/C 题意:有n只袜子,每只都有一个颜色,现在他的妈妈要去出差m天,然后让他每天穿第 L 和第 R 只 ...

随机推荐

  1. unity 打包Error:WebException: The remote server returned an error: (403) Forbidden.

    記一下在ios上打包出錯: UnityEditor.BuildPlayerWindow+BuildMethodException: 2 errors at UnityEditor.BuildPlaye ...

  2. nop4.1学习ServiceCollectionExtensions(一)

    从入口进去,读取系统appsetting.jion的配置文件: 单例实例化配置数据,全局调用 注入HttpContextAccessor ASP.NET Core中提供了一个IHttpContextA ...

  3. JS基础_条件运算符

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. O055、Detach Volume 操作

    参考https://www.cnblogs.com/CloudMan6/p/5636510.html     本节我们开始学习 Volume Detach 操作,就是将Volume从Instance上 ...

  5. cookie转换成字典类型方便scraoy 使用

    #bakooie装换成紫电模式方便scrapy使用 cookid = "_ga=GA1.2.1937936278.1538889470; __gads=ID=1ba11c2610acf504 ...

  6. JSR-303

    JSR-303是java标准的验证框架,已有的实现由 Hibernate validator 定义的注解验证bean属性: 空检查 @Null 验证对象是否为空 @NotNull 验证对象不为空 @N ...

  7. vbox的四种网络模式

      一.NAT模式 特点: 1.如果主机可以上网,虚拟机可以上网 2.虚拟机之间不能ping通 3.虚拟机可以ping通主机(此时ping虚拟机的网关,即是ping主机) 4.主机不能ping通虚拟机 ...

  8. 第一章·ELKstack介绍及Elasticsearch部署

    一.ELKstack课程大纲  二.ELKstack简介 什么是ELK? 通俗来讲,ELK是由Elasticsearch.Logstash.Kibana 三个开源软件的组成的一个组合体,这三个软件当 ...

  9. 海康RTSP取流URL格式

    预览取流url [海康威视]举例说明: 主码流取流: rtsp://admin:12345@192.0.0.64:554/h264/ch1/main/av_stream 子码流取流: rtsp://a ...

  10. 第1章 python入门

    1.1 python的出生与应用   python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间,决心开 ...