你忽然有了一个大房子,房子里面有一些房间。事实上,你的房子能够看作是一个包含n*m个格子的格状矩形,每一个格子是一个房间或者是一个柱子。在一开始的时候,相邻的格子之间都有墙隔着。学习
你想要打通一些相邻房间的墙,使得全部房间可以互相到达。在此过程当中,你不能把房子给打穿,或者打通柱子(以及柱子旁边的墙)。同时,你不但愿在房子中有小偷的时候会很难抓,因此你但愿任意两个房间之间都只有一条通路。如今,你但愿统计一共有多少种可行的方案。spa
生成树计数
定理的学习之后补充,老师放题太快了code
#include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <algorithm> using namespace std; typedef long long LL; const int M=11; const int Q=1000000000; int n,m; int num[M][M]; char s[M]; int tot; struct Guass{ int a[M*M][M*M]; int n; int tag; void init(int nn){ n=nn; tag=1; memset(a,0,sizeof(a)); } int getres(){ int i,res=1; for(i=1;i<=n;i++) res=(LL)a[i][i]*res%Q; (res+=Q)%=Q;//res可能原本是负数 return tag?res:(Q-res)%Q;//最后%Q否则res=0,tag=0会输出Q } void xiao(int x,int i,int y){ while(a[y][i]){ int D=a[x][i]/a[y][i]; swap(a[x],a[y]); tag^=1; for(int j=1;j<=n;j++) a[y][j]=(((LL)a[y][j]-(LL)a[x][j]*D)%Q+Q)%Q; } } void det(){ int i,j; for(i=1;i<=n;i++){ for(j=i+1;j<=n;j++) if(a[j][i]) xiao(i,i,j); } } }GS; int main(){ int i,j; scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ scanf("%s",s+1); for(j=1;j<=m;j++) if(s[j]=='.') num[i][j]=++tot; } int nw,to; int x,y; tot--; GS.init(tot); for(i=1;i<=n;i++) for(j=1;j<=m;j++){ nw=num[i][j]; if(!nw)continue; x=i-1,y=j; to=num[x][y]; if(to){ GS.a[nw][nw]++; GS.a[nw][to]--; } x=i+1,y=j; to=num[x][y]; if(to){ GS.a[nw][nw]++; GS.a[nw][to]--; } x=i,y=j-1; to=num[x][y]; if(to){ GS.a[nw][nw]++; GS.a[nw][to]--; } x=i,y=j+1; to=num[x][y]; if(to){ GS.a[nw][nw]++; GS.a[nw][to]--; } } for(i=1;i<=tot;i++) for(j=1;j<=tot;j++) if(GS.a[i][j]<0) GS.a[i][j]+=Q; GS.det(); printf("%d\n",GS.getres()); return 0; }