题目链接: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. 在中文windows下使用pywinauto进行窗口操作

    这两天开始接触pywinauto,听说百度的自动化QA也用这个模块,于是来了兴趣,但网上的教程很少,而且基本上都是拿官方的notepad来说,首先中文菜单的支持是问题,其次各种操作也没有写清楚,阅读官 ...

  2. iOS 真机文件系统区分大小写,而模拟器可能不区分

    模拟器区不区分大小写要看mac os是不是区分大小写,而这个和你的文件系统有关,如下图 如果你使用了默认的格式,就区分不了大小写了! 看来以后还是应该使用第二种格式啊!

  3. 关于TortoiseSVN的一些知识

    TortoiseSVN的一切命令都是相对于它自己来说的 1.Import,导入,指的是导入SVN的代码库,即Repository. 2.Export,导出,指的是将代码从Repository中导出到你 ...

  4. Maven 3.3.3 Win10环境下的使用实例(下)

    这一篇文章将介绍如何在 Eclipse 中使用 Maven. 我们以 Eclipse Java EE 版本为例,首先要对 IDE 进行一些设置: JDK 环境 Maven 的本地安装路径 Maven ...

  5. oracle简历自增序列(转)

    步骤:1.创建序列   2.创建触发器. 语法解析:create sequence TB_CODE_SEQUENCEminvalue 1maxvalue 99999999999999999999999 ...

  6. 【leetcode】Single Number (Medium) ☆

    题目: Given an array of integers, every element appears twice except for one. Find that single one. No ...

  7. 【leetcode】Count Primes(easy)

    Count the number of prime numbers less than a non-negative number, n 思路:数质数的个数 开始写了个蛮力的,存储已有质数,判断新数字 ...

  8. 20145213《Java程序设计》第四周学习总结

    20145213<Java程序设计>第四周学习总结 教材学习内容总结 本周任务是学习面向对象的继承.接口以及之后的如何活用多态.(还真是路漫漫其修远兮啊!)教材也是延续上周艰深晦涩的语言风 ...

  9. quartz+spring 实现多任务动态定时器问题

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  10. WaxPatch中demo注意问题

    问题一 https://github.com/mmin18/WaxPatch网址中提供的demo是可以运行,但是存在一个问题,如果把patch.zip换成自己的并且上传到自己的服务器(github), ...