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
Note

Consider the first sample. There are 12 possible situations:

  • p = 1, q = 3, f(p, q) = 10.
  • p = 2, q = 3, f(p, q) = 20.
  • p = 4, q = 3, f(p, q) = 30.
  • p = 1, q = 2, f(p, q) = 10.
  • p = 2, q = 4, f(p, q) = 20.
  • p = 4, q = 1, f(p, q) = 10.

Another 6 cases are symmetrical to the above. The average is .

Consider the second sample. There are 6 possible situations:

  • p = 1, q = 2, f(p, q) = 10.
  • p = 2, q = 3, f(p, q) = 20.
  • p = 1, q = 3, f(p, q) = 10.

Another 3 cases are symmetrical to the above. The average is .

译文

当然了,我们的小朋友是非常喜欢在动物园游玩的。这个动物园有n个区域,编号为1至n,第i个区域有a[i]仅仅动物。动物园里还有m条路,每条路连接两个不同样的区域。动物园自然是连通的——你能够从不论什么一个区域到达随意的其它区域。

我们的小朋友非常聪明。如果他打算从区域p走到区域q。他首先会考虑全部从p走到q的简单路径,然后对于每一条路径,他都会写下路径上的区域中动物数量的最小值。

我们记这些写下的数字中最大的为f(p,q)。终于,他会选择一条值为f(p,q)的路径。

在他游玩这个公园之后。他想到了这样一个问题:全部f(p,q)(p不等于q)的平均值是多少?你能回答这个问题吗?

Input

第一行是n,m(2<=n<=10^5; 0<=m<=10^5)。第二行有a[1],a[2],...,a[n](0<=a[i]<=10^5)。接下来m行,每行有两个整数x[i]和y[i](1<=x[i],y[i]<=n; x[i]不等于y[i]),表示一条连接x[i]和y[i]的道路。

全部道路都是双向。每对区域至多由一条道路直接连接。

Output

输出一个实数——全部f(p,q)之和(p不等于q)除以n(n-1)的值。

假设你的答案与标准答案的相对误差或绝对误差不超过10^(-4),就会被觉得是正确的。

题解

带权并查集、最大生成树。

想清楚了非常easy。

首先设从x点到y点,要进过k条路:1、每条路连接的两个点中,点权值小的点对答案有贡献,我们能够把这条边的边权记为“这条路连接的两个点中点较小的权值”。2、全部k条路中,边权最大的为f(x,y)。

以上因为要找最大(贪心,且在图中),就想到了最大生成树。

接下来是计数问题。

记一下每一个集合中的元素就可以。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int n,m,fa[100002];
ll a[100002],size[100002],ans;
struct bian {int x,y;ll v;} e[100002];
void init()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{scanf("%I64d",&a[i]);
fa[i]=i; size[i]=1;
}
for(int i=1;i<=m;i++)
{scanf("%d%d",&e[i].x,&e[i].y);
e[i].v=min(a[e[i].x],a[e[i].y]);
}
}
bool kp(const bian &u,const bian &w)
{return u.v>w.v;}
int find(int x)
{
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
void klskl()
{
sort(e+1,e+m+1,kp);
for(int i=1;i<=m;i++)
{int r1=find(e[i].x),r2=find(e[i].y);
if(r1!=r2)
{ans+=e[i].v*size[r1]*size[r2];
size[r1]+=size[r2];
fa[r2]=r1;
}
}
ll chus=(ll)n*(n-1)/2;//注意:一定要有这句话否则会wa。
double ans2=(double)ans/chus;
printf("%.6lf",ans2);
}
int main()
{
init(); klskl(); return 0;
}

codeforces 437D The Child and Zoo的更多相关文章

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

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

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

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

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

  4. Codeforces D - The Child and Zoo

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

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

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

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

  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 437B The Child and Set

    题目链接:Codeforces 437B The Child and Set 開始是想到了这样的情况,比方lowbit之后从大到小排序后有这么几个数,200.100,60.50.S = 210.那先选 ...

  9. cf437D The Child and Zoo

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

随机推荐

  1. shell script练习:利用日期进行文件的创建

    随日期变化:利用 date 进行文件的创建 想像一个状况,假设我的服务器内有数据库,数据库每天的数据都不太一样,因此当我备份时, 希望将每天的数据都备份成不同的档名,这样才能够让旧的数据也能够保存下来 ...

  2. vs2010 视图 aspx页面设计窗口创建控件时出错 未将对象引用设置到对象的实例

    第一步,首先关闭aspx页面 第二步,在单击项目右击,选择“清理” 第三步,然后在打开aspx页面,就可以看到正常的页面了. 注:一次不行的会,多做几次. 如果还是不行的话,你看看你.cs页面是否继承 ...

  3. 如何防止SQL注入式攻击

    一.什么是SQL注入式攻击?  所谓SQL注入式攻击,就是攻击者把SQL命令插入到Web表单的输入域或页面请求的查询字符串,欺骗服务器执行恶意的SQL命令.在某些表单中,用户输入的内容直接用来构造(或 ...

  4. microsoft ajax registered - to fix microsoft ajax update panel post back

    <dnn:DnnScriptBlock runat="server">     <script type="text/javascript"& ...

  5. 您厉害您赚得多:聪明投资者的聊天记录,雪球CEO的21条投资理念

    3星|<您厉害您赚得多>:雪球创始人的投资理念.原则.技巧,及其在雪球上跟一些用户的互动的内容 作者是雪球创始人.CEO,全书基本是作者的一些投资理念+作者在雪球上跟用户的互动的内容,还有 ...

  6. (转)postgis常用函数介绍(一)

    http://blog.csdn.net/gisshixisheng/article/details/47701237 概述: 在进行地理信息系统开发的过程中,常用的空间数据库有esri的sde,po ...

  7. eclipse版本和jdk的版本兼容问题

    eclipse也是有版本的,当版本过低时,无法兼容高版本的jdk 项目中用的是jdk1.8,但是低版本的eclipse只能选到jdk1.7,导致java文件在编译的过程中,不识别1.8版本jdk的语法 ...

  8. if判断,while循环,for循环

    if判断 if判断其实就是让计算机模拟人的判断 if if 条件: 代码1 代码2 代码3 ... # 代码块(同一缩进级别的代码,例如代码1.代码2和代码3是相同缩进的代码,这三个代码组合在一起就是 ...

  9. php连接数据库的两种方式

    一.mysqli方式连接数据库 $mysql_conf = array( 'host' => 'localhost:3306', 'db' => 'ssql', 'db_user' =&g ...

  10. 数组题汇总(python3)

    题目主要来自<剑指offer>和LeetCode,用python3来写的代码. 1.二维数组的查找: 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列 ...