今天看了一下矩阵树定理,然后学了一下\(O(n ^ 3)\)的方法求行列式。

哦对了,所有的证明我都没看……




这位大佬讲的好呀:

[学习笔记]高斯消元、行列式、Matrix-Tree 矩阵树定理




关于模数不是质数的情况,我看了半天才懂:其实就是加速了两行的辗转相减,把一次次减换成了取模。然后别忘了每一次交换两行的时候行列式要取相反数。




[HEOI2015]小Z的房间

这题就是板儿题了。把是'.'的格子记录下来,然后构造基尔霍夫矩阵就行了。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 105;
const ll mod = 1e9;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
} int n, m, cnt = 0, id[maxn][maxn];
char s[maxn][maxn];
ll f[maxn][maxn]; In void add(int x, int y)
{
++f[x][x], ++f[y][y], --f[x][y], --f[y][x];
} In ll Gauss(int n)
{
ll ans = 1;
for(int i = 1; i <= n; ++i)
{
for(int j = i + 1; j <= n; ++j)
while(f[j][i])
{
ll d = f[i][i] / f[j][i];
for(int k = i; k <= n; ++k) f[i][k] = (f[i][k] - d * f[j][k] % mod + mod) % mod;
swap(f[i], f[j]), ans = -ans;
}
ans = (ans * f[i][i] % mod + mod) % mod;
}
return ans;
} int main()
{
n = read(), m = read();
for(int i = 1; i <= n; ++i) scanf("%s", s[i] + 1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j) if(s[i][j] == '.') id[i][j] = ++cnt;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
if(id[i][j])
{
if(id[i][j + 1]) add(id[i][j], id[i][j + 1]);
if(id[i + 1][j]) add(id[i][j], id[i + 1][j]);
}
write(Gauss(cnt - 1)), enter;
return 0;
}



[[CQOI2018]社交网络](https://www.luogu.org/problemnew/show/P4455)
这题让求的是以节点1为根的生成树个数,于是我们把矩阵的第一行第一列消去,剩下的求一个行列式就是答案了。
注意连边是反的。
```c++
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 255;
const ll mod = 1e4 + 7;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans = 10) write(x / 10);
putchar(x % 10 + '0');
}

int n, m;

ll f[maxn][maxn];

In ll quickpow(ll a, ll b)

{

ll ret = 1;

for(; b; b >>= 1, a = a * a % mod)

if(b & 1) ret = ret * a % mod;

return ret;

}

In ll Gauss()

{

ll ans = 1;

for(int i = 2; i <= n; ++i)

{

int pos = i;

while(!f[pos][i]) ++pos;

if(pos ^ i) swap(f[i], f[pos]), ans = mod - ans;

if(!f[i][i]) return 0; //如果每一行第i个数都输0,才返回0

ans = ans * f[i][i] % mod;

ll inv = quickpow(f[i][i], mod - 2);

for(int j = i + 1; j <= n; ++j)

{

ll tp = f[j][i] * inv % mod;

for(int k = i; k <= n; ++k) f[j][k] = (f[j][k] - tp * f[i][k] % mod + mod) % mod;

}

}

return ans;

}

int main()

{

n = read(), m = read();

for(int i = 1; i <= m; ++i)

{

int y = read(), x = read(); //x -> y

++f[y][y], --f[y][x];

}

write(Gauss()), enter;

return 0;

}

[HEOI2015]小Z的房间 && [CQOI2018]社交网络的更多相关文章

  1. bzoj 4031: [HEOI2015]小Z的房间 轮廓线dp

    4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 98  Solved: 29[Submit][Status] ...

  2. 【bzoj4031】[HEOI2015]小Z的房间 解题报告

    [bzoj4031][HEOI2015]小Z的房间 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含\(n*m\)个格子的格状矩形,每个格子是一个房 ...

  3. 【BZOJ 4031】 4031: [HEOI2015]小Z的房间 (Matrix-Tree Theorem)

    4031: [HEOI2015]小Z的房间 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 1089  Solved: 533 Description ...

  4. BZOJ 4031: [HEOI2015]小Z的房间 高斯消元 MartixTree定理 辗转相除法

    4031: [HEOI2015]小Z的房间 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4031 Description 你突然有了一个 ...

  5. 【bzoj4031】[HEOI2015]小Z的房间 Matrix-Tree定理+高斯消元

    [bzoj4031][HEOI2015]小Z的房间 2015年4月30日3,0302 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的 ...

  6. 【bzoj4031】[HEOI2015]小Z的房间 && 【bzoj4894】天赋 (矩阵树定理)

    来两道矩阵树模板: T1:[bzoj4031][HEOI2015]小Z的房间 Description 你突然有了一个大房子,房子里面有一些房间.事实上,你的房子可以看做是一个包含n*m个格子的格状矩形 ...

  7. 【刷题】BZOJ 4031 [HEOI2015]小Z的房间

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

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

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

  9. bzoj4031 [HEOI2015]小Z的房间

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

随机推荐

  1. 详解Parcel:快速,零配置web应用打包工具。

    译者按: 新一代Web应用打包工具Parcel横空出世,快速.零配置的特点让人眼前一亮. 原文: Everything You Need To Know About Parcel: The Blazi ...

  2. 这几天上海移动网络可以直接打开 Google Play 了

    这几天上海移动网络可以直接打开 Google Play (谷歌应用商店)了. 速度还不错.基本无延迟. 想当初,为了防止国内应用市场里的木马或恶意软件,想从 Google Play 应用市场下载,折腾 ...

  3. JSON基础知识点

    一.介绍: JSON是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成. 二.数据格式: 1.JSON建构于两种数据格式: “名称/值”对(键值对)的集合,不同的语言中,它被理解为 ...

  4. HanLP汉语言分析框架

    HanLP(Han Language Processing)是由一系列模型与算法组成的Java工具包,目标是普及自然语言处理在生产环境中的应用. HanLP具备功能完善.性能高效.架构清晰.语料时新. ...

  5. linux:644、755、777权限详解

    第一位7等于4+2+1,rwx,所有者具有读取.写入.执行权限: 第二位5等于4+1+0,r-x,同组用户具有读取.执行权限但没有写入权限: 第三位5,同上,也是r-x,其他用户具有读取.执行权限但没 ...

  6. [20181204]低版本toad 9.6直连与ora-12505.txt

    [20181204]低版本toad 9.6直连与ora-12505.txt --//我们生产系统还保留有一台使用AMERICAN_AMERICA.US7ASCII字符集的数据库,这样由于toad新版本 ...

  7. 修正Percona Monitoring Plugins for Zabbix的一处脚本Bug

    今天小试了一把Percona Monitoring Plugins for Zabbix模板,自己辛辛苦苦写的那一大堆Python脚本,貌似用这个模板全都覆盖到了.但是,我也发现最新的版本percon ...

  8. c/c++ lambda 表达式 剖析

    lambda 表达式 剖析 大前提:捕获列表里变量的确定时机. 捕获列表和参数列表有区别,捕获列表里的变量,是在捕获的时间点就确定了,而不是在lambda调用时确定,参数列表是在调用时才确定.所以当捕 ...

  9. 用emacs 阅读 c/c++ 代码

    在emacs编程中有以下需求 从调用一个函数的地方跳转到函数的定义的地方 或是反过来从函数定义的地方列出所有调用这个函数的地方 实现办法 需要安装以下软件 gnu global(阅读源代码的工具)官网 ...

  10. 数据挖掘---Matplotib的学习

    什么是matplotlib mat - matrix 矩阵                二维数据 - 二维图表            plot - 画图            lib - libra ...