P4111 [HEOI2015] 小 Z 的房间 题解
这个大房子可以看成一张 $n\times m$ 的网格图,
柱子不能打通的限制可以看成删去每个柱子对应的点,
此时答案即为剩下的图的生成树个数。
用矩阵树定理求解即可。
#include <cstdio>
#include <algorithm>
#define M 1000000000
#define int long long
using namespace std;
char z;
int u, v, n, _ = 1, q = 1, o[350][350], a[350][350];
void A(int u, int v)
{
a[u][u] = (a[u][u] + 1) % M, a[v][v] = (a[v][v] + 1) % M;
a[u][v] = (a[u][v] + M - 1) % M, a[v][u] = (a[v][u] + M - 1) % M;
}
signed main()
{
scanf("%lld%lld", &u, &v);
for (int i = 1; i <= u; ++i)
for (int j = 1; j <= v; ++j)
{
scanf(" %c", &z);
if (z == '.')
o[i][j] = ++n;
}
for (int i = 1; i <= u; ++i)
for (int j = 1; j <= v; ++j)
{
if (i != u && o[i][j] && o[i + 1][j])
A(o[i][j], o[i + 1][j]);
if (j != v && o[i][j] && o[i][j + 1])
A(o[i][j], o[i][j + 1]);
}
for (int i = 2; i <= n; ++i)
for (int j = i + 1; j <= n; ++j)
{
while (a[i][i])
{
int z = a[j][i] / a[i][i];
for (int k = i; k <= n; ++k)
a[j][k] = (a[j][k] + M - a[i][k] * z % M) % M;
swap(a[i], a[j]);
_ = -_;
}
swap(a[i], a[j]);
_ = -_;
}
for (int i = 2; i <= n; ++i)
q = (q * a[i][i]) % M;
if (_ == -1)
q = (M - q) % M;
printf("%lld", q);
return 0;
}