题目链接:http://codeforces.com/problemset/problem/437/D

思路:并差集应用,先对所有的边从大到小排序,然后枚举边的时候,如果某条边的两个顶点不在同一个集合中就合并,并且用一个sum记录这两个集合的大小,这样这两个集合中的每一对点都要经过这条边,然后更新一下sum就可以了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (100000 + 100); struct Edge {
int u, v, w;
} edge[MAX_N << 1]; int cmp(const Edge &p, const Edge &q)
{
return p.w > q.w;
}
int N, M, a[MAX_N], parent[MAX_N];
long long sum[MAX_N], ans; int Find(int x)
{
if (x == parent[x]) return x;
return parent[x] = Find(parent[x]);
} int main()
{
cin >> N >> M;
FOR(i, 1, N) cin >> a[i], parent[i] = i, sum[i] = 1;
FOR(i, 1, M) {
int u, v; cin >> u >> v;
edge[i].u = u, edge[i].v = v, edge[i].w = min(a[u], a[v]);
}
sort(edge + 1, edge + M + 1, cmp);
ans = 0;
FOR(i, 1, M) {
int r1 = Find(edge[i].u), r2 = Find(edge[i].v);
if (r1 == r2) continue;
parent[r1] = r2;
ans += edge[i].w * sum[r1] * sum[r2];
sum[r2] += sum[r1];
}
printf("%.7f\n", 2.0 * ans / (1.0 * N * (N - 1)));
return 0;
}



Codeforces Round#250 D. The Child and Zoo(并差集)的更多相关文章

  1. BZOJ 3625: [Codeforces Round #250]小朋友和二叉树

    3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 304  Solved: 13 ...

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

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

  4. CodeForces Round #250 Div2

    A. The Child and Homework 注意仔细读题,WA了好多次,=_= #include <cstdio> #include <cstring> #includ ...

  5. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

    D. The Child and Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

  6. Codeforces Round #250 (Div. 1) A. The Child and Toy 水题

    A. The Child and Toy Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  7. Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

    题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...

  8. Codeforces Round #250 (Div. 2)—A. The Child and Homework

         好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人  被HACK  1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...

  9. Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. select count(*)和select count(1)的区别

    一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的 假如表沒有主键(Primary key), 那么count(1)比count(*)快, 如果有主键的話 ...

  2. java切换VPN让你像幽灵一样出现在全国各地

    在很多情况下,有些网络应用的需求会要求模拟人在不同地区访问网站和应用.因而切换IP也就应运而生了,然而IP作为一种稀缺资源不是随便可以获得的.因而会想到应用程序切换VPN来达到全国不同地区访问网络.因 ...

  3. 解读Unity中的CG编写Shader系列三

    转自http://www.itnose.net/detail/6096068.html 在上一个例子中,我们得到了由mesh组件传递的信息经过数学转换至合适的颜色区间以颜色的形式着色到物体上.这篇文章 ...

  4. python pexpect 学习与探索

    pexpect是python交互模块,有两种使用方法,一种是函数:run另外一种是spawn类 1.pexpect  module 安装 pexpect属于第三方的,所以需要安装, 目前的版本是 3. ...

  5. objective-c数组笔记

    数组与可变数组 2015年6月14日 1.数组 数组的初始化方式 1.初始化一个空数组 NSArray *array = [[NSArray alloc] init];//不可变数组,数组内不可以添加 ...

  6. PHP try catch

    本文转载自百度知道 http://zhidao.baidu.com/link?url=Wi5EOXIf12yBp9d_4VoFHCUFtlTPcZJ0sxidLspV6P7qAqYMap3IC6dXE ...

  7. Windows Form 中快捷键设置

    在Windows Form程序中使用带下划线的快捷键只需要进行设置: 就能够工作.

  8. chaper3_exerise_UVa455_周期串

    #include<iostream> #include<cstring> #include<stdio.h> using namespace std; ; int ...

  9. backtracking(回溯算法)

    http://blog.csdn.net/zxasqwedc/article/details/42270215 permutation的程式码都会长成这样的格式: ] = { 'a', 'b', 'c ...

  10. 玩转Java对象和XML相互转换

    最近在项目中一直出现Java对象和XML之间的相互转换,一开始由于项目很庞大,我又是临时调度过去,导致在按照项目组长的要求进行写代码的同时,总是在这块云里雾里,最近才慢慢开始搞清楚项目中具体的使用缘由 ...