codeforce 437D 并查集+贪心
来源:互联网 发布:linux ssh暴力破解 编辑:程序博客网 时间:2024/06/08 19:10
#include <cstdio>#include <algorithm>#include <iostream>using namespace std;const int maxn = 1E5 + 10;int x, y, m, n, fa[maxn], sz[maxn];double val[maxn];int find(int x){return x == fa[x] ? x : fa[x] = find(fa[x]);}struct Edge{int l, r;double w;bool operator < (const Edge &other) const{return w > other.w;}};Edge edge[maxn];int main(int argc, char const *argv[]){while (~scanf("%d%d", &n, &m) && n + m){for (int i = 1; i <= n; i++)scanf("%lf", &val[i]), fa[i] = i, sz[i] = 1;for (int i = 0; i < m; i++){scanf("%d%d", &x, &y);edge[i].l = x, edge[i].r = y;edge[i].w = min(val[x], val[y]);}sort(edge, edge + m);double ans = 0.0;for (int i = 0; i < m; i++){x = find(edge[i].l); y = find(edge[i].r);if (x != y)ans += edge[i].w * sz[x] * sz[y], sz[x] += sz[y], fa[y] = x;}printf("%lf\n", ans * 2 / n / (n - 1));}return 0;}
看题解做的:http://blog.csdn.net/ahjkl007/article/details/39353283
0 0
- codeforce 437D 并查集+贪心
- codeforce 731C Socks(并查集+贪心)
- Codeforces 366D 贪心+并查集
- CF437div D 贪心+并查集
- Codeforce 798D(贪心)
- codeforce 766D Mahmoud and a Dictionary 带权并查集
- 贪心+并查集
- 贪心+并查集
- Codeforces 437D The Child and Zoo(贪心+并查集)
- CodeForces 437D -- The Child and Zoo(并查集+贪心)
- Codeforces 437D The Child and Zoo(贪心+并查集)
- codeforce 445B 并查集
- codeforce C. Learning Languages(并查集)
- 【codeforce】744A 并查集
- Codeforce 705D 贪心选择
- codeforce 742 D. Arpa's weak amphitheater and Mehrdad's valuable Hoses (分组背包+并查集))
- cf 437 D(并查集)
- hdu1598 并查集+贪心
- maven jetty 插件的使用
- git 常见问题解决命令
- C# 理解Thread.Sleep()
- tomcat组成及工作原理
- golang日志中心取日志代码分享
- codeforce 437D 并查集+贪心
- Objective-C语法之NSArray和NSMutableArray
- POJ 2923 Relocation (状压DP+背包)
- 设计模式学习笔记
- lintcode:Binary Tree Level Order Traversal
- 新版XCode7.2.1的Instruments测试内存泄露
- 如何实现在Oracle中应用存储过程调用MatLab函数(3)
- 【优先队列】HDU4006The kth great number
- loadrunner简单使用——HTTP,WebService,Socket压力测试脚本编写