bzoj4031: [HEOI2015]小Z的房间

题目大意

就是生成树计数问题

题解

矩阵树定理
题表和定理大意

CODE:

#include
#include
#include
#include
using namespace std;
typedef long long LL;
const LL MOD=1e9;
const LL N=105;
LL n,m;
LL idx[N][N],id=0;
char ss[N][N];
LL d[N][N],a[N][N];
LL c[N][N];//度数   是否有边 
LL fx[4]={-1,0,0,1};
LL fy[4]={0,-1,1,0};
LL solve ()
{
    LL f=1;//翻转了多少次,非常重要 
    for (LL u=1;u<=id;u++)
        for (LL i=u+1; i<=id; i++)
            while (c[i][u]!=0)
            {
                LL q=c[u][u]/c[i][u];
                for (LL j=u; j<=id; j++)
                {
                    LL now=c[u][j];
                    c[u][j]=c[i][j];
                    c[i][j]=(now-q*c[u][j]%MOD+MOD)%MOD;
                }
                f=0-f;
            }
    LL ans=f;
    for (LL u=1;u<=id;u++)
        ans=ans*c[u][u]%MOD;
    return (ans+MOD)%MOD;
}
int main()
{
    scanf("%lld%lld",&n,&m);
    for (LL u=1;u<=n;u++)   scanf("%s",ss[u]+1);
    for (LL u=1;u<=n;u++)
        for (LL i=1;i<=m;i++)
            if (ss[u][i]=='.')
                idx[u][i]=++id;
    for (LL u=1;u<=n;u++)
        for (LL i=1;i<=m;i++)
            if (ss[u][i]=='.')
                for (LL j=0;j<4;j++)
                {
                    LL tx=u+fx[j],ty=i+fy[j];
                    if (tx<=0||tx>n||ty<=0||ty>m||ss[tx][ty]=='*') continue;
                    a[idx[u][i]][idx[tx][ty]]=1;
                    d[idx[u][i]][idx[u][i]]++;
                }
    for (LL u=1;u<=id;u++)
        for (LL i=1;i<=id;i++)
            c[u][i]=(d[u][i]-a[u][i]+MOD)%MOD;
    id--;
    printf("%lld\n",solve()%MOD);
    return 0;
}

你可能感兴趣的:(矩阵树定理)