[FJSC2014] Compounds

Source: Internet
Author: User

"Title description"

The chief Noi kneeling, so went to the culture class. Now he faces a chemistry problem.

The source of this problem is because in a strange school two of the party playing a strange game theory game, the game is very painful, I believe you are not interested in listening.

Because this game involves game theory, the students of the competition ask the chief to ask for a value similar to the SG function.

They have a very magical compound in their hands, and its molecules are made up of n atoms (don't care about the details of what an atom might and its multiple atoms bond into). This molecule forms a tree structure, and number 1th is the root.

If the distance between the two atoms I and J to their nearest public ancestor is Li and LJ, the AIJ values are defined as:

Aij = Li Xor LJ

The topic requires that for each k (k∈n), the number of atomic pairs with a value of 22 a k is calculated.

"Input Format"

The first line is an integer n.

Next N-1 line, each line an integer p, the integer of line I indicates the father of the I atom is P.

"Output Format"

Starting with k=0, the number of atomic pairs that output 22 A value of K is output to the last nonzero number in line k+1.

"Sample Input"

3

1

1

"Sample Output"

1

2

The "Data range" uses H to represent the maximum depth of the tree structure molecule.

40%:n<=1000,h<=30

70%:n<=3000,h<=100

100%:n<=100000,h<=500

Solution

70 minutes of violence ... So conscience.

100%: Similar to the Practice of tree Division, but only select the designated non-leaf nodes at a time.

Considering the effect of the path through the root in the subtree on the answer, note f[x][j] represents the subtree with the root of the X node, and the number of nodes with the X node distance of J. Can obviously be transferred through DFS. The impact is divided into two types:

1. Chain with X as its starting point. ANS[J]+=F[X][J];

2. The endpoint is within a different subtree. For example, the following set of data:

n=6,fa[]={0,1,1,2,2,2}

There are f[2]={1,3},f[3]={1}.

Two arrays multiply, everything is resolved.

Actually this is called the female function = =

F[2]=x+3x^2,f[3]=x

Originally f[2]*f[3]=x^2+3x^3

But this is XOR, so the exponent does not add up but is different or, so f[2]*f[3]=1+3x^3

If you have more than one tree, 22 violent rides. Different or this kind of thing do not know or do not play for the better.

  

And then this is just violence, the standard way to multiply O (n^2) with FWT down to O (Nlogn), really lonely as Snow =

1#include <cstdio>2#include <cstring>3#include <cstdlib>4#include <ctime>5#include <algorithm>6 intfa[100010];7 intMain ()8 {9Freopen ("che.in","W", stdout);TenSrand (Time (0)); One     intn=100000;p rintf ("%d\n", n); A      for(intI=2; i<= -; i++) printf ("#df", rand ()% (I-1)); -      for(intI=401; i<=n;i++) printf ("%d", rand ()% $+i- $); -}
Data Maker
1#include <cstdio>2#include <cstring>3 intfa[100010],n,dep[100010],q[100010],l,r,max,tmp,a[ -];Long Longans[ -];4 structe{intTO,NXT;} e[100010];5 intet,la[100010];6 voidAddintXintY) {e[++et]= (e) {y,la[x]};la[x]=et;}7 intLcaintx)8 {9      for(; X;x=fa[x])if(a[dep[x]]==x) Break;Ten     returnx; One } A intMain () - { -scanf"%d", &n);inti,j,k;dep[1]=1; the      for(i=2; i<=n;i++) scanf ("%d",&Fa[i]), add (fa[i],i); -      for(q[l=r=1]=1; l<=r;l++) -      for(i=la[q[l]];i;i=e[i].nxt) -     if(!dep[e[i].to]) +dep[q[++r]=e[i].to]=dep[q[l]]+1; -      for(i=1; i<n;i++) +     { AMemset (A,0,sizeof(a)); at          for(J=i;j;j=fa[j]) a[dep[j]]=J; -          for(j=i+1; j<=n;j++) -         { -k=LCA (j); - //printf ("i=%d j=%d k=%d ans=%d%d\n", I,j,k, (Dep[i]-dep[k]), (Dep[j]-dep[k])); -Ans[tmp= (Dep[i]-dep[k]) ^ (Dep[j]-dep[k])]++; in             if(max<tmp) max=tmp; -         } to     } +      for(k=0; k<=max;k++) printf ("%lld\n", Ans[k]); - } the /* * 9 $ 1 1 2 2 2 3 7 7Panax Notoginseng */
70%
1#include <cstdio>2#include <cstring>3 Const intmaxn=100010, maxm=1025;4 Long LongANS[MAXM],TMP2[MAXM];5 intFA[MAXN],N,DEP[MAXN],Q[MAXN],L,R,MAX,TMP[MAXM],ET,LA[MAXN],F[MAXN][MAXM],MDEP[MAXN];6 structe{intTO,NXT;} E[MAXN];7 voidAddintXintY) {e[++et]= (e) {y,la[x]},la[x]=et;}8 voidMultint*a,int*b,int&N)9 {Tenmemset (TMP2,0,sizeof(TMP2));intI,j,c=0, t=N; One      for(i=0; i<=n;i++)if(A[i]) A      for(j=0; j<=n;j++)if(B[j]) -Tmp2[c= ((i+1) ^ (j+1))]+=1ll*a[i]*b[j],t<c?t=c:1; -      for(n=t,i=0; i<=n;i++) ans[i]+=Tmp2[i]; the } - voidDfsintx) - { -     BOOLflag=0;inti,j; +      for(i=la[x];i;i=e[i].nxt) -     { + DFS (e[i].to); A         if(Mdep[x]<mdep[e[i].to]) mdep[x]=mdep[e[i].to]; at          for(j=0; j<=mdep[x];j++) f[x][j+1]+=F[e[i].to][j]; -     } -      for(i=la[x];i;i=e[i].nxt) -      for(j=e[i].nxt;j;j=e[j].nxt) - mult (f[e[i].to],f[e[j].to],mdep[x]); -f[x][0]++;mdep[x]++; in      for(i=1; i<=mdep[x];i++) ans[i]+=F[x][i]; - } to intMain () + { -scanf"%d", &n);inti,j,k;dep[1]=1; the      for(i=2; i<=n;i++) scanf ("%d",&Fa[i]), add (fa[i],i); *      for(q[l=r=1]=1; l<=r;l++) $      for(i=la[q[l]];i;i=e[i].nxt)Panax Notoginseng     if(!dep[e[i].to]) -dep[q[++r]=e[i].to]=dep[q[l]]+1; the      for(Dfs (1), max=1024x768;! ans[max];max--); +      for(k=0; k<=max;k++) printf ("%lld\n", Ans[k]); A}
100%

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.