题目链接: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. LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]

    1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...

  2. 软体project(两)——软体project

        每本书的第一章,都是在讲宏观的东西.软工也不例外.接下来.我们就要介绍软件project"是什么"的问题. 一.是什么? watermark/2/text/aHR0cDov ...

  3. Python 统计Facebook用户爱好的个数

    CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-8-12 @author: guaguastd @name: f ...

  4. Solr/SolrCloud -error

    状态 2014-08-20 10:46:22,356 INFO [coreZkRegister-1-thread-1] [org.apache.solr.cloud.ShardLeaderElecti ...

  5. 7 JavaScript Basics Many Developers Aren't Using (Properly)【转】

    JavaScript, at its base, is a simple language that we continue to evolve with intelligent, flexible ...

  6. RH133读书笔记(6) - Lab 6 Adding New Filesystems to the Filesystem Tree

    Lab 6 Adding New Filesystems to the Filesystem Tree Goal: Develop skills and knowlege related to par ...

  7. 基于android 社会的app短信分享 发送回调事件的实现

    摘要 前一段时间.由于项目的需要,采用ShareSDK该共享功能.其中包含 短信股吧.和呼叫系统,以分享要与成功处理服务器交互的消息后,(我不在乎在这里,收到.仅仅关心发出去了).可是ShareSDk ...

  8. HDOJ 4821 String

    串hash String Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. OpenCV视频播放方法

    OpenCV视频播放方法 今天折腾了一下OpenCV的视频播放功能,希望能对项目又帮助- 代码还是非常easy的,仅仅是之前遇到点小麻烦,找不到cvCreateFileCapture函数的定义,花了一 ...

  10. Teams(uva11609+组合)

    I - Teams Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit cid=7795 ...