CF437D(The Child and Zoo)最小生成树
题目:
2 seconds
256 megabytes
standard input
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?
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 a real number — the value of
.
The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
4 3
10 20 30 40
1 3
2 3
4 3
16.666667
3 3
10 20 30
1 2
2 3
3 1
13.333333
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
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)最小生成树的更多相关文章
- cf437D The Child and Zoo
D. The Child and Zoo time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- 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/ ...
- Codeforces 437D The Child and Zoo(贪心+并查集)
题目链接:Codeforces 437D The Child and Zoo 题目大意:小孩子去參观动物园,动物园分非常多个区,每一个区有若干种动物,拥有的动物种数作为该区的权值.然后有m条路,每条路 ...
- Codeforces 437 D. The Child and Zoo 并查集
题目链接:D. The Child and Zoo 题意: 题意比较难懂,是指给出n个点并给出这些点的权值,再给出m条边.每条边的权值为该条路连接的两个区中权值较小的一个.如果两个区没有直接连接,那么 ...
- 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 ...
- Codeforces D - The Child and Zoo
D - The Child and Zoo 思路: 并查集+贪心 每条边的权值可以用min(a[u],a[v])来表示,然后按边的权值从大到小排序 然后用并查集从大的边开始合并,因为你要合并的这两个联 ...
- 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 ...
- Codeforces 437D The Child and Zoo(并查集)
Codeforces 437D The Child and Zoo 题目大意: 有一张连通图,每个点有对应的值.定义从p点走向q点的其中一条路径的花费为途径点的最小值.定义f(p,q)为从点p走向点q ...
- 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. ...
随机推荐
- Java--日期的使用
Date 类: 最基础的日期时间类,返回一个相对日期的毫秒数.精确到毫秒,但不支持日期的国际化和分时区显示. Calender类: 相对于Date更加强大的时间类,是抽象类,提供了常规的日期修改功能和 ...
- Actor::updateMassFromShapes
unity报错Actor::updateMassFromShapes: Compute mesh inertia tensor failed for one of the actor's mesh s ...
- Eclipse中JBOSS5.1无法启动的问题解决办法
今天在Eclipse中启动JBoss 5.1时遇到这样的一个错误: …… ERROR [AbstractKernelController] Error installing to Instantiat ...
- SQL Server 基础 04 函数与分组查询数据
函数与分组查询数据 系统函数分 聚合函数.数据类型转换函数.日期函数.数学函数 . . . 1. 聚合函数 主要是对一组值进行计算,然后返回一个值. 聚合函数包括 sum(求和).avg(求平均值). ...
- C-最长回文子串(2)
在上一篇的文章中说到了,最长回文子串的问题,并且提到了基本的解决办法,即暴力求解法.效率O(N^3) 中心法求最长回文子串 我们知道回文字符串是以字符串中心对称的,如abba以及aba等.一个更好的办 ...
- shell登录模式及其相应配置文件(转)
参考<linux命令.编辑器与shell编程>(清华大学出版社) 当启动shell时,它将运行启动文件来初始化自己.具体运行哪个文件取决于该shell是登陆shell还是非登陆shell的 ...
- 开源OCR光学字符识别
纸张在 许多地方已日益失宠,无纸化办公谈论40多年,办公环境正限制纸山的生成.而过去几年,无纸化办公的概念发生了显着的转变.在计算机软件的帮助 下,包含大量重要管理数据和资讯的文档可以更方便的以电子形 ...
- POJ 2455 网络流 基础题 二分+网络流 dicnic 以及 sap算法
Secret Milking Machine Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8189 Accepted: ...
- 在程序中,你敢怎样使用“goto”语句!
用goto是一个个人爱好的问题.“我”的意见是,十个goto中有九个可以用相应的结构化结构来替换.在那些简单情形下,你可以完全替换掉goto,在复杂的情况下,十个中也有九个可以不用:你可以把部分代码写 ...
- UVA 10581 - Partitioning for fun and profit(数论递推)
10581 - Partitioning for fun and profit 题目链接 题意:给定m, n,表示分配给n个格子,分配m个数字进去,每一个格子最少1,而且序列要是递增的,问第k个字典序 ...