[NOIP2014] Joint weighted value

Source: Internet
Author: User

Main topic:

There is a tree that asks for the sum of the product of the distance of 2 points and the maximum value.

Ideas:

Enumerating each point, the points adjacent to each other are points with a distance of 2. The maximum value of this part is the product of the two points with the maximum point weight, and the square of each point minus the weights and squares of the points, so that each edge is run two times, so the complexity is O (n).

Using adjacency tables to store double arrays (no direction), of course, like the Cyk of the big God directly running without considering this.

Code:

adjacency table:

1#include <cstdio>2 Const intmo=10007, m=200008;3 intcnt,x,y,n,i,ans,tot,w[m],v[m<<1],last[m<<1],head[m<<1];4 5 voidAddintXintY) {v[++cnt]=y,last[cnt]=head[x],head[x]=CNT;}6 7 intMain ()8 {9scanf"%d",&n);Ten      for(i=1; i<n;i++) scanf ("%d%d",&x,&y), add (x, y), add (y,x); One      for(i=1; i<=n;i++) scanf ("%d",&w[i]); A      for(i=1; i<=n;i++) -     { -         intsum=0, max1=0, max2=0, J,o; the          for(j=head[i];j;j=Last[j]) -         { -o=W[v[j]]; -sum= (sum+o)%mo; +             if(O&GT;MAX1) max2=max1,max1=o; -             Else if(O&GT;MAX2) max2=o; +tot= (tot-o*o)%mo; A         } attot= (tot+sum*sum)%mo; -Sum=max1*Max2; -         if(Sum>ans) ans=sum; -     } -printf"%d%d\n", Ans,tot); -     return 0; in}

%%%cyk Great God:

1#include <iostream>2 using namespacestd;3 inti,n,maxx,ans,x[200005],y[200005],w[200005];4 intam[200005],am2[200005],s[200005],qs[200005];5 intMain ()6 {7Cin>>N;8      for(i=1; i<=n-1; i++) cin>>x[i]>>Y[i];9      for(i=1; i<=n;i++) cin>>W[i];Ten      for(i=1; i<=n-1; i++) One       { AS[x[i]]= (S[x[i]]+w[y[i])%10007; -Qs[x[i]]= (Qs[x[i]]+w[y[i]]*w[y[i])%10007; -S[y[i]]= (S[y[i]]+w[x[i])%10007; theQs[y[i]]= (Qs[y[i]]+w[x[i]]*w[x[i])%10007; -           if(W[y[i]]>am[x[i]]) {am2[x[i]]=am[x[i]];am[x[i]]=W[y[i]];} -           Else if(W[y[i]]>am2[x[i]]) am2[x[i]]=W[y[i]]; -           if(W[x[i]]>am[y[i]]) {am2[y[i]]=am[y[i]];am[y[i]]=W[x[i]];} +           Else if(W[x[i]]>am2[y[i]]) am2[y[i]]=W[x[i]]; -       } +maxx=0; A      for(i=1; i<=n;i++) Maxx=max (maxx,am[i]*am2[i]); atans=0; -      for(i=1; i<=n;i++) ans= (Ans+s[i]*s[i]-qs[i])%10007; -cout<<maxx<<' '<<ans; -}

[NOIP2014] Joint weighted value

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.