# bzoj3167 [Heoi2013]Sao

 1 /**************************************************************
2     Problem: 3167
3     User: _Angel_
4     Language: C++
5     Result: Accepted
6     Time:3984 ms
7     Memory:8792 kb
8 ****************************************************************/
9 #include<cstdio>
10 #include<cstring>
11 #include<algorithm>
12 #include<vector>
13 using namespace std;
14 const int maxn=1005,p=1000000007;
15 int C[maxn][maxn];
16 struct DP{
17     int f[maxn],n;
18     void clear(){
19         memset(f,0,sizeof(f));
20         n=1;
21         f[0]=1;
22     }
23     DP &operator+=(const DP &b){
24         static int g[maxn];
25         memset(g,0,sizeof(g));
26         for(int i=0;i<n;i++)for(int j=1,tmp=0;j<=b.n;j++){
27             tmp=(tmp+b.f[j-1])%p;
28             g[i+j]=(g[i+j]+(long long)f[i]*tmp%p*C[i+j][j]%p*C[n+b.n-i-j-1][b.n-j]%p)%p;
29         }
30         memcpy(f,g,sizeof(f));
31         n+=b.n;
32         return *this;
33     }
34     DP &operator*=(const DP &b){
35         static int g[maxn];
36         memset(g,0,sizeof(g));
37         int sum=0;
38         for(int j=0;j<b.n;j++)sum=(sum+b.f[j])%p;
39         for(int i=0;i<n;i++)for(int j=0,tmp=sum;j<b.n;j++){
40             g[i+j]=(g[i+j]+(long long)f[i]*tmp%p*C[i+j][j]%p*C[n+b.n-i-j-1][b.n-j]%p)%p;
41             tmp=(tmp-b.f[j]+p)%p;
42         }
43         memcpy(f,g,sizeof(f));
44         n+=b.n;
45         return *this;
46     }
47 }f[maxn];
48 void dfs(int);
49 vector<int>G[maxn];
50 vector<bool>W[maxn];
51 int T,n,prt[maxn];
52 int main(){
53     C[0][0]=1;
54     for(int i=1;i<=1000;i++)for(int j=0;j<=i;j++){
55         C[i][j]=C[i-1][j];
56         if(j)C[i][j]=(C[i][j]+C[i-1][j-1])%p;
57     }
58     scanf("%d",&T);
59     while(T--){
60         scanf("%d",&n);
61         memset(prt,0,sizeof(prt));
62         for(int i=1;i<=n;i++){
63             G[i].clear();
64             W[i].clear();
65             f[i].clear();
66         }
67         for(int i=1,x,y;i<n;i++){
68             char c;
69             scanf("%d %c%d",&x,&c,&y);
70             x++;
71             y++;
72             G[x].push_back(y);
73             W[x].push_back(c=='>');
74             G[y].push_back(x);
75             W[y].push_back(c=='<');
76         }
77         dfs(1);
78         int ans=0;
79         for(int i=0;i<n;i++)ans=(ans+f[1].f[i])%p;
80         printf("%d\n",ans);
81     }
82     return 0;
83 }
84 void dfs(int x){
85     for(int i=0;i<(int)G[x].size();i++)if(G[x][i]!=prt[x]){
86         prt[G[x][i]]=x;
87         dfs(G[x][i]);
88         if(W[x][i])f[x]+=f[G[x][i]];
89         else f[x]*=f[G[x][i]];
90     }
91 }
View Code

233333333
posted @ 2017-06-13 07:05  AntiLeaf  阅读(755)  评论(0编辑  收藏  举报