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;
}
posted @ 2017-02-21 10:55  _zwl  阅读(229)  评论(0编辑  收藏  举报