BZOJ 4031: [HEOI2015]小Z的房间 Matrix-Tree定理
题目链接:
http://www.lydsy.com/JudgeOnline/problem.php?id=4031
题解:
Matrix-tree定理解决生成树计数问题,其中用到高斯消元法求上三角矩阵,其中消元用的是辗转相除法。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; const int mod = 1e9;
const int maxn = ;
typedef long long LL; int n, m, tot;
char str[maxn][maxn];
int mp[maxn][maxn];
LL C[maxn][maxn];
const int dx[] = { ,,-, };
const int dy[] = { -,,, }; LL Det(int n) {
LL ret = ;
int f = ;
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
C[i][j] = (C[i][j] % mod + mod) % mod;
}
}
for (int i = ; i <= n; i++) {
for (int j = i + ; j <= n; j++) {
int A = C[i][i], B = C[j][i];
while (B!=) {
LL t = A / B; A = A%B; swap(A, B);
for (int k = i; k <= n; k++) {
C[i][k] = (C[i][k] - t*C[j][k] % mod + mod) % mod;
}
for (int k = i; k <= n;k++) {
swap(C[i][k], C[j][k]);
}
f = -f;
}
}
ret = ret*C[i][i] % mod;
}
if (f == -) ret = ((-ret)%mod + mod) % mod;
return ret;
} void init() {
tot = ;
memset(C, , sizeof(C));
} int main() {
while (scanf("%d%d", &n, &m) == && n) {
init();
for (int i = ; i < n; i++) scanf("%s", str[i]);
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if (str[i][j] == '.') {
mp[i][j] = ++tot;
}
}
}
for (int i = ; i < n; i++) {
for (int j = ; j < m; j++) {
if (str[i][j] == '.') {
for (int t = ; t < ; t++) {
int ii = i + dx[t], jj = j + dy[t];
if (ii < || ii >= n || jj < || jj >= m || str[ii][jj] == '*') continue;
C[mp[i][j]][mp[i][j]]++;
C[mp[i][j]][mp[ii][jj]]--;
}
}
}
}
LL ans=Det(tot - );
printf("%lld\n", ans);
}
return ;
}
BZOJ 4031: [HEOI2015]小Z的房间 Matrix-Tree定理的更多相关文章
- BZOJ.4031.[HEOI2015]小Z的房间(Matrix Tree定理 辗转相除)
题目链接 辗转相除解行列式的具体实现? 行列式的基本性质. //864kb 64ms //裸的Matrix Tree定理.练习一下用辗转相除解行列式.(因为模数不是质数,所以不能直接乘逆元来高斯消元. ...
- BZOJ 4031: [HEOI2015]小Z的房间(Matrix Tree)
传送门 解题思路 矩阵树定理模板题.矩阵树定理是求图中最小生成树个数,做法是首先求出基尔霍夫矩阵,就是度数矩阵\(-\)邻接矩阵.然后再求出这个矩阵的行列式,行列式的求法就是任意去掉一行一列,然后高斯 ...
- BZOJ 4031 [HEOI2015]小Z的房间(Matrix-Tree定理)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4031 [题目大意] 你突然有了一个大房子,房子里面有一些房间. 事实上,你的房子可以看 ...
- BZOJ 4031: [HEOI2015]小Z的房间 [矩阵树定理 行列式取模]
http://www.lydsy.com/JudgeOnline/problem.php?id=4031 裸题........ 问题在于模数是$10^9$ 我们发现消元的目的是让一个地方为0 辗转相除 ...
- BZOJ 4031: [HEOI2015]小Z的房间 (矩阵树定理 板题)
背结论 : 度-邻 CODE1 O(n3logn)O(n^3logn)O(n3logn) #include <bits/stdc++.h> using namespace std; typ ...
- bzoj 4031: [HEOI2015]小Z的房间 轮廓线dp
4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 98 Solved: 29[Submit][Status] ...
- BZOJ 4031: [HEOI2015]小Z的房间 高斯消元 MartixTree定理 辗转相除法
4031: [HEOI2015]小Z的房间 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个 ...
- 【刷题】BZOJ 4031 [HEOI2015]小Z的房间
Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形,每个格子是一个房间或者是一个柱子.在一开始的时候,相邻的格子之间都有墙隔着. ...
- BZOJ 4031 HEOI2015 小Z的房间 基尔霍夫矩阵+行列式+高斯消元 (附带行列式小结)
原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可 ...
随机推荐
- SDWebImage缓存清理
//计算缓存大小 [SDImageCache sharedImageCache] getSize] //清理缓存 SDImageCache *sd = [[SDImageCache alloc]ini ...
- python基础:day3作业
修改haproxy配置文件 基本功能:1.获取记录2.添加记录3.删除记录 代码结构:三个函数一个主函数 知识点:1.python简单数据结构的使用:列表.字典等 2.python两个模块的使用:os ...
- think in java 第四版读书笔记 第一章对象导论
很久没有碰过java了,为了项目需要以及以后找工作,还是有必要将think in java通读一遍.欢迎大家一起讨论学习 1.1抽象过程 面向对象语言的5个特性: 1.万物皆对象 任何事物都可以抽象为 ...
- double与int类型自动转换
package com.abc.test; public class SumTest { public static void main(String[] args) { //题目A:2+4+6+8+ ...
- 3月7日 Maximum Subarray
间隔2天,继续开始写LeetCodeOj. 原题: Maximum Subarray 其实这题很早就看了,也知道怎么做,在<编程珠玑>中有提到,求最大连续子序列,其实只需要O(n)的复杂度 ...
- Linux设备驱动01
准备: 1.英语-是工具 2.电路原理图-补充“数电”的知识 3.阅读内核代码的能力 linux:vi+ctags+cscope windows:source insight 驱动开发的步骤 I.编辑 ...
- Archiving
There are typically four steps of archving: Preprocessing Write Store Delete Normally Store is inv ...
- Windows phone(1)-ApplicationBar(应用栏)
在手机APP应用当中,我们往往会想某些操作能够进行快速访问或者能够持续显示用户信息的情况,比如像做泡泡堂游戏右边区域玩家信息 在wp7,wp8中为我们提供了ApplicationBar这样的控件来创建 ...
- 【风马一族_git_github】git与github的英文记录
Pull requests Issues Gist 请求 问题 要点 ---------------------------------------------------------------- ...
- CentOS学习笔记--基本命令--目录的相关操作
Linux基本命令--目录的相关操作 常见的处理目录的命令吧: cd:变换目录 pwd:显示目前的目录 mkdir:创建一个新的目录 rmdir:删除一个空的目录 cd (变换目录) cd是Chang ...