Joint weights (Codevs 3728)

Source: Internet
Author: User

Description

undirected Connectivity Graph G has n points, n−1 edges. The points are numbered from 1 to N, and the weights of the points numbered I are Wi, each edge has a length of 1. The distance from the two point (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 distance is 2, then they produce a WVXWU joint weight value.

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 Format

The first line consists of 1 integer n.

Next n−1 lines, each line contains 2 positive integer u,v separated by spaces, indicating that there are edges connected between points numbered U and numbered v.

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 weight of the point numbered I on Figure G is Wi.

Output Format

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 the 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.

Sample Input
51 22 33 44 51 5 2 3 10
Sample Output
20 74
Hint

The ordered point pairs with distances of 2 have (1,3), (2,4), (3,1), (3,5), (4,2), (5,3). The joint weights were 2,15,2,20,15,20. The largest of these is 20, and the sum is 74.

For 30% of data,1<n≤100;

For 60% of data,1<n≤2000;

For 100% of data, 1<n≤200000,0<wi≤10000.

It is guaranteed that there must be an ordered point pair which can generate joint weights.

/*just started with the triple loop, gorgeous tle, 70 points (⊙o⊙)!!! Positive solution: Enumerate each point, the distance of any two points connected by this point is 2, put them all into an array, take the maximum of two number multiplication is the current optimal solution, for all points take large; As for weights and, add a mathematical Knowledge: (a+b+c) ^2=a*a+b*b+c*c+2ab+2ac+ 2BC, 2AB+2AC+2BC is the current and, for all points fetch and. */#include<cstdio>#include<iostream>#include<vector>#include<algorithm>#include<cstring>#defineM 200010using namespaceStd;vector<int>Grap[m];intN,a[m],q[m];intcmpConst intXConst inty) {    returnA[x]>a[y];}intMain () {scanf ("%d",&N);  for(intI=1; i<n;i++)    {        intx, y; scanf ("%d%d",&x,&y);        Grap[x].push_back (y);    Grap[y].push_back (x); }     for(intI=1; i<=n;i++) scanf ("%d",&A[i]); intsum=0, maxn=0;  for(intI=1; i<=n;i++)    {        intCnt=0, x=0, y=0; if(Grap[i].size () >1)        {             for(intj=0; J<grap[i].size (); j + +) {q[++cnt]=Grap[i][j]; X+=a[grap[i][j]];x%=10007; Y+=a[grap[i][j]]*a[grap[i][j]];y%=10007; } sort (Q+1, q+cnt+1, CMP); MAXN=max (maxn,a[q[1]]*a[q[2]]); Sum+=x*x-X; Sum= (sum+10007)%10007; }} printf ("%d%d", maxn,sum); return 0;}
View Code

Joint weights (Codevs 3728)

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.