[NOIP2014_D1_T2] Joint weights
Time limit: 1000MS
Memory Limit: 131072KB
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.
Exercises
Store with the edge-linked list, enumerate each point, find the edge of the point connection, and the distance from the end point of the two edges from which it was set is 2.
Thanks to Miskoo, just record all the and, then add w* (t-w) can.
↑ This is a tall way on the ↑
1#include <bits/stdc++.h>2 using namespacestd;3 intN,cnt=0;4 structEdge {5 intTo,next;6}e[400010];7 intw[200010];8 inthead[200010];9 intans=0, maxx=-1;TenInlineintRead () { One intx=0, f=1;Charch; ACh=GetChar (); - while(ch>'9'|| ch<'0') {if(ch=='-') f=-1; Ch=GetChar ();} - while(ch<='9'&&ch>='0') {x= (x<<1) + (x<<3) +ch-'0'; ch=GetChar ();} the returnx*F; - } - voidAddedge (intUintv) { -e[++cnt].to=v; +e[cnt].next=Head[u]; -head[u]=CNT; + } A intMain () { atn=read (); - for(intI=1; i<n;++i) { - int from, to; - from=read (); to=read (); -Addedge ( from, to); -Addedge (To, from); in } - for(intI=1; i<=n;++i) w[i]=read (); to for(intI=1; i<n;++i) { + intmax1=-1, max2=-1, t=0, WX; - for(intj=head[i];j;j=E[j].next) { thewx=w[e[j].to]; *t=t+wx,t%=10007; $ if(wx>max1) {Panax NotoginsengMax2=max1; -max1=WX; the } + Else if(wx>max2) { AMax2=WX; the } + } -max1*=Max2; $ if(Max1>maxx) maxx=max1; $ for(intj=head[i];j;j=E[j].next) { -wx=w[e[j].to]; -ans+=wx* (T-WX), ans%=10007; the } - }WuyiAns= (ans+10007)%10007; theprintf"%d%d\n", Maxx,ans); - return 0; Wu}
View Code
[Fzyzoj 2044] [NOIP d1t2] Joint weights