否则,因为根节点不可能和任何一个节点相等,所以f[u][i]=f[u][i-1]
#include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<iostream> #define maxn 410 #define mod 1000000007 using namespace std; struct yts { int x,y; }a[maxn]; int head[maxn],to[maxn],next[maxn]; long long f[maxn][maxn],g[maxn]; int fa[maxn]; bool vis[maxn]; long long c[maxn][maxn]; int d[maxn],size[maxn]; int n,m,num,t,M; char s[5]; void addedge(int x,int y) { num++;to[num]=y;next[num]=head[x];head[x]=num;d[y]++; } int find(int x) { if (fa[x]==x) return x; else return fa[x]=find(fa[x]); } bool dfs(int x,int fa) { vis[x]=1; bool flag=0; for (int p=head[x];p;p=next[p]) if (to[p]!=fa) { if (vis[to[p]]) return 0; if (!dfs(to[p],x)) return 0; if (flag) { memset(g,0,sizeof(g)); for (int j=1;j<=size[x];j++) for (int k=1;k<=size[to[p]];k++) if (f[x][j] && f[to[p]][k]) for (int i=max(j,k);i<=j+k;i++) g[i]=(g[i]+(long long)((f[x][j]*f[to[p]][k]%mod)*c[i][j]%mod)*c[j][k+j-i]%mod)%mod; size[x]+=size[to[p]]; for (int i=1;i<=size[x];i++) f[x][i]=g[i]; } else { size[x]=size[to[p]];flag=1; for (int i=1;i<=size[x];i++) f[x][i]=f[to[p]][i]; } } if (x) { size[x]++; if (flag) for (int i=size[x];i>=1;i--) f[x][i]=f[x][i-1]; else f[x][1]=1; } return 1; } int main() { scanf("%d%d",&n,&M); c[0][0]=1; for (int i=1;i<=100;i++) { c[i][0]=1; for (int j=1;j<=i;j++) c[i][j]=(c[i-1][j]+c[i-1][j-1])%mod; } for (int i=1;i<=n;i++) fa[i]=i; for (int i=1;i<=M;i++) { int x,y; scanf("%d%s%d",&x,s,&y); if (s[0]=='=') { int f1=find(x),f2=find(y); fa[f1]=f2; } else { a[++m].x=x;a[m].y=y; } } for (int i=1;i<=m;i++) { int f1=find(a[i].x),f2=find(a[i].y); addedge(f1,f2); if (f1==f2) {printf("0\n");return 0;} } for (int i=1;i<=n;i++) if (!d[find(i)]) addedge(0,find(i)); if (!dfs(0,-1)) {printf("0\n");return 0;} for (int i=1;i<=n;i++) if (fa[i]==i && !vis[i]) {printf("0\n");return 0;} long long ans=0; for (int i=1;i<=size[0];i++) ans=(ans+f[0][i])%mod; printf("%lld\n",ans); return 0; }