http://acm.timus.ru/problem.aspx?space=1&num=1627
生成树计数模板
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 105;
const int mode= 1e9;
ll a[maxn][maxn];
ll det(int n){
int i,j,k;
ll ans=1;
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
while(a[j][i]){
ll tmp =a[i][i]/a[j][i];
for(k=0;k<n;k++){
a[i][k]=((a[i][k]-tmp*a[j][k])%mode+mode)%mode;
swap(a[i][k],a[j][k]);
}
ans=-ans;
}
}
if(a[i][i]==0)return 0;
ans=ans*a[i][i];
}
return (ans%mode+mode)%mode;
}
char ch;
int mp[13][13];
int g[maxn][maxn];
int n,m,cnt;
int dir[4][2]={0,1,0,-1,1,0,-1,0};
void build(int i,int j){
int k;
for(k=0;k<4;k++){
int ii=i+dir[k][0];
int jj=j+dir[k][1];
if(ii<0||ii>=n||jj<0||jj>=m)continue;
if(mp[ii][jj]<0)continue;
g[mp[i][j]][mp[ii][jj]]=1;
}
}
int main(){
int i,j;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin>>ch;
if(ch=='*')mp[i][j]=-1;
else mp[i][j]=cnt++;
}
}
for(i=0;i<n;i++){
for(j=0;j<m;j++){
if(mp[i][j]<0)continue;
build(i,j);
}
}
for(i=0;i<cnt;i++){
for(j=0;j<cnt;j++){
if(i!=j&&g[i][j]){
a[i][j]=-1;
a[i][i]++;
}
}
}
ll ans=det(cnt-1);
printf("%lld\n",ans);
return 0;
}