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/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 并查集的更多相关文章
- 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 ...
- [CF#250 Div.2 D]The Child and Zoo(并查集)
题目:http://codeforces.com/problemset/problem/437/D 题意:有n个点,m条边的无向图,保证所有点都能互通,n,m<=10^5 每个点都有权值,每条边 ...
- 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 ...
- 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 ...
- 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 ...
- 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/ ...
- 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, ...
- Codeforces Round #250 (Div. 2)—A. The Child and Homework
好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人 被HACK 1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...
- 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 ...
随机推荐
- Android 如何使用juv-rtmp-client.jar向Red5服务器发布实时视频数据
使用juv-client-client.jar主要是尽快地完成毕业设计里面手机端向网页端发送实时视频的功能,由于实习和做毕业设计的时间冲突,因此完成毕业设计只花了1个多月时间. (万恶的形式主义,论文 ...
- 正在连接...ORA-12541: TNS: 无监听程序
明明已经在Net Configuration Assistant中配置过监听程序并启动过.但在测试本地网络服务名配置中扔提示以上错误"正在连接...ORA-12541: TNS: 无监听程序 ...
- 自定义控件:抽屉SlidingDrawer——wrap_content非全屏
android:allowSingleTap 指示抽屉是否可以打开/通过手柄上的一个水龙头关闭. android:animateOnClick 表示所述抽屉是否应该打开/与当用户点击手柄动画关闭 ...
- HDU 4911 Inversion
http://acm.hdu.edu.cn/showproblem.php?pid=4911 归并排序求逆对数. Inversion Time Limit: 2000/1000 MS (Java/ ...
- Python字典增删操作技巧简述
Python编程语言是一款比较容易学习的计算机通用型语言.对于初学者来说,首先需要掌握的就是其中的一些基础应用.比如今天我们为大家介绍的Python字典的相关操作,就是我们在学习过程中需要熟练掌握的技 ...
- 浏览器URL传参最大长度问题
这几天为解决一个BUG头疼了一段时间,BUG现象如下: 一个选择人员的选择控件,当选择多个人时(50多个的时候),返回没有错误现象,而再一次打开的时候就报404错误.看到这个错误非常纳闷,无法下手,只 ...
- List转换成Json、对象集合转换Json等
#region List转换成Json /// <summary> /// List转换成Json /// </summary> public static string Li ...
- support vector regression与 kernel ridge regression
前一篇,我们将SVM与logistic regression联系起来,这一次我们将SVM与ridge regression(之前的linear regression)联系起来. (一)kernel r ...
- 线性方法用于Binary clssification
到现在,我们已经学过三种线性方法:linear classification.Linear Regression.logistic Regression.这三种方法的核心都是,不同点在于:最小化的er ...
- MFC学习知识点20160715
1. sizeof() :返回所查询目标所占用字节数 _countof() :返回所查询目标所含有元素个数 _countof 是 C++中计算一个固定大小数组长度的宏,比如: T arr[10] ...