Codeforces Round#250 D. The Child and Zoo(并差集)
题目链接: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(并差集)的更多相关文章
- BZOJ 3625: [Codeforces Round #250]小朋友和二叉树
3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 304 Solved: 13 ...
- 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/ ...
- 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 ...
- CodeForces Round #250 Div2
A. The Child and Homework 注意仔细读题,WA了好多次,=_= #include <cstdio> #include <cstring> #includ ...
- 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 ...
- 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/ ...
- 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, ...
- Codeforces Round #250 (Div. 2)—A. The Child and Homework
好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人 被HACK 1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...
- 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 ...
随机推荐
- subprocess模块在Windows下调用失败问题
bug of pythonhttp://bugs.python.org/issue1759845 解决:print sys.stdout.encoding #eg : it shows cp936i ...
- ios Swift 一些注意事项
func test(one:NSString) -> NSString{ return "aaa" } func test(one:Int) -> NSString{ ...
- ACM/ICPC 之 两道dijkstra练习题(ZOJ1053(POJ1122)-ZOJ1053)
两道较为典型的单源最短路径问题,采用dijkstra解法 本来是四道练习题,后来发现后面两道用dijkstra来解的话总觉得有点冗余了,因此暂且分成三篇博客(本篇以及后两篇). ZOJ1053(POJ ...
- Python2.7<-------->Python3.x
版本差异 from __future__ Python2.7 Python3.x 除法 / // Unicode u'' ...
- [转]Android How to Download and Make Volley.jar
原文来自:http://tips.androidhive.info/2015/08/android-how-to-download-and-make-volley-jar/ 1 Comment . ...
- 【故障处理】mysql出现大量slave bin日志,将磁盘空间占满
master服务器IO线程 NO 问题描述:造成大量slave bin 日志 大量占用磁盘 排查解决步骤: 1.检查是配置的问题还是mysql数据库本身的故障 2.将master的机器 mysql_ ...
- Java Web开发框架Spring+Hibernate整合效果介绍(附源码)
最近花了一些时间整合了一个SpringMVC+springAOP+spring security+Hibernate的一套框架,之前只专注于.NET的软件架构设计,并没有接触过Java EE,好在有经 ...
- CString之GetBuffer与ReleaseBuffer
我们知道,CString是MFC中提供的方便字符串操作的一个类,非常好使,具有自动动态内存管理功能. GetBuffer()主要作用是将字符串的缓冲区长度锁定: ReleaseBuffer()则是解除 ...
- October 2nd 2016 Week 41st Sunday
The road to success is lined with many tempting parking spaces. 通往成功的路边充斥着许多诱人的休息区. Exhausted, I thi ...
- Android Service 与 Thread 的区别
Ref:http://blog.csdn.net/jiangwei0910410003/article/details/17008687 1). Thread:Thread 是程序执行的最小单元,它是 ...