Minimum spanning tree + minimum expectation of any two points on the tree

Source: Internet
Author: User

Reference Blog

Test instructions: There are n cities, m need to be rebuilt road, each road has the right value and different, let you make all the city (direct or indirect) situation, find the weight and the smallest of the kind of plan, and then ask you from this scheme arbitrarily choose two points, ask the distance between two points the minimum expected value is how much.
Problem-Solving ideas: let you find the right value and the smallest, of course, is the smallest generation tree ah, but the smallest expectations behind a bit of trouble, but we can carefully analyze the weight of each road is not the same as the smallest spanning tree is the only (specific proof of their own Baidu), then the generation of trees only, how to ask for minimum expectations Minimum expectation? This is actually the question person to bluff you, a tree on any two points between the distance is the only, so there is no minimum maximum, just find out the distance between any 22 points on the line, and then add up all the distance divided by N (n-1)/2 on the line, Then the question is converted to the sum of the distances between any two points in a tree without roots, and we can get the sum of all distances equal to the weights of each edge multiplied by the number of occurrences of this edge, and the weights of each edge we know, we now want to know how many times each edge appears, and we can find that The number of occurrences of each edge is equal to the product of the number of vertices on each side of the two endpoints of the edge. Knowing this, we can directly DFS, arbitrarily select a vertex as the root (I choose 1), so that dp[u] is equal to the number of vertices of a subtree rooted in U, and then enumerate each edge, if the two vertices of an edge is X1 , x2, then x1,x2 either X1 is x2 father, or X2 is x1 father, then who is who's father, very simple, compared dp[x1],dp[x2], the small one must be the child, all the side of the edge of the vertex number is v1 = min (dp[x1],dp[x2]), So what is the number of vertices on the other side? Very simple, the total number is n, all the other side vertex number v2 = N-v1, all this edge occurrences of the number of V1V2, pay attention to the final expectation divided by N (n-1)/2 must be n,n-1 into a long long after the request, I am here wa not several times.

Code:

#include <stdio.h>#include<iostream>#include<algorithm>#include<vector>#include<string.h>typedefLong Longll;using namespacestd;Const intmax_=1e6+5;Const intmax_n=1e5+5;structedge{intform; intto ; ll W;};structEdge Tu[max_],tree[max_n];intDp[max_n];//Record the child tree node of the package itselfintFa[max_n];//parent NodeBOOLVis[max_n];//Tag Arrayvector<int>g[max_n];//Record child nodesintTot1,tot2;voidAdd_edge1 (intXintY,ll W)//Building Map{tu[tot1].form=x; Tu[tot1].to=y; TU[TOT1++].w=W;}voidAdd_edge2 (intXintY,ll W)//Achievements{tree[tot2].form=x; Tree[tot2].to=y; Tree[tot2++].w=W;}BOOLCMP (Edge A,edge B)//Sort{    returna.w<B.W;}intFIND_FA (intX//and check Set{    if(x==Fa[x])returnx; Else        returnfa[x]=Find_fa (fa[x]);} ll Kruskal (intN//minimum spanning tree algorithm{sort (tu,tu+tot1,cmp); intans=0; ll Ant=0;  for(intI=0; i<tot1;i++)    {        intv=Tu[i].form; intu=tu[i].to; ll W=TU[I].W; intf1=Find_fa (v); intF2=find_fa (U); if(f1!=F2) {FA[F1]=F2; Ant+=W;            Add_edge2 (U,V,W); Ans++;            G[u].push_back (v);        G[v].push_back (U); }        if(ans==n-1)        {        returnAnt; }    }}voidDfsintX//DFS Find child nodes of tree{vis[x]=true; DP[X]=1;  for(intI=0; I<g[x].size (); i++)    {        inty=G[x][i]; if(!Vis[y])            {dfs (y); DP[X]+=Dp[y]; }    }}voidInitintN//Initialize{memset (Vis,0,sizeof(VIS)); Memset (DP,0,sizeof(DP));  for(intI=1; i<=n;i++) {Fa[i]=i;    G[i].clear (); } tot1=0; Tot2=0;}intMain () {intT; scanf ("%d",&t);  while(t--)    {        intn,m; scanf ("%d%d",&n,&m); ll ans=0;        Init (n); if(m==0) {printf ("0 0.00\n"); Continue; }         while(m--)        {            intx, y;            ll W; scanf ("%d%d%lld",&x,&y,&W);        Add_edge1 (X,Y,W); } ll sum=Kruskal (n); DFS (1);  for(intI=0; i<tot2;i++)        {            intu=Tree[i].form; intv=tree[i].to; ll W=TREE[I].W; intk=min (dp[v],dp[u]); Ans+=w* (n-k) *K; //cout<<ans<<endl;        }       //printf ("%.2f\n", 1.0* (n-1) *N/2);        Doubleexpe=ans*1.0/(1.0*n* (n1)/2); printf ("%lld%.2lf\n", SUM,EXPE); }}

Minimum spanning tree + minimum expectation of any two points on the tree

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.