Minimum spanning tree Kruskal hdu 5723 abandoned country

Source: Internet
Author: User

Title Link: Hdu 5723 abandoned country

The main topic: N points, M-bar, first form a minimum spanning tree, and then the minimum spanning tree to find the path length between any two points, and to expect

/************************************************************** problem:hdu 5723 user:youmi language:c++ R esult:accepted time:2932ms memory:22396k Solution: First notice that any two sides of the Benquan is not the same, so that the smallest spanning tree is unique, since the smallest spanning tree is unique,
Then the expectation is actually the only one, there is no minimum expectation. After finding the smallest spanning tree, the next question can be converted to
The average of distances between any two points is calculated on the minimum spanning tree, and for each edge, the number of times that all paths are used for this edge is
is the product of the number of points on both sides of the edge. Then the total contribution of this side is the number of times * edge right. Finally get the sum of the contributions of all sides divided by the total road
Number of diameters N (n-1)/2n∗ (n−1)/2 is the answer. Can be onon to find out. Either take a bit for root DFS and record its subtree package for each point II
The number of points included (including itself), Sum[i]sum[i], then the father side of the II points is n-sum[i]n−sum[i].
Walk the side of the statistics on the line. ****************************************************************///#pragma COMMENT (linker, "/stack:1024000000,1024000000")//#include <bits/stdc++.h>#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<stack>#include<Set>#include<sstream>#include<cmath>#include<queue>#include<deque>#include<string>#include<vector>#defineZeros (a) memset (A,0,sizeof (a))#defineOnes (a) memset (A,-1,sizeof (a))#defineSC (a) scanf ("%d", &a)#defineSC2 (A, b) scanf ("%d%d", &a,&b)#defineSC3 (a,b,c) scanf ("%d%d%d", &a,&b,&c)#defineSCS (a) scanf ("%s", a)#defineSclld (a) scanf ("%i64d", &a)#definePT (a) printf ("%d\n", a)#definePtlld (a) printf ("%i64d\n", a)#defineRep (i,from,to) for (int i=from;i<=to;i++)#defineIrep (I,to,from) for (int i=to;i>=from;i--)#defineMax (a) (a) > (b)? ( A):(B))#defineMin (a) < (b) ( A):(B))#defineLson (step<<1)#defineRson (lson+1)#defineEPS 1e-6#defineOO 0x3fffffff#defineTEST cout<< "*************************" <<endlConst DoublePi=4*atan (1.0);using namespaceStd;typedefLong Longll;template<classT> InlinevoidRead (T &N) { CharCintFlag =1; for(c = GetChar ();! (c >='0'&& C <='9'|| c = ='-'); c = GetChar ());if(c = ='-') flag =-1, n =0;Elsen = C-'0'; for(c = GetChar (); C >='0'&& C <='9'; c = GetChar ()) n = n *Ten+ C-'0'; N *=Flag;}intPow (int Base, ll N,intmo) { if(n = =0)return 1; if(n = =1)return Base%mo; intTMP = Pow (Base, N >>1, MO); TMP= (LL) TMP * TMP%mo; if(N &1) TMP = (LL) TMP *Base%mo; returntmp;}//***************************intn,m;Const intmaxn=200000+Ten;intT,HEAD[MAXN],FA[MAXN],VIS[MAXN];structedge{intu,v; ll W; voidInitint_u,int_v,ll _w) {u=_u,v=_v,w=_w; }}EDGE[MAXN*Ten];intGet_f (intu) { returnu==fa[u]?u:fa[u]=Get_f (Fa[u]);}BOOLCMP (Edge A,edge b) {returna.w<B.W;}structside{intV,next; ll W;} E[MAXN*Ten];voidinit () {T=0; Ones (head); Zeros (VIS);}voidBuildintUintv,ll W) {E[T].V=v; E[T].W=W; E[t].next=Head[u]; Head[u]=t++;}voidKruskal () {Sort (Edge+1, edge+1+m,cmp); Rep (I,0, N) fa[i]=i; ll Sum=0; Rep (I,1, M) { intf1=Get_f (EDGE[I].U); intF2=Get_f (EDGE[I].V); if(f1!=F2) {FA[F2]=F1; Build (EDGE[I].U,EDGE[I].V,EDGE[I].W); Build (EDGE[I].V,EDGE[I].U,EDGE[I].W); Sum+=EDGE[I].W; //j + +;}} printf ("%i64d", sum);}DoubleTt;ll DFS (intu) {ll num=1, temp=0; Vis[u]=1; for(intI=head[u];~i;i=E[i].next) { intv=e[i].v; ll W=E[I].W; if(!Vis[v]) {Temp=Dfs (v); TT+=1.0*w* (1.0* (n-temp) *temp); Num+=temp; } } returnnum;}voidSovle () {TT=0; DFS (1); TT=tt*2.0/(1.0*n* (n1)); printf ("%.2lf\n", TT);}intMain () {#ifndef Online_judge freopen ("In.txt","R", stdin); #endif intt_t; scanf ("%d",&t_t); for(intKase=1; kase<=t_t;kase++) {SC (n); SC (m); Init (); intu,v; ll W; Rep (I,1, M) {SC2 (u,v); Sclld (w); Edge[i].init (U,V,W); } Kruskal (); Sovle (); }}

Minimum spanning tree Kruskal hdu 5723 abandoned country

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.