[BZOJ1005] Prufer Series + permutation combination

Source: Internet
Author: User

The prufer sequence of a tree

Each time you find the smallest leaf node in the remaining tree (which is a node with a degree of 1 for a tree without root), delete it.

The parent node (that is, the unique point connected to it) is added to the prufer sequence.

A tree corresponding to a prufer sequence

G sets start as empty set

Set the current processing to the Prufer sequence of item I, found in the G collection does not appear and in Prufer[i. N-2] The smallest node of the label that is not present, set it to U.

Add u to the set G and the U to Prufer[i] with an edge.

Finally, an edge is attached between two points that are still not present in the G collection (one must be N).

To think about why you can restore it to the original tree.

First of all, we can consider the operation of adding the G set as deleting the leaf node.

No representation of this number in the G set is still in the tree and has not been deleted.

Again prufer[i. N-2] does not appear to represent the current degree of the node is 1, that is, the leaf node.

So the finding of U is actually equivalent to the smallest leaf node of the label.

Obviously, the tree and Prufer series one by one correspond.

That is, there is only one prufer sequence for a tree.

For a prufer sequence, only one tree can be restored.

Some properties

Each prufer sequence has n-2 entries (n is the number of nodes)

For a node with a degree of x, it appears x-1 times in the Prufer sequence

The prufer sequence that satisfies both of these conditions (of course each number cannot exceed N) must be able to form a valid tree

 With this idea, you can begin to do the problem.

That is, the number of possible Prufer series.

into a permutation and combination problem

For the CNT has a degree limit of the node, its a[i]-1 and we set to sum, then the prufer sequence of n-2 in the space to select the sum position of the CNT dot

For the rest of the n-sum-2, you can place arbitrary points without a degree limit.

  (n-2)!* (n-cnt) n-sum-2/(∏ (a[i]-1)!* (n-sum-2)!)

  The decomposition of the mass factor can be used to solve the above-mentioned formula, and a faster method can be used for the decomposition of factorial.

Final high-precision multiplication.

Program bzoj1005;Constmaxn=1010; tt=10000; type arr=array[-1.. Maxn]of Longint;varN,cnt,sum,i,j:longint; a,p,w:array[-1..    Maxn]of Longint; vis:array[-1..    Maxn]of Boolean;    Ans:arr; SS:string;p rocedure printf;begin writeln (0); Halt;end;procedure build;varI,j:longint;begin Fillchar (Vis,sizeof(VIS),true); p[0]:=0;  fori:=2To MAXN DobeginifVis[i] THEN BEGIN Inc. (p[0]); p[p[0]]:=i;        End  forj:=1To p[0] DobeginifI*P[J]&GT;MAXN Then Break; Vis[i*p[j]]:=false; ifI mod p[j]=0Then Break;    End End;end;procedure work (x,y:longint);vari,xx,tot:longint;begin XX:=x;  fori:=1To p[0] Dobegin x:=xx;tot:=0;  whileX<>0  DoBegin Inc (Tot,x Div P[i]); X:=x Div p[i];        End Inc (W[i],tot*y); End;end;procedure Work2 (x,y:longint);varI,tot:longint;begin fori:=1To p[0] Do ifX mod p[i]=0Then begin tot:=0;  whileX mod p[i]=0  DoBegin Inc (TOT); x:=x Div p[i];        End Inc (W[i],tot*y); End;end;function Mul (a:arr;b:longint): arr;varC:arr; I:longint;begin Fillchar (c,sizeof(c),0); c[0]:=a[0];  fori:=1To c[0] DoBegin Inc (C[I],A[I)*b); Inc (C[i+1],c[i] div tt); C[i]:=C[i] MoD tt;    End ifc[c[0]+1]<>0Then Inc (c[0]); Exit (c); End;begin//assign (input, ' bzoj1005.in '); reset (input); //assign (output, ' bzoj1005.out '); rewrite (output);READLN (n); CNT:=0; sum:=0;  fori:=1to n Dobegin READLN (A[i]); ifA[i]>0Then Inc (CNT)Else            ifa[i]=0then printf; ifA[i]>0Then Inc (sum,a[i]-1);    End Fillchar (W,sizeof(W),0); ifsum>n-2then printf;    Build Work (n-2,1); Work (n-2-sum,-1); WORK2 (n-cnt,n-sum-2);  fori:=1to n Do ifA[i]>0Then work (a[i]-1,-1); ans[0]:=1; ans[1]:=1;  fori:=1To p[0] Do         forj:=1To W[i] Doans:=Mul (Ans,p[i]); Write (ans[ans[0]]);  fori:=ans[0]-1Downto1  Dobegin STR (ANS[I],SS);  forJ:=length (ss) +1To4  DoWrite0);    Write (Ans[i]); End;end.

[BZOJ1005] Prufer Series + permutation combination

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.