"Noip Tour" NOIP2014 day1 T2 Joint weights

Source: Internet
Author: User

2 . Joint Weight Value

(Link.cpp/c/pas)

"Problem Description described "

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

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.

Output

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.

"Input and Output sample"

Link.in

Link.out

5

1 2

2 3

3 4

4 5

1 5 2) 3 10

20 74

"Sample description"

In this example, the graph as shown above, the distance is 2 of the ordered point pairs have (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 description"

For 30% of data,1<≤100;

For 60% of data,1<≤2000;

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

Ideas

Analysis topic: N points n-1 edge of the connected graph is the forest.

Notice the particularity of the topic: the distance is 2. It is known that the joint weights can be generated for any of the two connected points of each node. Enumeration requires O (n^3) of time.

Optimization: Solve Max: Note that for a node, the resulting joint weights are the maximum of two w in a child node.

Solve Sum:suma= w1*w2+...+w1*wn+w2*w1+w2*w3...+w2*wn+.....=sumw^2-w1^2-w2^2 .... So it takes only O (n) time to find out the Suma.

Code

#include <iostream>#include<cstring>#include<queue>#include<vector>#definefor (A,B,C) for (int a= (b);a< (c); a++)using namespacestd;Const intMAXN =200000+Ten;//from 1 Beware of crossing the borderConst intMOD =10007;intW[MAXN];intmaxw=0, sumw=0, N;vector<int> G[MAXN];//Vector accelerated BFSvoidBFS (ints) {intVIS[MAXN]; for (I,0, n+1) vis[i]=0; Queue<int>Q;  Q.push (s); Vis[s]=1;  while(!Q.empty ()) {        intu=Q.front (); Q.pop (); intSuma=0, sumb=0; Suma= (Suma*suma)%MOD; intmax1=0, max2=0; For (I,0, G[u].size ()) {              intv=G[u][i]; Suma= (Suma+w[g[u][i]])%MOD; Sumb= (sumb+ (w[v]*w[v]%mod))%MOD; if(W[V]&GT;MAX1) {max2=max1; max1=w[v];}//if it is updated, it will cause leakage .              Else if(W[V]&GT;MAX2) max2=W[v]; if(!Vis[v]) {Vis[v]=1; Q.push (v); }} SUMW= (sumw+ (suma*suma)%mod-sumb+mod)%mod)%mod;//attention to many modsMaxw=max (maxw,max1*max2); }}intMain () {Ios::sync_with_stdio (false); CIN>>N; //starting from 1 markingfor (I,0, N-1) {        intU,v; Cin>>u>>v;        G[u].push_back (v);    G[v].push_back (U); } for (I,1, n+1) cin>>W[i]; BFS (1); cout<<maxw<<" "<<SUMW; return 0;}

"Noip Tour" NOIP2014 day1 T2 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.