Valley 1351 Joint Weights

Source: Internet
Author: User

Title Description

undirected Connectivity Graph G has n points, n-1 edges. Points are numbered from 1 to N, and the weights of the points numbered I are w i and each edge is 1 in length. The distance between two points (U, v) on the figure is defined as the shortest distance from the U point to the V point. For point Pairs (U, v) on Figure G, if their distances are 2, they create a Wu
The joint weight value of the XWV.
What is the maximum number of joint weights in an ordered point pair that generates a joint weight on figure g? What is the sum of all the joint weights?

Input/output format

Input format:

The input file name is link. In.
The first line consists of 1 integer n.
Next n-1 lines, each line contains 2 positive integers separated by spaces U, V, indicating that the number is U and the point numbered V has an edge connected.
The last 1 rows, which contain n positive integers, are separated by a space between each of the two positive integers, where the I integer indicates that the weights of the points numbered I on Figure G are w i.

Output format:

The output file is named link. Out.
Outputs a total of 1 rows, containing 2 integers, separated by a space, followed by the maximum value of the union weights on Figure g
And the sum of all joint weights. Since the sum of all the joint weights is likely to be large, it is necessary to output 10007 of the residual value.

Input/Output sample

Input Sample # #:

5  1 2  2 4  4 5  

Sample # # of output:

20 74
Description


In this example, the graph as shown above, the distance is 2 of the ordered point pairs have (1,3), (2,4), (3,1), (3,5), (4,2), (5,3).
The joint weights are 2, 15, 2, 20, 15, 20, respectively. The largest of these is 20, and the sum is 74.

"Data description"
For 30% of data, 1 < n≤100;
For 60% of data, 1 < n≤2000;
For 100% of data, 1 < n≤200, 0 < WI≤10, 000.

"Problem-solving ideas"

The most important part of the problem is a formula

Example 3 Number of a,b,c, then its weight is AB,AC,BA,BC,CA,CB, mention common divisor into 2 (AB+AC+BC), you can save a lot of time, by the way in the process of the record with a point connected to the maximum value and the second large value, then multiply, you can get the maximum value, By the way, you can get the AC off.

Code to write a little bit more complex, using a dynamic array, we P party also has a dynamic array!!!

 ProgramT2; Constmo=10007; varF:Array  of Array  ofLongint;      N,i,j,k,max,mid:longint;      Ans:int64; W,max1,max2,sum,a,b:Array[1..200000] ofLongint; beginread (n); SetLength (F,n+1,1);  fori:=1  toN Do      beginF[i,0]:=1; End;  fori:=1  toN-1  Do      beginread (a[i],b[i]); End;  fori:=1  toN Doread (w[i]);  fori:=1  toN-1  Do      beginSetLength (F[a[i]],f[a[i],0]+1);          Inc (Sum[a[i]],w[b[i]);          Inc (Sum[b[i]],w[a[i]); F[a[i],f[a[i],0]]:=B[i]; ifMax1[a[i]]<=w[b[i]] Then          beginMax2[a[i]]:=Max1[a[i]]; Max1[a[i]]:=W[b[i]]; End; if(Max2[a[i]]<=w[b[i]]) and(Max1[a[i]]>w[b[i]]) Then          beginMax2[a[i]]:=W[b[i]]; End; Inc (F[a[i),0]); SetLength (F[b[i]],f[b[i],0]+1); F[b[i],f[b[i],0]]:=A[i]; ifMax1[b[i]]<=w[a[i]] Then          beginMax2[b[i]]:=Max1[b[i]]; Max1[b[i]]:=W[a[i]]; End; if(Max2[b[i]]<=w[a[i]]) and(Max1[b[i]]>w[a[i]]) Then          beginMax2[b[i]]:=W[a[i]]; End; Inc (F[b[i),0]); End;  fori:=1  toN Do ifMax1[i]*max2[i]>max Thenmax:=max1[i]*Max2[i];  fori:=1  toN Do           forj:=1  toF[i,0]-1  Do          beginans:= (ans+ (sum[i]-w[f[i,j])MoDmo* (W[f[i,j])MoDMO))MoDMoMoDmo; End; Writeln (Max,' ', (ans)); End.

Valley 1351 Joint Weights

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.