B. The Child and Zoo

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/438/problem/B

Description

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 ≤ n; xi ≠ 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 Input

4 3
10 20 30 40
1 3
2 3
4 3

Sample Output

16.666667

HINT

题意

给你一个图

然后f(p,q)表示从p到q的所有路径中,最小价值是多少

然后让你求sigma(p,q,p!=q)f(p,q) /n*(n-1)

题解:

并查集

从大往小考虑,然后加进这个图里面,在不断加点的过程中,新加入的点一定是最小的点,于是新产生的路径,这些路径的权值,就是这个点的权值

所以并查集跑一发就好啦,维护并查集的时候,顺便维护一下每个集合的个数就好了

代码

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
#define maxn 100005
int fa[maxn],a[maxn],num[maxn];
int fi(int x)
{
return fa[x]==x?x:fa[x]=fi(fa[x]);
}
struct node
{
int x,y,v;
}d[maxn];
bool cmp(node a,node b)
{
return a.v>b.v;
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
fa[i]=i;num[i]=;
}
for(int i=;i<=m;i++)
{
scanf("%d%d",&d[i].x,&d[i].y);
d[i].v = min(a[d[i].x],a[d[i].y]);
}
sort(d+,d++m,cmp);
double ans = ;
for(int i=;i<=m;i++)
{
int xx = fi(d[i].x);
int yy = fi(d[i].y);
if(xx!=yy)
{
ans += 1.0 * num[xx] * num[yy] * d[i].v;
fa[xx]=yy;
num[yy]+=num[xx];
}
}
printf("%.12lf\n",ans*/n/(n-));
}

Codeforces Round #250 (Div. 1) B. The Child and Zoo 并查集的更多相关文章

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

  2. [CF#250 Div.2 D]The Child and Zoo(并查集)

    题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...

  3. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  4. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...

  5. Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间取摸

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

  6. Codeforces Round #250 (Div. 1) A. The Child and Toy 水题

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

  7. Codeforces Round #250 (Div. 1) D. The Child and Sequence (线段树)

    题目链接:http://codeforces.com/problemset/problem/438/D 给你n个数,m个操作,1操作是查询l到r之间的和,2操作是将l到r之间大于等于x的数xor于x, ...

  8. Codeforces Round #250 (Div. 2)—A. The Child and Homework

         好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人  被HACK  1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...

  9. Codeforces Round #250 (Div. 1) D. The Child and Sequence(线段树)

    D. The Child and Sequence time limit per test 4 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. 【web】web欢迎页面执行servlet

    <!-- servlet名 --> <welcome-file-list> <welcome-file>Begin_page</welcome-file> ...

  2. C#中嵌入互操作类型的含义

    首先说一下它的含义: 1. ”嵌入互操作类型”中的嵌入就是引进.导入的意思,类似于c#中using,c中include的作用,目的是告诉编译器是否要把互操作类型引入. 2. “互操作类型”实际是指一系 ...

  3. php-PHP试题

    ylbtech-doc:php-PHP试题 PHP试题 1.A,PHP试题返回顶部 1.{PHP题目}标识符是变量的名称.PHP中的标识符用“$+变量名”来表示.标识符在PHP中遵循下列选项中的那些规 ...

  4. Dev gridControl 按回车增加一行

    将NewItemRowPosition属性设置为Top或Bottom, 在这样的新行中输入数据后,会自动添加到绑定的数据源中的, 如果你希望在按回车时焦点跳至下一列, 只需要设置GridView的Op ...

  5. http://jingyan.baidu.com/article/4dc40848e7b69bc8d946f127.html

    http://jingyan.baidu.com/article/4dc40848e7b69bc8d946f127.html

  6. [.NET源码学习]实例化Font,遭遇字体不存在的情况。

    实例化Font类时,当传入参数为不存在或未安装的字体时,Windows系统会用Microsoft Sans Serif字体替代该字体. Msdn: "For more information ...

  7. bzoj 2959 长跑(LCT+BCC+并查集)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2959 [题意] n个点,提供操作:连边,修改点权,查询自定义边的方向后起点a终点b能经 ...

  8. (四面体)CCPC网络赛 HDU5839 Special Tetrahedron

    CCPC网络赛 HDU5839 Special Tetrahedron 题意:n个点,选四个出来组成四面体,要符合四面体至少四条边相等,若四条边相等则剩下两条边不相邻,求个数 思路:枚举四面体上一条线 ...

  9. 多校5 1004 HDU5784 统计锐角三角形数目

    http://acm.hdu.edu.cn/showproblem.php?pid=5784 题意:n个点,找多少个锐角三角形数目 思路:极角排序+two pointers 当前选择的点集要倍增一倍, ...

  10. [算法] 快速排序 Quick Sort

    快速排序(Quick Sort)使用分治法策略. 它的基本思想是:选择一个基准数,通过一趟排序将要排序的数据分割成独立的两部分:其中一部分的所有数据都比另外一部分的所有数据都要小.然后,再按此方法对这 ...