437D (The Child and Zoo) Minimum Spanning Tree

Source: Internet
Author: User

Question:

D. The Child and Zootime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Of course our child likes walking in a zoo. The zoo hasNAreas, that are numbered from 1N.I-Th area containsAIAnimals in it. Also there areMRoads 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 areaPTo areaQ. Firstly he considers all the simple routes fromPToQ. 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 writeach numbersF(P, Bytes,Q). Finally, the child chooses one of the routes for which he writes down the valueF(P, Bytes,Q).

After the child has visited the zoo, he thinks about the question: what is the average valueF(P, Bytes,Q) For all pairsP, Bytes,Q(P  =Q)? Can you answer his question?

Input

The first line contains two integersNAndM(2 cores ≤ CoresNMemory ≤ memory 105; 0 memory ≤ memoryMLimit ≤0000105). The second line containsNIntegers:A1, bytes,A2, middle..., middle ,...,AN(0 bytes ≤ bytesAIBandwidth ≤ bandwidth 105). Then followMLines, each line contains two integersXIAndYI(1 digit ≤ DigitXI, Bytes,YILimit ≤ limitN;XI  =YI), Denoting the road between areasXIAndYI.

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

Output

Output a real number-the value.

The answer will be considered correct if its relative or absolute error doesn' t exceed 10 seconds-limit 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
Solution:

Defines that the weight of each edge is equal to the vertex weight of a smaller vertex in two vertices. Then, query the set to find the maximum spanning tree. The edges with the largest edge weight each time. Then the smallest combination is the combination of all vertices at both ends.

Code:

/******************************************************* 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-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<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;}


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.