Tree heuristic merging (DSU on tree)

Source: Internet
Author: User

The story tells us that before making a spicy chicken quiz, it's best to look at what new posture he invented = = actually directly out of the way naked

Reference Links:

http://codeforces.com/blog/entry/44351 (original)

http://blog.csdn.net/QAQ__QAQ/article/details/53455462

This technique can solve most of the non-modified subtree queries in O (NLOGN) time.

Example 1 sub-tree color statistics

There is a tree of n points, the root is 1, each point has a 1~n color, for each point given a number k, to inquire about the number of points in this subtree with the color K. n<=500000.

This example is of course too trivial,dfs a tree to be able to water the past, but this is not the point ...

Our goal is to achieve a less violent thing, instead of the following code:

EdgintN,cc[sz],col[sz],ks[sz],anss[sz];voidEDTintXintFintv) {Cc[col[x]]+=v;  forESB (x,e,b)if(b!=f) EDT (B,X,V);}voidDfsintXintf=0) {EDT (X,f,1); ANSS[X]=Cc[ks[x]]; EDT (X,f,-1);  forESB (x,e,b)if(b!=f) DFS (b,x);}

The input is roughly as follows:

intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d", col+i);  for(intI=1; i<=n;i++) scanf ("%d", ks+i);  for(intI=1; i<n;i++)    {        intb; scanf ("%d%d",&a,&b);     Adde (A, b); } DFS (1);  for(intI=1; i<=n;i++) printf ("%d\n", Anss[i]);}

Here is a halal Nlogn approach:

EDGintN,cc[sz],col[sz],ks[sz],anss[sz];intSz[sz],son[sz];voidGsintXintf=0) {Sz[x]=1;  forESB (x,e,b) {if(b==f)Continue; GS (B,X); SZ[X]+=Sz[b]; if(Sz[b]>sz[son[x]]) son[x]=b; }}intskip=0; voidEDTintXintFintv) {Cc[col[x]]+=v;  forESB (x,e,b)if(b!=f&&b!=Skip) EDT (B,X,V);}voidDfsintXintf=0,BOOLkep=0){     forESB (x,e,b)if(b!=f&&b!=son[x]) DFS (B,X); if(Son[x]) DFS (son[x],x,1), skip=Son[x]; EDT (X,f,1); ANSS[X]=Cc[ks[x]]; Skip=0; if(!KEP) EDT (x,f,-1);}

(If the operation is more complex, it is recommended to look at the following example three codes = =)

Why is this nlogn? Because a heavy chain doubles the size of the subtree.

We found that this technique is very useful, as long as the magnetic to a set to insert is O (1), the deletion of elements to be empty so that each O (1) (note that every deletion is bound to delete the full empty), then for the sub-tree collection of queries can be done O (NLOGN), I don't know where the spicy Chicken team is. (Mo team Complexity nsqrt (n), and must also support the removal of O (1)).

Example 2 Lomsat gelral (cf600e)

N points have a root tree, 1 is the root, and each point has a color. We call a color occupy a subtree when and only if no other color appears in this subtree more than it. The sum of all the colors that occupy every subtree.

Template problem.

#defineSZ 666666EDGintN,cc[sz],col[sz],sz[sz],son[sz];ll Anss[sz];voidGsintXintf=0) {Sz[x]=1;  forESB (x,e,b) {if(b==f)Continue; GS (B,X); SZ[X]+=Sz[b]; if(Sz[b]>sz[son[x]]) son[x]=b; }}BOOLSkip[sz];intcx=0; ll sum=0;voidEDTintXintFintk) {Cc[col[x]]+=K; if(k>0&&cc[col[x]]>=CX) {if(cc[col[x]]>cx) Sum=0, cx=Cc[col[x]]; Sum+=Col[x]; }     forESB (x,e,b)if(b!=f&&!Skip[b]) EDT (B,X,K);}voidDfsintXintf=0,BOOLkep=0){     forESB (x,e,b)if(b!=f&&b!=son[x]) DFS (B,X); if(Son[x]) DFS (son[x],x,1), skip[son[x]]=1; EDT (X,f,1); ANSS[X]=sum; if(Son[x]) skip[son[x]]=0; if(!Kep) EDT (X,f,-1), cx=sum=0;}intMain () {scanf ("%d",&N);  for(intI=1; i<=n;i++) scanf ("%d", col+i);  for(intI=1; i<n;i++)    {        intb; scanf ("%d%d",&a,&b);     Adde (A, b); } GS (1); Dfs1);  for(intI=1; i<=n;i++) printf ("%i64d", Anss[i]);}

Example 3 Arpa ' s letter-marked tree and Mehrdad ' s Dokhtar-kosh paths (CF741D)

I rating the hot chicken.

We call a string as Doubi string when and only if the characters that rearrange it can form a palindrome string.

Give a root tree with n points, a root of 1, a character on each edge (only a~v, don't ask me why), and the maximum length of the Doubi string that can be composed of all the simple paths in each sub-tree of each point.

Doubi string is obviously only 0/1 characters appear odd number of strings, if only a~v to consider each character as a bits, a point I to the root of the path XOR value is recorded as S[i], then we are for each x in the subtree to find a and B, so that the s[a]^s[b ] is a power of 0 or 2, and the dep[a]+dep[b]-dep[lca]*2 is the largest.

So the problem is, LCA if the direct as x figure out the answer will be bigger ... It looks like we need to expand this thing so that it only counts different subtrees. This is good, for each subtrees tree first statistics and then update on the line.

Template Dafa Good!

#defineSZ 1234567EDGCintN,sz[sz],son[sz],fc[sz],dep[sz];voidGsintXintf=0) {Sz[x]=1;  forESB (x,e,b) {if(b==f)Continue; FC[B]=fc[x]^Vc[e]; DEP[B]=dep[x]+1; GS (B,X); SZ[X]+=Sz[b]; if(Sz[b]>sz[son[x]]) son[x]=b; }}Const ints='v'-'a'+1, inf=1e9;intmd[5555555],CANS,SKIP,CDEP;voidclrintx) {md[fc[x]]=-inf;}voidUpdintx) {Cans=max (cans,md[fc[x]]+dep[x]-cdep*2);  for(intI=0; i<s;i++) cans=max (cans,md[fc[x]^ (1<<i)]+dep[x]-cdep*2);}voidInsintx) {Md[fc[x]]=Max (md[fc[x]],dep[x]);} Template<void(*func) (int) >voidEDTintXintf)    {func (x);  forESB (x,e,b)if(B!=f&&b!=skip) edt<func>(b,x);}intDp[sz];voidDfsintXintf=0,BOOLkep=0){     forESB (x,e,b)if(b!=f&&b!=son[x]) DFS (B,X); if(Son[x]) DFS (son[x],x,1), skip=Son[x]; CDEP=Dep[x];  forESB (X,E,B)if(b!=f) dp[x]=Max (dp[x],dp[b]);  forESB (X,E,B)if(b!=f&&b!=Son[x]) EDT<upd> (b,x), edt<ins>(B,X); UPD (x);    Ins (x); DP[X]=Max (Dp[x],cans); Skip=0; if(!KEP) edt<clr> (x,f), cans=-inf;}intMain () { for(intI=0;i< (1<<s); i++) Md[i]=-inf; scanf ("%d",&N);  for(intI=2; i<=n;i++)    {        intXCharc[3]; scanf ("%d%s",&x,c); Adde (I,x,1<< (c[0]-'a')); } GS (1); Dfs1);  for(intI=1; i<=n;i++) printf ("%d", Dp[i]);}

Tree heuristic merging (DSU on tree)

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.