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
原创粉丝点击