矩阵树+生成树计数
小Z的房间
注意使用夕哥哥板子的时候点id从0开始计数
或者改板子w
#include<bits/stdc++.h>
//#include<gl/glut.h>
//#include<ctime>
#define ll long long
#define mes(a,b) memset(a,b,sizeof(a))
#define ctn continue
#define ull unsigned long long
//#pragma warning(disable:4996)
#define tgg cout<<"---------------"<<endl;
const ll linf = 9223372036854775807;
const int inf = 0x3f3f3f3f;
using namespace std;
const double pi = acos(-1);
const ll maxn = 4e5 + 5;
const int mod = 1e9;
const double eps = 1e-8;
const ull p = 131;
const ull pc = 13331;
inline ll gcd(int a, int b) {
while (b ^= a ^= b ^= a %= b);
return a;
}
string mp[13];
int id[12][12];
ll A[12][12], D[12][12];
struct Matrix {
ll mat[12][12];
void init(int n) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
mat[i][j] = D[i][j] - A[i][j];
//cout << mat[i][j] << " ";
}
//cout << endl;
}
}
ll det(int n) {
ll res = 1;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
while (mat[j][i] != 0) {
ll t = mat[i][i] / mat[j][i] % mod;
for (int k = i; k <= n; k++) {
mat[i][k] = (mat[i][k] - t * mat[j][k] % mod + mod) % mod;
}
swap(mat[i], mat[j]);
res = -res;
}
}
res = (res * mat[i][i]) % mod;
}
return (res + mod) % mod;
}
}mat;
int main(int argc, char* argv[]) {
int n, m;
cin >> n >> m;
mes(D, 0);
mes(A, 0);
for (int i = 0; i < n; i++) {
cin >> mp[i];
}
int lab = 0;
int dx[] = { 1, 0 }, dy[] = { 0, 1 };
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mp[i][j] == '.') {
id[i][j] = lab;
++lab;
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (mp[i][j] == '.') {
int now = id[i][j];
for (int k = 0; k < 2; k++) {
int ax = i + dx[k];
int bx = j + dy[k];
if (ax<0 || bx<0 || ax>=n || bx>=m || mp[ax][bx] != '.') ctn;
int nxt = id[i + dx[k]][j + dy[k]];
++A[now][nxt];
++A[nxt][now];
++D[now][now];
++D[nxt][nxt];
}
}
}
}
mat.init(lab);
ll ans = mat.det(lab - 1);
cout << ans << endl;
return 0;
}