今天看了一下矩阵树定理,然后学了一下\(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. 2018年最值得关注的30个Vue开源项目

    译者按: 学习优秀的开源项目是提高代码水平最有效的方式. 原文: 30 Amazing Vue.js Open Source Projects for the Past Year (v.2018) 译 ...

  2. jquery中each中使用break和continue

    在jquery中each中直接使用break或者continue会提示:必须在循环中使用.会报错不能直接使用. 但是,是不是就不能用呢,答案是的,但是换种方法可以达到相同的效果: 可以只用return ...

  3. BZOJ1101: [POI2007]Zap(莫比乌斯反演)

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2951  Solved: 1293[Submit][Status ...

  4. CSS选择器:子选择符号

    <html> <head> <style type="text/css"> .class>h2{color:red} </style ...

  5. Win7怎么录制电脑屏幕视频

    我们在看视频的时候,经常会看到自己特别喜爱的视频,想要把其中的某些片段给录制下来,那么Win7怎么录制电脑屏幕视频?其实步骤很简单,下面就来分享下具体的步骤. 使用工具: 电脑 操作方法: 第一步.首 ...

  6. python模块--collections

    python的内建模块collections有几个关键的数据结构,平常在使用的时候,开发者可以直接调用,不需要自己重复制造轮子,这样可以提高开发效率. 1. deque双端队列 平常我们使用的pyth ...

  7. springboot 学习之路 9 (项目启动后就执行特定方法)

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  8. mysql的数据类型和字段属性

    本文内容: 数据类型 数值类型 整数型 浮点型 定点型 日期时间类型 字符串类型 补充: 显示宽度与zerofll 记录长度 字段属性 空\不为空值:NULL.NOT NULL 主键:primary ...

  9. 遇到一个很古怪的问题,C++类static const成员的初始化

    在我的文件里有这class NFDuration, NFDuration.h里是这样的: // A Duration represents the elapsed time between two i ...

  10. 产品经理说|AIOps 让告警管理变得更智能

    AIOps 人工智能和IT运营支撑 Ops 之间的故事,愈演愈烈,已经成为当今运维圈的热门话题,我打算从2篇文档分享我们在 AIOps 上一些探索和实践.(本篇)为什么事件(告警)处理需要 AIOps ...