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 ...
随机推荐
- liux vim命令
命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...
- 【大数模板】C++大数类 大数模板
分别使用C++中的运算符重载的方法来实现大数之间的数学运算,包括加法.减法.乘法.除法.n次方.取模.大小比较.赋值以及输入流.输出流的重载. 感觉很麻烦... [代码] #include<io ...
- solr4.5配置中文分词器mmseg4j
solr4.x虽然提供了分词器,但不太适合对中文的分词,给大家推荐一个中文分词器mmseg4j mmseg4j的下载地址:https://code.google.com/p/mmseg4j/ 通过以下 ...
- MySQL基础之第5章 操作数据库
假设已经登录 mysql-h localhost -uroot -proot 5.1.显示.创建.删除数据库 show databases; 显示所有的数据库 create database ...
- K2 blackpearl 流程开发(二)
转:http://blog.csdn.net/gxiangzi/article/details/8444590 本来想一篇文章把流程开发介绍完的,后来发现实在是太多了,只好分成两部分了.上一篇很简单的 ...
- Andorid-Fragment生命周期
官网帮助文档链接: http://developer.android.com/guide/components/fragments.html Fragment的生命周期: Fragment与Activ ...
- 【转】使用NetBeans和Eclipse开发PHP应用程序
[51CTO独家特稿]各位用户如果单独看NetBeans和Eclipse的市场占有率,你可能会认为使用其中任何一种IDE开发PHP应用程序都没有 问题,例如: 1.NetBeans:一款开源的集成开发 ...
- Spring的事务传播属性,数据库的隔离级别
Spring事务的传播属性 REQUIRED 业务方法需要在一个事务中运行,如果方法运行时,已处在一个事务中,那么就加入该事务,否则自己创建一个新的事务.这是spring默认的传播行为. SUPPO ...
- kettle实现文本文件数据抽取方法
KETTLE做调度的思路是,把一个有特定格式的的文本文件,写入ORACLE数据库表, 具体方法见如下操作: 首先来看下文本文件的内容: 1|test1 2|test2 3|test3 通过|进行分割的 ...
- bjfu1284 判别正则表达式
做解析器做得多的我,一上来就觉得要写解析器,麻烦,于是想偷懒用java的正则表达式类Pattern直接进行判断,结果wa了,原因是这题要求的正则表达式只是真正正则表达式的一个子集.比如|12是合法正则 ...