[JZOJ5165] W small animation

[JZOJ5165] W small animation

(sort.cpp 1s 256M)
small WW recently fell in love with Japanese anime, updated every day countless anime waiting to see him, so he had all the animation sequential row, of course, although there are numerous anime, but in addition to No. 1 animation, every department and only a cartoon animation has its prequel (father), that is to say, all of the animation form a tree structure. The order of the animation must meet the following two limitations:
all subsequent ① an animation (descendants) must be ranked behind it.
② For the sequel with an animation (children), a small W high love to be standing in the front.
WW sort of small light also unhappy, and he wanted to know how many species of a total ordering scheme, and it outputs the answer mod10007 of.
Input
The first row represents the group T represents the number of data.
Next, each data of the first row and n represents the number sorting unit waiting animation,
next n lines each represented by a first number tot this animation number sequel,
next tot in accordance with the number of small favorite descending WW given its sequel number.
n≤1000.
Output
of each data line number ans, show the results of the answers mod10007.
The Input the Sample
. 1
. 5
. 3. 4. 3 2
0
. 1. 5
0
0
the Sample the Output
2

Solution

For X-rooted subtree, all of which can sort the composition, the relative position of the sons of the node X and X is unique, but below the point at these points can be permutated.

Provided f (x) represents the number of valid sequence x is a subtree rooted energy thereof;

In the subtree FA (x) as the root, the descendant node x can only arranged after x, x has been assumed that the nodes are arranged t, then the descendant node x alone \ (C_ {sz (x) -1 + t} ^ {sz ( x) -1} \ times f (x) \) species arrangement, which is the contribution to f (fa (x)) caused by

The last answer is f (1)

Code

#include<bits/stdc++.h>
#define int long long
using namespace std;
int read(){
    int x=0,f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
    for(;isdigit(ch);ch=getchar())x=x*10+ch-'0';
    return x*f;
}
const int N=1e3+28,p=10007;
int n,sz[N],tr[N],tot[N],son[N][N];
int mul[N],di[N];
int Pow(int x,int y=p-2){
    int re=1;
    while(y){
    if(y&1)re=re*x%p;
    y>>=1;
    x=x*x%p;
    }
    return re;
}
void Pre(int n=1000){
    mul[1]=di[1]=1;
    for(int i=2;i<=n;i++){
    mul[i]=mul[i-1]*i%p;
    di[i]=Pow(mul[i]);
    }
}
int C(int n,int m){
    if(m==0||n==m)return 1;
    int re=mul[n]*di[m]%p;
    re=re*di[n-m]%p;
    return re;
}
void dfs(int x=1){
    tr[x]=sz[x]=1;
    int fk=0;
    for(int i=tot[x];i>=1;i--){
    dfs(son[x][i]);
    sz[x]+=sz[son[x][i]];
    tr[x]=tr[x]*C(fk+sz[son[x][i]]-1,fk)%p;
    tr[x]=tr[x]*tr[son[x][i]]%p;
    fk+=sz[son[x][i]];
    }
}
signed main(){
    //freopen("sort.in","r",stdin);
    //freopen("sort.out","w",stdout);
    Pre();
    int t=read();
    while(t--){
    memset(tr,0,sizeof(tr));
    n=read();
    for(int i=1;i<=n;i++){
        tot[i]=read();
        for(int j=1;j<=tot[i];j++){
        son[i][j]=read();
        }
    }
    dfs();
    printf("%lld\n",tr[1]);
    }
    return 0;
}
/*
  1
  5
  3 4 3 2
  0
  1 5
  0
  0
*/

Guess you like

Origin www.cnblogs.com/nlKOG/p/11488403.html