bzoj 4031 [HEOI2015]小Z的房间 Matrix-tree定理

题目大意

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

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

分析

生成树计数
定理的学习以后补充,老师放题太快了

solution
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long LL;
const int M=11;
const int Q=1000000000;

int n,m;
int num[M][M];
char s[M];
int tot;

struct Guass{
    int a[M*M][M*M];
    int n;
    int tag;
    void init(int nn){
        n=nn;
        tag=1;
        memset(a,0,sizeof(a));
    }
    int getres(){
        int i,res=1;
        for(i=1;i<=n;i++)
            res=(LL)a[i][i]*res%Q;
        (res+=Q)%=Q;//res可能本来是负数
        return tag?res:(Q-res)%Q;//最后%Q不然res=0,tag=0会输出Q
    }
    void xiao(int x,int i,int y){
        while(a[y][i]){
            int D=a[x][i]/a[y][i];
            swap(a[x],a[y]);
            tag^=1;
            for(int j=1;j<=n;j++) a[y][j]=(((LL)a[y][j]-(LL)a[x][j]*D)%Q+Q)%Q;
        }
    }
    void det(){
        int i,j;
        for(i=1;i<=n;i++){
            for(j=i+1;j<=n;j++)
            if(a[j][i]) xiao(i,i,j);
        }
    }
}GS;

int main(){

    int i,j;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        scanf("%s",s+1);
        for(j=1;j<=m;j++)
            if(s[j]=='.') num[i][j]=++tot;
    }
    int nw,to;
    int x,y;
    tot--;
    GS.init(tot);
    for(i=1;i<=n;i++)
    for(j=1;j<=m;j++){
        nw=num[i][j];
        if(!nw)continue;
        x=i-1,y=j; to=num[x][y];
        if(to){
            GS.a[nw][nw]++;
            GS.a[nw][to]--;
        }
        x=i+1,y=j; to=num[x][y];
        if(to){
            GS.a[nw][nw]++;
            GS.a[nw][to]--;
        }
        x=i,y=j-1; to=num[x][y];
        if(to){
            GS.a[nw][nw]++;
            GS.a[nw][to]--;
        }
        x=i,y=j+1; to=num[x][y];
        if(to){
            GS.a[nw][nw]++;
            GS.a[nw][to]--;
        }
    }
    for(i=1;i<=tot;i++)
    for(j=1;j<=tot;j++)
        if(GS.a[i][j]<0) GS.a[i][j]+=Q;
    
    GS.det();
    
    printf("%d\n",GS.getres());
    
    return 0;
}

转载于:https://www.cnblogs.com/acha/p/6423035.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值