题目链接: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. 谷歌上不去,长期的解决方案。在稳定高速Google和Gmail

    对稳定Google神器 国内Google很不稳定,缓慢并经常上不去,由"我想去Google",安全和稳定的使用Google.Gmail.Google+所以通常需要特殊的手段岗位胜任 ...

  2. UVA 1291 Dance Dance Revolution(DP)

    意甲冠军:跳舞机有一个上5积分,分别central, top, bottom, left, right分,区区足站立还是需要1点物理,从一个单纯的脚central点上须要2点体力,从一个点上移动到相邻 ...

  3. 自己主动机串标:Directed Acyclic Word Graph

    trie -- suffix tree -- suffix automa 有这么几个情况: 用户输入即时响应AJAX搜索框, 显示候选名单. 搜索引擎keyword统计数量. 后缀树(Suffix T ...

  4. HDN2048(交错复发)

    上帝.神与神 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  5. JSON 数据使用

    当用不同的数据的模板需要更换时.假设数据点的量.使用json非常方便. json物: var JSONObject= { "name":"Bill Gates" ...

  6. Sqlmap渗透测试是常用语句

    1.获取站点信息 sqlmap.py  -u http://.../ 2.获取站点的数据库信息 sqlmap.py -u http://../ --dbs 3.获取数据库DataBaseTest下的表 ...

  7. Lichee(三) Android4.0该产品的目标文件夹,Lichee链接---extract-bsp

    由<Lichee() 在sun4i_crane平台下的编译>介绍了编译lichee的基本情况,我们终于得到了编译后的结果例如以下: out/ ├── android │   ├── bIm ...

  8. Team Foundation Server 2015使用教程--默认团队成员连接tfs及checkin操作

  9. Android Studio简单设置(转)

    Android Studio 简单设置 界面设置 默认的 Android Studio 为灰色界面,可以选择使用炫酷的黑色界面.Settings --> Appearance --> Th ...

  10. [SignalR]异常信息捕获以及处理

    原文:[SignalR]异常信息捕获以及处理 异常处理,一般采用try..catch方式处理,而signalR里面有HubPipelineModule类可以捕获到Hub内发生的异常信息. 从上图中,可 ...