[NOIP2014] Joint weights

Source: Internet
Author: User

Describe

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 distances are 2, the joint weights of wuxwv are produced 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 format

The input file name is link.in.

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.

Input Sample:

5

1 2

2 3

3 4

4 5

1 5 2) 3 10

Output format

The output file name is Link.out.

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 output:

20 74

Note

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

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

For 100% of data, 1<n≤200,000,0<wi≤10,000.

Analysis

At first, 7 points were spent using pure violence. Later to see some of the problems on the Internet, and carefully read the topic, roughly understand the method.

Since the minimum distance of two points for generating the joint weights is 2, it can be considered that the two points are connected by a transit point. Then save the neighboring points of each point directly, 22 pairs in these neighboring points. But the 22 pairing is obviously too slow, calculating the joint weights of each of the points of the point to be O (K2) (k is the number of neighbors). This is obviously not worth the cost. The internet has introduced the optimization using the addition-binding law (yes, the addition-binding law): preprocessing the sum of the weights of the neighbors of each point I s[i], then the total joint weights and sum=sum{Sum{w[v[i][j]] * (S[i]-w[v[i][j])} (0≤j≤k )。

The most powerful value can be found by directly finding the largest two multiplies in the I neighbor when calculating s[i].

TYVJ can be directly ac,vijos need to use Getint optimization read-in.

Code
1#include <cstdio>2#include <cctype>3#include <vector>4 using namespacestd;5vector<int> v[200001];6 ints[200001];7 intw[200001];8 intN, Maxx, sum;9 intGetint ()Ten { One     Charc =GetChar (); A      while(!IsDigit (c)) -c =GetChar (); -     intx =0; the      while(IsDigit (c)) { -x = x *Ten+ C-'0'; -c =GetChar (); -     } +     returnx; - } + voidAddinti) A { at      for(intj =0; J! = V[i].size (); ++j) -sum = (sum + (S[i]-w[v[i][j]) * w[v[i][j]])%10007; - } - intMain () - { -n =getint (); in      for(inti =0, A, B; I! = N-1; ++i) { -A =getint (); tob =getint (); + V[a].push_back (b); - V[b].push_back (a); the     } *      for(inti =1; I <= N; ++i) $W[i] =getint ();Panax Notoginseng      for(inti =1; I <= N; ++i) { -         intmax1, Max2; theMax1 =0; +MAX2 =0; A          for(intj =0; J! = V[i].size (); ++j) { theS[i] + =W[v[i][j]]; +             if(S[i] >10007) -S[i]-=10007; $             if(W[v[i][j]) >max1) { $MAX2 =max1; -Max1 =W[v[i][j]]; -             } the             Else if(W[v[i][j]] = = MAX1 | | W[V[I][J]] >max2) -MAX2 =W[v[i][j]];Wuyi         } the         if(MAX1 * max2 >Maxx) -Maxx = Max1 *Max2; Wu     } -      for(inti =1; I <= N; ++i) About Add (i); $printf"%d\40%d", Maxx, sum%10007); -     return 0; -}

[NOIP2014] Joint weights

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.