The joint weights of the second problem of NOIP2014 raising group

Source: Internet
Author: User

Let's read the question first:

Question 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 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 distance is 2, the joint weights of Wu * Wv are generated between them. 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
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 weight of the point numbered I on Figure G is Wi.
Output
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.
Input example
5
1 2
2 3
3 4
4 5
1 5 2) 3 10
Output example
20 74
Other Notes
Sample Description: An ordered point pair with a distance of 2 (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 range: For 100% of data, 1 < n≤200,000,0 < wi≤10,000.

Very depressed, only over 7 points, the remaining 3 points inexplicably in the second value of WA.

Or first of all, to see the scope of N, it must be used adjacent to the table (note that the non-map to the opposite of each other), there is no link to the topic of the graph has n nodes, n-1, indicating that this is a tree. Then we begin to solve, the first question: You can enumerate in turn, but this is too slow. We can target each node, through the adjacency table to find all of its neighbors, and then to determine their point, for each node to maintain two values, the point neighbor right of the first and second largest, sweep over once, we only need to find which point the first and second Mahayana product maximum can be obtained first question. Here is the second question: see the second ask everyone first reaction is definitely to find all the neighbors, and then every two points at a time to calculate the product of the right, add up is the result, the second reaction is this is O (n^2) complexity, do not want to definitely timeout, the third reaction is to try to think O (n) or O (n log N) algorithm. Then began to think: for a node p, his neighbour a,b,c,d,e ... We need to calculate ab+ac+ad+......bc+bd ... Can this be done with a mathematical formula? As a result of the recent brush school summer homework (early high school math bridging reader), there is a chapter on the factorization, wherein the formula back very 6: (a+b) ^2=a^2+b^2+2ab, (a+b+c) ^2=a^2+b^2+c^2+2* (AB+AC+BC) ... And then immediately think of the AB+AC+BC that I asked for? Know this, we for each point, just put this point of the neighbor squared sum and the sum of the square maintenance, then a subtraction can be calculated, time complexity O (N).

Here's the code:

1#include <iostream>2#include <queue>3#include <cmath>4#include <algorithm>5#include <cstring>6#include <conio.h>7 using namespacestd;8 Const intmaxn=200000+Ten;9 intn,u[maxn],v[maxn],w[maxn],first[2*maxn],next[2*Maxn],a,b,max,lmax,ans1,sum1,sum2,ans2;Ten intRead () One { A     intf=1, x=0; -     CharCh=GetChar (); -     if(ch=='-') f=-1; the      while(ch<'0'|| Ch>'9') -     { -         if(ch=='-') f=-1; -Ch=GetChar (); +     } -      while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} +     returnx*F; A } at voidAddedge (intIintAintb)//plus edge - { -u[i]=a;v[i]=b; -next[i]=First[a]; -first[a]=i; - }  in intMain () - { tomemset (first,-1,sizeof(first)); +n=read (); -      for(intI=0; i<n-1; i++) the     { *A=read (); b=read (); $Addedge (2*i,a,b);Panax NotoginsengAddedge (2*i+1, b,a); -     } the      for(intI=1; i<=n;i++) w[i]=read (); +      for(intI=1; i<=n;i++) A     { thesum1=sum2=max=0; +lmax=-1; -          for(intj=first[i];j!=-1; j=Next[j]) $         { $             if(w[v[j]]>LMAX)//maintain maximum and secondary maximum values -             { -                 if(W[v[j]]>max) max=W[v[j]]; the                 Elselmax=W[v[j]]; -             }WuyiSum1= (Sum1+w[v[j])%10007and/or maintaining the right of the neighbor's midpoint theSum2= (sum2+ (w[v[j]]%10007*w[v[j]]%10007)%10007)%10007;//Maintain the square sum of the right of the neighbors -         } WuAns1=max (ans1,max*LMAX); -Ans2= (ans2+ ((SUM1*SUM1)%10007-SUM2)) +10007)%10007;//Set of formulas About     } $printf"%d%d", ans1,ans2); -     return 0; -}

One of the caveats is that the code 55 line adds a 10007, because these are modulo operations, and there are subtraction operations, so it is possible to calculate a negative, so we need to add a 10007 so that can solve the problem of negative numbers (in fact, the money teacher said in the food chain), These lessons must be borne in mind in the future.

Also, here for help, there is a big God can help me tune this problem, this code for some particularly large data when the second question is always wrong, I do not know why. Like this, small data and the data can be too, and big data will not go out of the general where the problem? When you find the problem, please be in time @ me, I am greatly appreciated.

The joint weights of the second problem of NOIP2014 raising group

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.