Daily quiz: Colors && hackerrank unique_colors

Source: Internet
Author: User

Topic Portal: Https://www.hackerrank.com/challenges/unique-colors

Thanks to HZQ for this problem.

Consider the point division (after all, it is the road statistics), for each color, its contribution is independent. We can do it together in a single point division (why is the time complexity right, because each change we make changes only according to the color of the current point, not all the colors to it all over again). Each time the answer contribution is calculated separately for the center of gravity, the individual answer contributions for the current region are also calculated and based on (then referred to as the baseline contribution, the tot in the code). For each subtrees tree, we first DFS cancel the contribution of this area (why to cancel, because the contribution here to be left to their level to solve, or will be repeated calculation), and then a separate to the deep search again, the specific content is as follows:

There is a color, if this is the first time in the deep search process, we have to deduct the current benchmark contribution to the contribution of the color (due to enter this deep search, has canceled the contribution of the subtrees tree, so the actual deduction of the outside subtree of the color contribution) and the size of the outside subtree, so as to maintain the benchmark contribution, Then the direct addition to this point forms the current contribution. Of course, if it is not the first appearance of the tube, because the deep search has been processed on the road, and then directly added.

This is the point division approach, time Complexity (NLOGN). There is also a linear approach, which is to use the DFS sequence, and then to make a difference to one interval, and finally sweep over.

Point Division Rule Code:

1#include <bits/stdc++.h>2 using namespacestd;3 #defineN 1000054 #defineINF 1e95 #defineLL Long Long6InlineintRead () {7     intx=0, f=1;CharA=GetChar ();8      while(a<'0'|| A>'9') {if(a=='-') f=-1; A=GetChar ();}9      while(a>='0'&& a<='9') x=x*Ten+a-'0', a=GetChar ();Ten     returnx*F; One } A intN,cnt,sum,size[n],head[n],a[n],s[n],weight,weights,tot; - LL Ans[n]; - BOOLVis[n],app[n]; the structedges{ -     intTo,next; -}e[2*N]; -InlinevoidInsert () { +     intU=read (), v=read (); -e[cnt]= (edges) {v,head[u]};head[u]=cnt++; +e[cnt]= (edges) {u,head[v]};head[v]=cnt++; A } at voidGetroot (intXintFA) { -size[x]=1;inttmp=0; -      for(inti=head[x];i>=0; i=E[i].next) { -         if(Vis[e[i].to] | | fa==e[i].to)Continue; -Getroot (E[I].TO,X); size[x]+=size[e[i].to]; -tmp=Max (tmp,size[e[i].to]); in     } -Tmp=max (tmp,sum-size[x]); to     if(tmp<weights) weight=x,weights=tmp; + } - voidDfsintXintFaintf) { the     BOOLok=0; *     if(!app[a[x]] && a[x]!=a[weight]) app[a[x]]=ok=1, s[a[x]]+=size[x]*f,tot+=size[x]*F; $      for(inti=head[x];i>=0; i=e[i].next)if(!vis[e[i].to] && fa!=e[i].to) DFS (e[i].to,x,f);Panax Notoginseng     if(OK) app[a[x]]=0; - } the voidDasointXintFaintp) { +     BOOLok=0; A     if(!app[a[x]] && a[x]!=a[weight]) app[a[x]]=ok=1, tot+=p-S[a[x]]; theans[x]+=tot; +      for(inti=head[x];i>=0; i=e[i].next)if(!vis[e[i].to] && fa!=e[i].to) DFS (e[i].to,x,p); -     if(OK) app[a[x]]=0, tot-=p-S[a[x]]; $ } $ voidWorkintx) { -Weights=inf; Getroot (x,0); Getroot (weight,0); -X=weight; vis[x]=1; theDFS (weight,0,1); TOT+=SIZE[X]; ans[x]+=tot; -      for(inti=head[x];i>=0; i=E[i].next) {Wuyi         if(Vis[e[i].to])Continue; theDFS (e[i].to,x,-1); tot-=size[e[i].to]; -DFS (e[i].to,x,size[x]-size[e[i].to]); WuDFS (E[i].to,x,1); tot+=size[e[i].to]; -     } AboutDFS (weight,0,-1); tot=0; $      for(inti=head[x];i>=0; i=e[i].next) -     if(!vis[e[i].to]) sum=size[e[i].to],work (e[i].to); - }  - intMain () { AN=read (); memset (head,-1,sizeof(head)); +      for(intI=1; i<=n;i++) a[i]=read (); the      for(intI=1; i<n;i++) insert (); -Sum=n; Work1); $      for(intI=1; i<=n;i++) printf ("%lld\n", Ans[i]); the     return 0; the}

Daily quiz: Colors && hackerrank unique_colors

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.