题目链接: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. Wijmo 5 与Breeze 的组合,及与METRONIC 的集成

    1.Wijmo 5  是支持ANGULARJS 的HTML5 控件   http://wijmo.gcpowertools.com.cn/ 官方试用版  C1Wijmo-Eval_5.20151.42 ...

  2. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  3. 76 binary_search 查找重复元素

    [本文链接] http://www.cnblogs.com/hellogiser/p/binary-search-for-repeated-element.html [题目] 给定一个升序排列的自然数 ...

  4. Java中使用Collections.sort()方法对数字和字符串泛型的LIst进行排序

    在List的排序中常用的是Collections.sort()方法,可以对String类型和Integer类型泛型的List集合进行排序. 首先演示sort()方法对Integer类型泛型的List排 ...

  5. mysql的innodb中事务日志ib_logfile

    mysql的innodb中事务日志ib_logfile事务日志或称redo日志,在mysql中默认以ib_logfile0,ib_logfile1名称存在,可以手工修改参数,调节开启几组日志来服务于当 ...

  6. Python: 程序print到文件中

    Python 3.x 将输出内容写入到一个文件,需要两个命令.open指明Python用什么文件名,w 意味着我们要写入该文件, encoding=”utf-8″指明Python如何把中文写入该文件. ...

  7. nyoj925_国王的烦恼_并查集

    国王的烦恼 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 C国由n个小岛组成,为了方便小岛之间联络,C国在小岛间建立了m座大桥,每座大桥连接两座小岛.两个小岛间可能 ...

  8. 针对Xcode的警告忽略消除处理

    一.问题描述 html代码如下 <html> <head> <meta charset="utf-8"/> <title>我的网页& ...

  9. codeforces 558B. Amr and The Large Array 解题报告

    题目链接:http://codeforces.com/problemset/problem/558/B 题目意思:给出一个序列,然后找出出现次数最多,但区间占用长度最短的区间左右值. 由于是边读入边比 ...

  10. 【leetcode】Gray Code (middle)

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...