HDU 2736 Average Distance

Source: Internet
Author: User

Transmission Door

Average distance

Time limit:1000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 682 Accepted Submission (s): 244
Special Judge


Problem Descriptiongiven a tree, calculate the average distance between, vertices in the tree. For example, the average distance between and the vertices in the following tree are (d01 + d02 + d03 + d04 + D12 +d13 +d14 +d2 3 +d24 +d34)/10 = (6+3+7+9+9+13+15+10+12+2)/10 = 8.6.



Inputon the first line an integer t (1 <= t <=): The number of test cases. Then to each test case:

One line with a integer n (2 <= n <=): The number of nodes in the tree. The nodes is numbered from 0 to n-1.

N-1 lines, each with three integers a (0 <= a < n), B (0 <= b < n) and D (1 <= d <= 1 000). There is an edge between the nodes with numbers a and b of length d. The resulting graph would be a tree.

Outputfor each testcase:

One line with the average distance between and the vertices. This value should has either an absolute or a relative error of in most 10-6

Sample Input150 1 60 2 30 3 73 4 2

Sample Output8.6

Source bapc2007

Recommendlcy | We have carefully selected several similar problems for you:2378 2379 2377 2380 2381

Ah, thinking of complex, in fact, quite simple:

Turn the puzzle:

Citation: If the brute force enumeration is two points, then the distance is obviously timed out. Switching ideas, we can on each side, to ask all possible paths through this side of the number of times: Set the end of this side of the points are a and B, then the number of times the edge is a*b, it is the total distance and contribution is (a*b* this side length). We sum up the contribution of all sides, divided by the total number of paths N (N-1)/2, which is the last request.

The calculation of the number of points at each end of each edge can actually be resolved with Dfs one time. Whichever is the root, in the DFS process, for each point K records its subtree contains the number of points (including its own), the points is a[k], then K on the father side of the point is n-a[k]. This statistic can be performed concurrently with traversal. Therefore, the time complexity is O (n).

16447376 2016-03-06 09:40:59 Accepted 2376 312MS 4540K 2093 B C++ Czy

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <stack>6#include <cctype>7#include <vector>8#include <cmath>9#include <map>Ten#include <queue> One  A #definell Long Long - #defineN 10005 - #defineEPS 1e-8 the  - using namespacestd; -  - intT; + DoubleTot[n]; - DoubleCou[n]; + Doublenum; A intN; at  - structPP - { -     intto ; -     DoubleVal; - }; in  -Vector<pp>Edge[n]; to  + PP te; -  the voidAdd_adge (int  from,intTo,Doubleval) * { $te.to = To;te.val =Val;Panax Notoginsengedge[ from].push_back (TE); -Te.to = from; te.val =Val; the edge[to].push_back (TE); + } A  the voidDfsintNowintFA) + { -Unsignedinti; $ PP NT; $Cou[now] =1; -      for(i =0; i < edge[now].size (); i++){ -Nt.to =edge[now][i].to; theNt.val =Edge[now][i].val; -         if(Nt.to = =FA) {Wuyi             Continue; the         } - DFS (nt.to,now); WuTot[now] = Tot[now] + tot[nt.to] + (n-cou[nt.to]) * cou[nt.to] *Nt.val; -Cou[now] = Cou[now] +cou[nt.to]; About         //printf ("now =%d i=%d to=%d tot=%.6f cou=%.6lf\n", Now,i,nt.to,tot[now],cou[now]); $     } -     //printf ("i=%d tot=%.6f cou=%.6f\n", Now,tot[now],cou[now]); - } -  A intMain () + { the     //freopen ("In.txt", "R", stdin); -scanf"%d",&T); $     inti; the     int  from, to; the     DoubleVal; the      for(intCcnt=1; ccnt<=t;ccnt++){ the     //while (scanf ("%lf%lf%lf%lf", &a[0],&a[1],&a[2],&a[3])!=eof) { -scanf"%d",&n); inmemset (Tot,0,sizeof(tot)); thememset (Cou,0,sizeof(Cou)); thenum =1.0*n* (n1)/2; About          for(i=0; i<=n;i++){ the edge[i].clear (); the         } the          for(i=1; i<=n-1; i++){ +scanf"%D%D%LF",& from,&to,&val); -Add_adge ( from, to,val); the         }BayiDfs0,-1); the         //For (i=0;i<n;i++) { the          //for (int j=0;j<edge[i].size (); j + +) { -          //printf ("i=%d to=%d val=%.6lf\n", i,edge[i][j].to,edge[i][j].val); -          //   } the        // } the          for(i=0; i<n;i++){ the             //printf ("i=%d tot=%.6f cou=%.6f\n", I,tot[i],cou[i]); the         } -printf"%lf\n", tot[0]/num); the     } the     return 0; the}

HDU 2736 Average Distance

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.