【BZOJ4031】小Z的房间

Description

你突然有了一个大房子,房子里面有一些房间。事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子。在一开始的时候,相邻的格子之间都有墙隔着。

你想要打通一些相邻房间的墙,使得所有房间能够互相到达。在此过程中,你不能把房子给打穿,或者打通柱子(以及柱子旁边的墙)。同时,你不希望在房子中有小偷的时候会很难抓,所以你希望任意两个房间之间都只有一条通路。现在,你希望统计一共有多少种可行的方案。

Input

第一行两个数分别表示n和m。

接下来n行,每行m个字符,每个字符都会是’.’或者’*’,其中’.’代表房间,’*’代表柱子。

Output

 一行一个整数,表示合法的方案数 Mod 10^9

Sample Input

3 3
...
...
.*.

Sample Output

15

HINT

对于前100%的数据,n,m<=9

【题解】

果果的矩阵树定理

构造邻接矩阵与度数矩阵之差的矩阵,求行列式;

由于:任意一行乘以一个常数加到另一行,矩阵的行列式不变。

又知:三角矩阵(上三角及下三角矩阵)的行列式就是它对角线上所有元素的乘积。

所以求出三角矩阵求行列式即可。

【code】

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define pb push_back 
 4 #define ll long long
 5 #define ull unsigned long long
 6 #define file(s) freopen("s.in","r",stdin),freopen("s.out","w",stdout)
 7 #define rep(k,i,j) for(int k = i;k <= j; ++k)
 8 inline int read(){
 9     int x = 0,f = 1; char ch=getchar();
10     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();}
11     while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0'; ch=getchar();}
12     return x*f; 
13 } 
14 const int mxn = 100+3;
15 const int mod = 1e9; 
16 int n,m,tot;
17 int s[mxn][mxn];
18 ll c[mxn][mxn];
19 
20 inline void in(){
21 //    tot = 0;
22     n = read(),m = read();
23     rep(i,1,n){
24         char ch;
25         rep(j,1,m){
26             cin >> ch;
27             if(ch=='.') s[i][j] = ++tot;
28         }
29     }
30 } 
31 inline void add(int x,int y){
32     if(x>y) return;
33     c[x][x]++,c[y][y]++;
34     c[x][y]--,c[y][x]--;
35 } 
36 inline void prewor(){
37     rep(i,1,n){
38         rep(j,1,m){
39             if(!s[i][j]) continue;
40             if(s[i-1][j]) add(s[i][j],s[i-1][j]);
41             if(s[i+1][j]) add(s[i][j],s[i+1][j]);
42             if(s[i][j-1]) add(s[i][j],s[i][j-1]);
43             if(s[i][j+1]) add(s[i][j],s[i][j+1]);
44         } 
45     }
46 }
47 inline int gauss(){
48     int ans = 1;
49     for(int i = 1;i < tot; ++i){
50         for(int j = i+1;j < tot; ++j){
51             while(c[j][i]){
52                 int rate = c[i][i]/c[j][i];
53                 for(int k = i;k < tot; ++k)
54                     c[i][k] = (c[i][k]-rate*c[j][k]+mod)%mod;
55                 swap(c[i],c[j]);
56                 ans = -ans;
57             }
58         }
59         ans = (ans*c[i][i])%mod;
60     }
61     return (ans+mod)%mod;
62 }
63 inline void print(){
64     printf("%d\n",gauss());
65 }
66 int main(){
67 //    file();
68     in();
69     prewor();
70     print();
71     return 0;
72 }
View Code

 

posted @ 2019-02-09 19:25  ve-2021  阅读(144)  评论(0编辑  收藏  举报