[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. winfrom 点击按钮button弹框显示颜色集

    1.窗体托一个按钮button: 2.单击事件: private void btnForeColor_Click(object sender, EventArgs e) { using (ColorD ...

  2. vue axios异步请求django

    1,配置请求路径 (1),vue中的请求路径要与django视图路径相同. (2),vue中的路由路径也要和django视图路径相同,比如视图路径为127.0.0.1:8000:home/index, ...

  3. js跳转页面与打开新窗口的方法

    1.超链接<a href="http://www.jb51.net" title="脚本之家">Welcome</a> 等效于js代码 ...

  4. 如何源码编译安装并控制nginx

    安装nginx 注意 Linux操作系统需要2.6及其以上的内核(支持epoll) 使用nginx的必备软件 gcc编辑器 yum -y install gcc gcc-c++ pcre库(支持正则表 ...

  5. [LeetCode]1089. Duplicate Zeros

    Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remainin ...

  6. 第十章、typing模块

    目录 第十章.typing模块 一.说明 二.typing模块的作用 三.使用typing模块 四.typing常用类型 第十章.typing模块 一.说明 二.typing模块的作用 类型检查,防止 ...

  7. vlan linux内核数据流程

    转:http://blog.sina.com.cn/s/blog_62bbc49c0100fs0n.html 一.前言 前几天做协议划分vlan的时候看了一些linux内核,了解不深,整理了下vlan ...

  8. Vivotek 摄像头远程栈溢出漏洞分析及利用

    Vivotek 摄像头远程栈溢出漏洞分析及利用 近日,Vivotek 旗下多款摄像头被曝出远程未授权栈溢出漏洞,攻击者发送特定数据可导致摄像头进程崩溃. 漏洞作者@bashis 放出了可造成摄像头 C ...

  9. LNMP安装与配置之CentOS7的安装。

    LNMP与LAMP是我们常见的两种网站服务器架构.LNMP代表的就是Linux系统下Nginx+MySQL+PHP/Python,LAMP代表的则是Linux系统下Apache+MySQL+PHP/P ...

  10. linux基础—课堂随笔09_数组

    数组:(6.14 第一节) 取分区利用率,大于百分之八十则发出警报 取分区第一列 取分区使用率: 脚本: 检查脚本语法: ——end 数组切片: 1.跳过前两个取后面 2.跳过前两个取三个 生成10个 ...