Topological Order Count
time limit: 1 s
space limit: 128000 KB
Title Description Description
Find the number of topological orders that have a root tree/diagram.
Enter a description input Description
The first line is an n and a prime number p, which indicates that the tree has n nodes, which requires the result of the modp of topological order.
Next n-1 lines, each line has two digits x and Y, indicating that X is the father of Y.
Guaranteed 1<=n<=1500000, n<p<2^31,p for Prime.
Outputs description output Description
A line of numbers that modp the number of topological orders for the tree diagram.
sample input to sample
Example 1
4 100000007
1 2
1 3
2 4
Example 2
6 100000007
1 2
2 3
1 4
3 5
5 6
Sample output Sample outputs
Example 1
3
Example 2
5
data size & Hint
The number of sons of each non-root node is more average, the amount of data is large, it is recommended that C + + players use scanf reading mode
Title Link: http://codevs.cn/problem/1304/
The tree-shaped DP, in fact, is more like a count on a tree.
Title Analysis : The topic requires a topological sequence on a tree, and the topological order on the tree is certainly better than the topological order on the graph. We can roughly introduce a two-fork tree to merge the sub-tree into the root formula, and then promote.
State design: set ANS1[X] is the number of topological order of the subtree with node x as root, and ans2[x] is the number of nodes of the subtree with x as the root.
State transitions:When the tree is a two-fork tree, it is easy to find Ans2[u]=ans2[v1]+ans2[v2]+1,ans1[u]=c (Ans2[v1]+ans2[v2],ans2[v1]) *ans1[v1]*ans1[v2]. When the tree is not a two-tree, we can merge two subtrees into a subtree, then merge the subtree with the other subtrees in turn.
#include <bits/stdc++.h>#defineN 3000055using namespacestd;Long LongMoD;Long LongJc[n],ny[n];Long LongQmod (Long LongXLong Longy) { Long Longans=1; while(y) {if(y%2==1) ans= (ans*x)%MoD; Y/=2; X= (x*x)%MoD; } returnans;}voidinit () {jc[1]=1; for(intI=2; i<n;i++) jc[i]= (jc[i-1]*i)%MoD; Ny[n-1]=qmod (jc[n-1],mod-2); for(inti=n-2; i>=1; i--) ny[i]= (ny[i+1]* (i+1))%MoD;}Long LongCLong LongMLong LongN) { if(n==0|| M==n)return 1; returnjc[m]*ny[n]%mod*ny[m-n]%MoD;}structss{intV,next;}; SS Edg[n/2];inthead[n/2],now_edge=0;intd[n/2]={0};voidAddedge (intUintv) {Edg[now_edge]=(ss) {V,head[u]}; Head[u]=now_edge++;}Long Longans1[n/2]={0},ans2[n/2]={0};voidDfsintx) {Ans1[x]=1; for(inti=head[x];i!=-1; i=Edg[i].next) { intv=edg[i].v; DFS (v); ANS2[X]+=Ans2[v]; ANS1[X]=c (Ans2[x],ans2[v]) *ans1[x]%mod*ans1[v]%MoD; } Ans2[x]++;}Long LongRead () {Long Longans=0; CharCh=GetChar (); while(! (ch>='0'&&ch<='9')) ch=GetChar (); while(ch>='0'&&ch<='9') {ans*=Ten; Ans+=ch-'0'; CH=GetChar (); } returnans;}intMain () {intN; //scanf ("%d%lld", &n,&mod);N= (int) read (); mod=read (); Init (); memset (Head,-1,sizeof(head)); for(intI=1; i<n;i++) { intx, y; //scanf ("%d%d", &x,&y);X=read (); y=read (); Addedge (x, y); D[y]++; } for(intI=1; i<=n;i++) if(!D[i]) {DFS (i); printf ("%lld\n", Ans1[i]); Break; } return 0;}
View Code
Topological order Count