codefroces 并查集 250 D
来源:互联网 发布:sql删除一个字段的数据 编辑:程序博客网 时间:2024/05/17 23:33
#include <bits/stdc++.h>using namespace std;#define N 100000 + 10int n, m;int w[N];double ans;struct Edge{ int u, v, w;}e[N];int cmp(Edge a, Edge b){ return a.w > b.w ;}int fa[N], Rank[N];void init_set(){ for(int i = 1; i <= n; i++) { fa[i] = i; Rank[i] = 1; }}int Find(int x){ int p = x, t; while(fa[p] != p) p = fa[p]; while(x != p) { t = fa[x]; fa[x] = p; x = t; } return x;}void union_set(int i, int x, int y){ if((x = Find(x)) == (y = Find(y))) return ; ans += (double)Rank[x] * Rank[y] * e[i].w; Rank[x] += Rank[y]; fa[y] = x;}int main(){ while(~scanf("%d%d", &n, &m)) { init_set(); ans = 0; for(int i = 1; i <= n; i++) scanf("%d", w + i); for(int i = 0; i < m; i++) { scanf("%d%d", &e[i].u, &e[i].v); e[i].w = min(w[e[i].u], w[e[i].v]); } sort(e, e + m, cmp); for(int i = 0; i < m; i++) { union_set(i, e[i].u, e[i].v); } ans = ans * 2 / ((double) n * (n - 1)); printf("%.6lf\n", ans); } return 0;}/*3 310 20 301 22 33 14 310 20 30 401 32 34 37 840 20 10 30 20 50 401 22 33 44 55 66 71 45 7*/
1 0
- codefroces 并查集 250 D
- Codefroces 250D. Building Bridge (数学思维)
- MUTC 2 D - Matrix 并查集
- HDU 4496 D-City(并查集)
- hdu(4996) D-City 并查集
- Codeforces 366D 贪心+并查集
- hdu 4496 D-City 并查集
- HDU 4496 D-CITY(并查集)
- 【并查集】-HDU-4496-D-City
- HDU 4496 D-City(并查集)
- hdu 4496 D-City(并查集)
- 【HDU】4496 D-City 并查集
- D - Ubiquitous Religions(并查集)
- CF 371D Vessels 【并查集】
- Codeforces 371D. Vessels【并查集】
- CF437div D 贪心+并查集
- hdu4496--D.City(并查集)
- hdu D-City 反向并查集
- loadrunner中sprintf函数的用法
- ubuntu卸载安装包
- 树结构的自定义及基本算法(Java数据结构学习笔记)
- 20150816 开始捡起以前地专业!
- JAVA反射机制获取类和方法
- codefroces 并查集 250 D
- HDU 1536(sg博弈) S-Nim
- HDOJ 题目分类
- Java学习之路0806<复习>(线程wait 、notify)
- JVM总结
- 适配器模式——设计模式
- ACM数论基础之一_______质因数分解
- poj 2689 Prime Distance 筛素数加强版
- Wildcard Matching