NOIP2014 Joint weights--the strongest analysis in history

Source: Internet
Author: User




[Ideas for solving problems]
(Although the following may be a bit more, but I still hope that you can read, to understand the benefits)
In general, the ontology allows for two values, a combined sum of one ownership value, and two is the largest joint weight, well, let's take a look at the largest joint weights
The so-called joint weights, is the two distance 2 nodes of the full-time product, then how to multiply the largest? For a point, the greatest product is naturally the product of the weights of the two most weighted nodes adjacent to it,
As for the two maximum value, we can completely read the data when the way to handle it, a little bit!!
For the sum of all the joint weights, we ask that you still have a little bit of mathematical foundation, but not high, the third grade is enough!! Think first, how are we going to ask for this and what? Oh! is to put all the
Add up! Yes, but how do you add it fast? You think, oh, a point next to a lot of points, every two a ride, really good trouble ah!! Are there any legendary algorithms that can handle this?
No. There is only one, that is called, the addition of the Law of Union!!!
Well, I also don't pretend, you think, and O point adjacent points have a,b,c,d,e,f,g seven, assuming their weight is a,b,c,d,e,f,g, two forced youth of the calculation process is this: sum:=a b+a c+a d.......+a....+e*g ... But in fact, we do this, we first use the addition algorithm, we find that sum is actually the weight of each point multiplied by the joint (distance is 2) The sum of the ownership value of the node, so we can read into the data, we could pre-processing, the adjacent nodes around each node of the sum of weights to add up, for example , at the time of processing, add the weights of the A,B,C....G to the sum of the weights of the neighboring points of the S[o]{o points}, and then in the calculation, you can use the Sum:=a (s[o]-a) +b (s[o]-b) ... the natural result comes out!
The efficiency of the above algorithm analysis, it seems that there is no place to use a two-tier cycle, the time complexity of the infinite close to O (n), definitely do not time out!!!
[Reference Program]

 ProgramLinktypeEdge=Record//used to store edges, U and V for two nodesU,v:longint;End;varN,i,j,ans1,ans2,u,v:longint; SArray[1..200000] ofInt64;//s array is used to store the sum of weights of all points adjacent to node IW,MAX1,MAX2:Array[1..200000] ofLongint;The //w array is used to store weights for each edge, and MAX1 is used to store the node weights with the highest weights in the neighboring nodes of each node, MAX2 to store the sub-large node weights .E:Array[1..200000] ofEdge//Storage Edge procedure work(x:longint;  var a,b:longint);//Note that this process is larger than the size, but because the result of the comparison must be returned, the global variable must be used, and the Var in parentheses is indispensablebegin                                      ifX>a Then   beginB:=a; A:=x;End    Else  ifX>b ThenB:=x;End;beginREADLN (n); fori:=1  toN-1  DoREADLN (E[I].U,E[I].V);//Read into two endpoints on each side   fori:=1  toN Do Read(W[i]);//read-in weights   fori:=1  toN-1  Do //preprocessing of values  beginu:=e[i].u;    V:=E[I].V; Inc (S[u],w[v]);///equivalent to S[U]:=S[U]+W[V], intended to calculate the sum of weights of adjacent nodes of the U nodeInc (S[v],w[u]);//Ibid .Work (W[v],max1[u],max2[u]);//constantly update the maximum and secondary values of the weights around the nodesWork (W[u],max1[v],max2[v]);//Ibid .  End; fori:=1  toN Do ifMax1[i]*max2[i]>ans1 ThenAns1:=max1[i]*max2[i];//Calculate the maximum joint weight value   fori:=1  toN-1  Do//Handle two nodes per edge  beginu:=e[i].u;    V:=E[I].V; Ans2:= (ans2+ (S[u]-w[v]) *w[v]MoD 10007)MoD 10007; Ans2:= (ans2+ (S[v]-w[u]) *w[u]MoD 10007)MoD 10007;End; Writeln (ANS1,"', ANS2);End.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

NOIP2014 Joint weights--the strongest analysis in history

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.