题目:

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. Impossible WPF Part 1: Binding Properties

    原文 http://www.11011.net/wpf-binding-properties Ever wanted to write the following? <RichTextBoxDo ...

  2. 为什么要选择cdn加速

    CDN的通俗理解就是网站加速,CPU均衡负载,可以解决跨运营商,跨地区,服务器负载能力过低,带宽过少等带来的网站打开速度慢等问题. 比如: 1.一个企业的网站服务器在北京,运营商是电信,在广东的联通用 ...

  3. VC中关于 0xcccccccc和 0xcdcdcdcd异常

    VC在调试时,可能会报“写入位置0xcccccccc 时发生访问冲突”,或者“写入位置0xcdcdcdcd 时发生访问冲突”,这些问题可能是由于使用了未初始化的指针引起的. 在 Debug 模式下,V ...

  4. oracle scn浅析

    1. 系统SCN号 查询系统SCN号的方法: select dbms_flashback.get_system_change_number from dual commit后系统SCN号会增长,但是即 ...

  5. e-mail Web端管理

    邮件是和上海的一家微软的代理商合作的,管理很方便,但是目前感觉他家的邮件过滤机制有问题.

  6. First_1

    #region 练习题 1.4 (1) ///*求一个N阶方阵所有原素的和.(改进:二维数组的大小可以自己输入)*/ //Console.WriteLine("请输入i和j的值:" ...

  7. 浅谈MySql的存储引擎(表类型) (转)

    什么是MySql数据库 通常意义上,数据库也就是数据的集合,具体到计算机上数据库可以是存储器上一些文件的集合或者一些内存数据的集合. 我们通常说的MySql数据库,sql server数据库等等其实是 ...

  8. 区间dp-zoj3541-The Last Puzzle

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3541 题目大意: 在数轴上,有n个按钮,位置递增为d1,d2, ...

  9. 使用数组实现队列----《数据结构与算法分析---C语言描述》

    一.h文件:my_que.h #ifndef _MY_QUE_H_ #define _MY_QUE_H_ struct QueRecord; typedef struct QueRecord* que ...

  10. UML03-类图

    1.在类图中,聚合关系表达总体与局部的关系. 2.请根据下面的需求,画出用例图和类图. 系统允许管理员通过磁盘加载存货数据来运行存货清单报告: 管理员通过从磁盘加载存货数据.向磁盘保存存货数据来更新存 ...