题目:

D. The Child and Zoo
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Of course our child likes walking in a zoo. The zoo has n areas, that are numbered from 1 to n.
The i-th area contains ai animals
in it. Also there are m roads in the zoo, and each road connects two distinct areas. Naturally the zoo is connected, so you can reach any area of the zoo
from any other area using the roads.

Our child is very smart. Imagine the child want to go from area p to area q.
Firstly he considers all the simple routes from p to q.
For each route the child writes down the number, that is equal to the minimum number of animals among the route areas. Let's denote the largest of the written numbers as f(p, q).
Finally, the child chooses one of the routes for which he writes down the value f(p, q).

After the child has visited the zoo, he thinks about the question: what is the average value of f(p, q) for all pairs p, q (p ≠ q)?
Can you answer his question?

Input

The first line contains two integers n and m (2 ≤ n ≤ 105; 0 ≤ m ≤ 105).
The second line contains n integers: a1, a2, ..., an (0 ≤ ai ≤ 105).
Then follow m lines, each line contains two integers xi and yi (1 ≤ xi, yi ≤ nxi ≠ yi),
denoting the road between areas xi and yi.

All roads are bidirectional, each pair of areas is connected by at most one road.

Output

Output a real number — the value of .

The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.

Sample test(s)
input
4 3
10 20 30 40
1 3
2 3
4 3
output
16.666667
input
3 3
10 20 30
1 2
2 3
3 1
output
13.333333
input
7 8
40 20 10 30 20 50 40
1 2
2 3
3 4
4 5
5 6
6 7
1 4
5 7
output
18.571429

解法:

定义每一个边的权值等于两个点中较小一个点的点权。

然后并查集求最大生成树。每次从加边权最大的边。然后以此最小的组合就是两头全部点的相互组合。

代码:

/******************************************************
* author:xiefubao
*******************************************************/
#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <string.h>
//freopen ("in.txt" , "r" , stdin);
using namespace std; #define eps 1e-8
const double pi=acos(-1.0);
typedef long long LL;
const int Max=101000;
const int INF=1000000007 ; int parent[Max];
LL count1[Max];
int num[Max];
int getparent(int t)
{
    if(t==parent[t])
        return t;
    return parent[t]=getparent(parent[t]);
}
int n,m;
struct point
{
    int u,v;
    LL value;
} points[Max];
bool operator<(const point& a,const point& b)
{
    return a.value>b.value;
}
int main()
{
  while(scanf("%d%d",&n,&m)==2)
  {
      for(int i=1;i<=n;i++)
      scanf("%d",num+i);
      for(int i=1;i<=n;i++)
      {
          parent[i]=i;
          count1[i]=1;
      }
      for(int i=0;i<m;i++)
      {
          scanf("%d%d",&points[i].u,&points[i].v);
          points[i].value=min(num[points[i].u],num[points[i].v]);
      }
      sort(points,points+m);
      double ans=0;
      for(int i=0;i<m;i++)
      {
          int t1=getparent(points[i].u);
          int t2=getparent(points[i].v);
          if(t1==t2)
            continue;
          parent[t2]=t1;
          ans+=count1[t1]*count1[t2]*points[i].value;
          count1[t1]+=count1[t2];
      }
      LL N=n;
      printf("%.6f\n",ans/(N*(N-1))*2.0);
  }
   return 0;
}

CF437D(The Child and Zoo)最小生成树的更多相关文章

  1. cf437D The Child and Zoo

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  2. Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集

    B. The Child and Zoo Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  3. Codeforces 437D The Child and Zoo(贪心+并查集)

    题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...

  4. Codeforces 437 D. The Child and Zoo 并查集

    题目链接:D. The Child and Zoo 题意: 题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边.每条边的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么 ...

  5. 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 ...

  6. Codeforces D - The Child and Zoo

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

  7. Codeforces Round #250 (Div. 2) D. The Child and Zoo 并查集

    D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

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

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

  9. 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. ...

随机推荐

  1. 基于FPGA的DW8051移植(二)

    基于上一篇博文继续,本来想换到oc8051,但是还是不甘心,弄了这么久还是没有弄出来,真是打击屎了. 上一篇说3f进入了operation code所以判断是代码错误,后来发现不可以这么判断. 因为地 ...

  2. Flask web开发 处理POST请求(登录案例)

    本文我们以一个登录例子来说明Flask对 post请求的处理机制. 1.创建应用目录,如 mkdir   example cd example 2.在应用目录下创建  run.py文件,内容如下 fr ...

  3. windows下各个浏览器用html5进行h.264大视频播放的性能对比说明

    最近在调查windows下哪种浏览器进行大视频播放时候稳定性比较高. 举h.264的4g的视频为例. 选用的浏览器有ie10,firefox,chrome.(因为opera不支持h.264所以没有考虑 ...

  4. net use \\192.168.54.145 /user:administrator "12345qwert"无法连接,错误码1326

    1.在远程机的"控制面板-文件夹选项-查看-简单的文件共享",去掉选取,然后再尝试连接 2.控制面板\所有控制面板项\管理工具 下-->本地安全策略-->安全设置--& ...

  5. IOS SWIFT 简单操作文件

    //Home目录 let homeDirectory = NSHomeDirectory() //Documents目录 苹果建议将程序中建立的或在程序中浏览到的文件数据保存在该目录下,iTunes备 ...

  6. 回归基础从新认识——HTML+CSS

    前言 这段时间工作没那么繁杂,索性就想说来套系统的学习,之前去面试的时候,有被问及些基础的知识,居然回答不上来,也不能说是回答不上吧,回答的不全面.前端群上问了那个机构比较好,选择了慕课网.看了一段时 ...

  7. Android FindMyPhone功能模块的实现

    类似iPhone手机上面“查找我的iPhone” 1. 手机定位 需要考虑到国内和国外,国内使用百度地图,国外使用google地图,两种地图,属于不同的坐标系. 手机这边为了避免不同坐标系的问题,直接 ...

  8. Java 多线程-生产者、消费者

    一.整体代码 ThreadDemo.java public class ThreadDemo { public static void main(String[] args) { Godown god ...

  9. VS2008查看dll导出函数

    打开Visual Studio 2008 命令提示,使用命令 [plain] view plaincopyprint? dumpbin /exports simple.dll 即可查看

  10. The Apache™ Batik Project

    Apache(tm) Batik SVG Toolkit - a Java-based toolkit for applications or applets that want to use ima ...