P4111 [HEOI2015]小Z的房间 生成树计数

这个题是生成树计数的裸题,中间构造基尔霍夫矩阵,然后构成行列式,再用高斯消元就行了。这里高斯消元有一些区别,交换两行行列式的值变号,且消元只能将一行的数 * k 之后加到别的行上。

剩下就没啥了。。。

找到一个写的特别详细的。

il int det() {
    int ans = 1;
    for (ri i = 1; i <= sz; ++i) {  // 当前在消第i个(i,i)
        for (ri j = i + 1, t; j <= sz; ++j) {  // 把它下面对应的位置消成0
            while (m[j][i]) { // 直到为0
                t = m[i][i] / m[j][i];  // 计算第j行相应的数是第i行的几倍
                for (ri k = i; k <= sz; ++k) {  // 一个一个消去并交换数字(消去后之前的位置变小)
                    mod(m[i][k] -= m[j][k] * t);
                    swap(m[i][k], m[j][k]); // 交换
                }
                ans *= -1; // 记得交换是,行列式取反
            }
        }
        if (m[i][i] == 0) return 0; // 如果有零,就不用继续做了
        else mod(ans *= m[i][i]); // 更新ans
    }
    return (ans % md + md) % md;
}

题干:

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

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

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

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

输出格式:

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

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(register int i = a;i <= n;++i)
#define lv(i,a,n) for(register int i = a;i >= n;--i)
#define clean(a) memset(a,0,sizeof(a))
#define int long long
const int INF = 1 << 30;
const int mod = 1e9;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
const int N = 100;
int tot = 0,n,m;
int f[N][N],mp[N][N];
void add(int x,int y)
{
    if(x > y) return;
    f[x][x]++;f[y][y]++;
    f[x][y]--;f[y][x]--;
}
int gauss()
{
    int ans = 1;
    for(int i = 1;i < tot;i++)
    {
        for(int j = i + 1;j < tot;j++)
        {
            while(f[j][i])
            {
                int t = f[i][i] / f[j][i];
                for(int k = i;k < tot;k++)
                {
                    f[i][k] = (f[i][k] - t * f[j][k] % mod + mod) % mod;
                }
                swap(f[i],f[j]);
                ans = -ans;
            }
        }
        ans = (ans * f[i][i]) % mod;
    }
    return (ans + mod) % mod;
}
main()
{
    read(n);read(m);
    duke(i,1,n)
    {
        char c;
        duke(j,1,m)
        {
            cin>>c;
            if(c == '.') mp[i][j] = ++tot;
        }
    }
    duke(i,1,n)
    {
        duke(j,1,m)
        {
            int tem,u;
            if(!(u = mp[i][j])) continue;
            if(tem = mp[i - 1][j]) add(u,tem);
            if(tem = mp[i + 1][j]) add(u,tem);
            if(tem = mp[i][j - 1]) add(u,tem);
            if(tem = mp[i][j + 1]) add(u,tem);
        }
    }
    printf("%lld\n",gauss());
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/DukeLv/p/10415849.html