Describe
undirected Connectivity Graph G has n points, n-1 edges. Points are numbered from 1 to N, and the weights for the points numbered I areWiWi
, each edge has a length of 1. 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 produceWuWu
XWvWv
Value of the joint weight.
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?
Format input Format
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 two positive integers, where the I integer represents the weight of the point numbered I on Figure G WiWi
.
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 may be large, it is necessary to 10007 the output.
Sample input:
5
1 2
2 3
3 4
4 5
1 5 2) 3 10
Sample output:
20 74
Idea: Two nodes with a distance of 2 are sibling nodes with the same Father node. Iterate through each knot node in turn, seeking the sum of weights and values of each node and self. The sum of the weights generated by all the sons of the parent node is sum*sum-self. The maximum value of the union weights is the maximum and secondary values of the node weights of the son that traverse each parent node. The quadrature is compared with the global variable.
#include <iostream>#include<string.h>#include<algorithm>using namespaceStd;typedefLong Longll;Const intmaxn=200005;Const intMod=10007;structedge{intto,net;} ES[MAXN+MAXN];intN,W[MAXN];intHead[maxn],tot;voidAddedge (intUintv) {es[tot].to=v; Es[tot].net=Head[u]; Head[u]=tot++;} ll Mx,res;voidsolve () { for(intI=1; i<=n;i++) {ll mx1=0, mx2=0; ll Sum=0, self=0; for(intj=head[i];j!=-1; j=es[j].net) { intv=es[j].to; if(w[v]>=MX1) {MX2=MX1; MX1=W[v]; } Else if(w[v]>MX2) {MX2=W[v]; } Sum+=W[v]; Self+ = (w[v]*W[v]); } MX=max (mx,mx1*MX2); Res+ = (sum*sum); Res-=Self ; Res%=MOD; }}intMain () {memset (head,-1,sizeof(head)); CIN>>O; for(intI=0; i<n-1; i++) { intu,v; CIN>>u>>v; Addedge (U,V); Addedge (V,u); } for(intI=1; i<=n;i++) {cin>>W[i]; } solve (); cout<<mx<<" "<<res<<Endl; return 0;}
vijos1906: Joint weights