Codeforce 437 D the child and Zoo

Source: Internet
Author: User

Test instructions: Given n weighted points, M-bar, any two points between the weights of a path is the minimum weight of all points on this path, the weights between any two points are the largest of the weights of all the paths between them.


Practice: Consider and check set, is the first to sort all the edges in descending order, and then start to set up and check set, to join the two points already in the same set, then has contributed to the ANS, no tube, if not, then merge, using the multiplication principle that is two elements of the set number of combinations to figure out how many paths they can form , the weights of these paths are the minimum weights for these two points, and then contribute to ans, so that the answer can be calculated. It's still very interesting.


#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;int fa[100010],num[100010],val[100010];int dfs (int v) {return fa[v]=v==fa[ V]?v:dfs (Fa[v]);} struct Edge{int U,v;bool operator < (Edge one) Const{return min (Val[u],val[v]) >min (VAL[ONE.U],VAL[ONE.V]);}} Edge[100010];int Main () {int n,m;cin>>n>>m;for (int i=1;i<=n;i++) cin>>val[i];for (int i=0;i<m ; i++) Cin>>edge[i].u>>edge[i].v;sort (edge,edge+m); for (int i=1;i<=n;i++) {fa[i]=i;num[i]=1;} Double ans=0;for (int i=0;i<m;i++) {int A=dfs (EDGE[I].U), B=dfs (EDGE[I].V), if (a!=b) {ans+=double (Num[a]) *num[b]* Min (val[edge[i].u],val[edge[i].v]); fa[a]=b;num[b]+=num[a];}} ans=ans*2/n/(n-1);p rintf ("%.5f\n", ans);} 

Time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Of course our child likes walking in a zoo. The zoo hasNAreas, that is numbered from1ToN. TheI-th area contains ai Animals in it. Also there ismRoads in the zoo, and each road connects the distinct areas. Naturally the zoo is a connected, so can reach any area of the zoo from any other area using the roads.

We are very smart. Imagine the want to goPTo areaQ. Firstly he considers all the simple routes fromPToQ. For each route the child writes down the number, which 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 have 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 integersNandm(2?≤? n? ≤?105 ;0?≤? m. ≤?105 ). The second line containsNIntegers: a1,? a 2,?...,? a N (0?≤? a i? ≤?105 ). Then followmLines, each line contains the integers xi and yi (1?≤? x i,? y i? ≤? N ; xi? ≠? y I ), denoting the road between areas xi and yi .

All roads am bidirectional, each pair of areas is connected by at the most one road.

Output

Output a real number-the value of.

The answer would be a considered correct if its relative or absolute error doesn ' t exceed?-? 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
Note

Consider the first sample. There is 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 is symmetrical to the above. The average is.

Con Sider 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 is symmetrical to the above. The average is.





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforce 437 D the child and Zoo

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.