博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF437D(The Child and Zoo)最小生成树
阅读量:6887 次
发布时间:2019-06-27

本文共 2724 字,大约阅读时间需要 9 分钟。

题目:

D. The Child and Zoo
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 ≤ 1050 ≤ 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 310 20 30 401 32 34 3
output
16.666667
input
3 310 20 301 22 33 1
output
13.333333
input
7 840 20 10 30 20 50 401 22 33 44 55 66 71 45 7
output
18.571429
解法:

定义每一个边的权值等于两个点中较小一个点的点权。

然后并查集求最大生成树。每次从加边权最大的边。然后以此最小的组合就是两头全部点的相互组合。 

代码:

/******************************************************* author:xiefubao*******************************************************/#pragma comment(linker, "/STACK:102400000,102400000")#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//freopen ("in.txt" , "r" , stdin);using namespace std;#define eps 1e-8const 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

转载地址:http://nrtbl.baihongyu.com/

你可能感兴趣的文章
剑指offer---12-**--数值的整数次方
查看>>
PAT - L2-010. 排座位(并查集)
查看>>
Python 学习笔记 - 线程(线程锁,信标,事件和条件)
查看>>
大数据技术服务商个推获4亿人民币D轮融资
查看>>
Git的详细使用教程
查看>>
iOS实现类似苹果手机原生的锁屏界面(数字密码)
查看>>
[vue] 表单输入格式化,中文输入法异常
查看>>
Observer观察者模式与OCP开放-封闭原则
查看>>
如何搭建高级工程师知识框架?推荐两种方式
查看>>
BAT的医疗春秋大梦
查看>>
Pulsar本地单机(伪)集群 (裸机安装与docker方式安装) 2.2.0
查看>>
利用H5的css3制作动画
查看>>
Android View 事件分发源码分析
查看>>
vue 2.0 - props
查看>>
RustCon Asia 实录 | Rust 在国内某视频网站的应用
查看>>
Vue遇上Analytics
查看>>
修改max_allowed_packet(允许执行的sql最大长度)
查看>>
node js 处理时间分析
查看>>
判断数据库、表和字段是否存在
查看>>
新手安装postgreSQL后无法连接服务器
查看>>