题目链接:Codeforces 437D The Child and Zoo

题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值。然后有m条路,每条路的权值为该条路连接的两个区中权值较小的一个。假设两个区没有直接连接,那么f值即为从一个区走到还有一个区中所经过的路中权值最小的值做为权值。问,平均两个区之间移动的权值为多少。

解题思路:并查集+贪心。将全部的边依照权值排序,从最大的開始连接,每次连接时计算的次数为连接两块的节点数的积(乘法原理)。

#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int N = 1e5+5; struct edge {
int u, v, w;
}e[N]; int n, m, f[N], c[N], w[N]; bool cmp (const edge& a, const edge& b) {
return a.w > b.w;
} int getfar (int x) {
return x == f[x] ? x : f[x] = getfar(f[x]);
} void init () {
scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) {
f[i] = i;
c[i] = 1;
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);
} int main () {
init();
double ans = 0; for (int i = 0; i < m; i++) {
int p = getfar(e[i].u);
int q = getfar(e[i].v); if (p != q) {
ans += (double)e[i].w * c[p] * c[q];
c[p] += c[q];
f[q] = p;
}
}
printf("%.6lf\n", ans * 2 / (n * 1.0 * (n-1)));
return 0;
}

Codeforces 437D The Child and Zoo(贪心+并查集)的更多相关文章

  1. Codeforces 437D The Child and Zoo(并查集)

    Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...

  2. Codeforces 437D The Child and Zoo - 树分治 - 贪心 - 并查集 - 最大生成树

    Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n. The ...

  3. codeforces 437D The Child and Zoo

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  4. Codeforces 437C The Child and Toy(贪心)

    题目连接:Codeforces 437C  The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...

  5. POJ 1456 Supermarket(贪心+并查集)

    题目链接:http://poj.org/problem?id=1456 题目大意:有n件商品,每件商品都有它的价值和截止售卖日期(超过这个日期就不能再卖了).卖一件商品消耗一个单位时间,售卖顺序是可以 ...

  6. Codeforces Round #582 (Div. 3)-G. Path Queries-并查集

    Codeforces Round #582 (Div. 3)-G. Path Queries-并查集 [Problem Description] 给你一棵树,求有多少条简单路径\((u,v)\),满足 ...

  7. Codeforces D - The Child and Zoo

    D - The Child and Zoo 思路: 并查集+贪心 每条边的权值可以用min(a[u],a[v])来表示,然后按边的权值从大到小排序 然后用并查集从大的边开始合并,因为你要合并的这两个联 ...

  8. Codeforces 437D 贪心+并查集

    这个题目让我想起了上次在湘潭赛的那道跪死了的题.也是最值问题,这个也是,有n个动物园 每个都有权值 然后被m条路径相连接,保证图是连通的,然后求所有的p[i][j]之和.i,j为任意两个zoo,pij ...

  9. Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心

    题目链接:http://codeforces.com/contest/731/problem/C 题解: 1.看题目时,大概知道,不同的袜子会因为要在同一天穿而差生了关联(或者叫相互制约), 其中一条 ...

随机推荐

  1. cassandra 服务启动流程

    cassandra 服务启动流程 1.  setup 1)   CassandraDaemon ->main publicstaticvoidmain(String[]args) { insta ...

  2. Chapter 1 Securing Your Server and Network(5):使用SSL加密会话

    原文:Chapter 1 Securing Your Server and Network(5):使用SSL加密会话 原文出处:http://blog.csdn.net/dba_huangzj/art ...

  3. 解决无法切换到jenkins用户的问题

    su - jenkins一直有效,今天在centos发现无效,原因是 /etc/password文件里的/bin/bash被yum安装的时候变成了/bin/false. 改动后就能够了. ubuntu ...

  4. 类似的微博推断客户关系sql声明

    类别似新浪微博的关注和共同关心 不知道别人是怎么设计的. 反正我是例如以下设计的 ID   USER   FRIEND 1     A         B 2      B         A 3   ...

  5. Struts2之—实现自己的结果集的定义ajax

    项目中我们常常遇到这种需求--页面部分刷新.比如:加入用户,转到加入用户页面时,页面自己主动载入了全部部门. 完整流程:选择所属部门,填写username和password,点击"注冊&qu ...

  6. WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制

    .NET WebAPI 用ActionFilterAttribute实现token令牌验证与对Action的权限控制 项目背景是一个社区类的APP(求轻吐...),博主主要负责后台业务及接口.以前没玩 ...

  7. Android4.4 蓝牙源代码段分析

    最近GOOGLE发布时间Android4.4,我看了看源代码.4.4蓝牙打开过程或这部分的一些变化,判断蓝牙开关是从接口设置settings在里面switch开关,widget当然,它可以切换,也许启 ...

  8. ContentProvider总结(Android)

    ContentProvider 1.适用场景 1) ContentProvider为存储和读取数据提供了统一的接口 2) 使用ContentProvider,应用程序能够实现数据共享 3) andro ...

  9. matlab简单实现SVD的推荐

    %svd chengxu A = [5 5 0 5;5 0 3 4; 3 4 0 3; 0 0 5 3; 5 4 4 5; 5 4 5 5]; A = A'; [U S V] = svd(A); U ...

  10. Microsoft.AlphaImageLoader过滤评论

    Microsoft.AlphaImageLoader是IE滤镜的一种,其主要作用就是对图片进行透明处理.尽管FireFox和IE7以上的IE浏览器已经支持透明的PNG图片,可是就IE5-IE6而言还是 ...