Codeforces Round #250 (Div. 1)B(排序+并查集)

B. 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?≤?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?≤?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 3
10 20 30 40
1 3
2 3
4 3

output

16.666667

input

3 3
10 20 30
1 2
2 3
3 1

output

13.333333

input

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

output

18.571429

Note

Consider the first sample. There are 12 possible situations:

  • p?=?1,?q?=?3,?f(p,?q)?=?10.
  • p?=?2,?q?=?3,?f(p,?q)?=?20.
  • p?=?4,?q?=?3,?f(p,?q)?=?30.
  • p?=?1,?q?=?2,?f(p,?q)?=?10.
  • p?=?2,?q?=?4,?f(p,?q)?=?20.
  • p?=?4,?q?=?1,?f(p,?q)?=?10.

Another 6 cases are symmetrical to the above. The average is .

Consider the second sample. There are 6 possible situations:

  • p?=?1,?q?=?2,?f(p,?q)?=?10.
  • p?=?2,?q?=?3,?f(p,?q)?=?20.
  • p?=?1,?q?=?3,?f(p,?q)?=?10.

Another 3 cases are symmetrical to the above. The average is .

题意:RT

思路:先将点按点权降序排序,然后一个一个点遍历

假设当前点为i, 则只需考虑i和前面已经遍历过的点,如果存在点j和点v不在同一个连通分量(可以用并查集)

且j和v都能到达i,则f(j,v)一定等于i的权值,做完以后把i加入并查集即可,并查集还需要维护集合的大小

时间: 2024-08-10 19:11:36

Codeforces Round #250 (Div. 1)B(排序+并查集)的相关文章

Codeforces Round #423 (Div. 2) C 思维,并查集 或 线段树 D 树构造,水

Codeforces Round #423 (Div. 2, rated, based on VK Cup Finals) C. String Reconstruction   思维,并查集 或 线段树 题意:一个字符串被删除了,但给出 n条信息,要还原出可能的字典序最小的字符串.信息有:字符串ti,ki个位置xi,表明原本的字符串在xi位置是以字符串ti开头的. tags:惨遭 fst,一开始把所有字符串都存下来,排序做的,结果爆内存了.. 方法1: 考虑并查集,对于字符串 ti,在位置xi,

Codeforces Round #623 (Div. 2) D.Recommendations 并查集

ABC实在是没什么好说的,但是D题真的太妙了,详细的说一下吧 首先思路是对于a相等的分类,假设有n个,则肯定要把n-1个都增加,因为a都是相等的,所以肯定是增加t小的分类,也就是说每次都能处理一个分类,复杂度是O(n^2),这个思路很好写,优先队列随便搞一下就行了,但是题目中N = 2 * 1e5,n^2肯定是不行的,然后就是这个很巧妙的方法了,cf官方的题解我没看懂...有点尴尬,它也没给标程,但是cf的题解好像不是这个方法 看了网上用并查集的方法,首先以t为关键字从大到小排序,然后用并查集维

Codeforces Round #250 (Div. 1)

这几次CF都挺惨.. A 没条边权设为两端点的最小点权,最后加起来. 数组开小,WA一次 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #

codeforces --- Round #250 (Div. 2) B. The Child and Set

<传送门> [题目大意] 给你一个sum和一个limit,现在要你在1~limit中找到一些数来使得这些数的和等于sum,如果能找到的话就输出找到的数的个数和这些数,未找到输出"-1". 比赛的时候被hack了. [题目分析] 这题需要将所有的数的lowbit先求出来,然后按照大小排序,然后从后往前判断,如果这个数小于sum那么这个数就是可以构成sum的数,选进去,完了以后判断sum的值是否为0就可以了.做题的时候没将题目理解透彻. #include<bits/std

Codeforces Round #250 (Div. 2)——The Child and Set

题目链接 题意: 给定goal和limit,求1-limit中的若干个数,每一个数最多出现一次,且这些数的lowbit()值之和等于goal,假设存在这种一些数,输出个数和每一个数:否则-1 分析: 先考虑一下比較普通的情况,给一些数,和一个goal,问时候能达到.(最好还是设这些数已经从大到小排序) 考虑能否够贪心,对于当前的数x: 1.之后的数的和能等于x,那么假设x<=goal,显然必须选x: 2.之后的数的和能等于x-1,那么同上(这个情况就是二进制的情况) 3.之后的数的和不包含上述两

codeforces --- Round #250 (Div. 2) A. The Child and Homework

<传送门> 这题就是一个坑,尼玛wa了一大片啊. 自己被hack了,比赛结束后改了又wa两次才过. [题目大意] 其实就是一个猜题小技巧(联系自己初中考试的时候怎么猜题的,这题就好理解多了).这位同学是这样来选答案的:1.如果有一些选项长度至少比其他所有的描述短两倍,或至少超过所有其他的描述的两倍,那么孩子认为这个选择很可能是正确的.2.如果正好满足以上其中一种条件(重点),这个同学就会选择它,否则就选C.给你一个选择题,让你选择出这个同学将会选择的答案. [题目分析] 首先,这个题目就是一个

Codeforces Round #250 (Div. 2) C、The Child and Toy

注意此题,每一个部分都有一个能量值v[i],他移除第i部分所需的能量是v[f[1]]+v[f[2]]+...+v[f[k]],其中f[1],f[2],...,f[k]是与i直接相连(且还未被移除)的部分的编号. 注意题目移除的都是与第i部分直接相连的部分的能量值, 将本题目简化得,只考虑两个点1和2,1和2相连,1的能量值是10,2的能量值是20, 移除绳子时,要保持能量最小,可以移除部分2,这样移除的能量就是与2相连的部分1的能量即是10: 故每次相连两部分都移除能量值大的即可 #includ

Codeforces Round #250 (Div. 1) B 并查集

坑!神坑!深坑!,WA了几十把,最终答案  (ans * 2)/(n * 1.0 * (n - 1)) 要是写成(ans * 2)/(n *(n - 1)*1.0)就是WA,不明白为啥,愤怒的我 全改成double就可以了,若前面变量用了int的 答案必须是前一种写法, 题目不是特别难,没啥思路画一画就有思路了,10^5的n去扫肯定是要超时的,那就想想一次性的10^5,发想通过m是可以的,建边,边权就是两端点中小的那个,然后对最终答案的种数进行分析,发现其实就是 每次你要连接的两块连通块的个数相

Codeforces Round #250 (Div. 1) B. The Child and Zoo(排序+并查集)(常规好题)

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